求10000 以内的阶乘 与 字符串最大跨距

求10000 以内的阶乘

这是大整数运算,用数组存储,逐位计算并存储即可。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
vector<int> result;

void sum_n(int n){
    for (int i = 2; i <= n; i++) {
        int carry = 0;
        for (int j = 0; j < result.size(); j++) {
            int product = result[j] * i + carry;
            result[j] = product % 10;
            carry = product / 10;
        }
        // 处理剩余进位
        while (carry > 0) {
            result.push_back(carry % 10);
            carry /= 10;
        }
    }
}

int main() {
    int n;
    cin >> n;
    result.push_back(1);  // 初始为 1
    sum_n(n);
    
    // 输出结果(逆序输出)
    for (int i = result.size() - 1; i >= 0; i--) {
        cout << result[i];
    }
    cout << endl;
    return 0;
}

字符串最大跨距

先存储s,s1,s2 然后在s中查找s1,(若不存在,输出-1)若存在,由于要找最大跨距,反转s2与截去s1后的s,在s中查找s2(返回索引pos2),(若不存在,输出-1)若存在,进行简单计算(s.length() - pos2 - s2.length())并输出索引即为最大跨距。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int main(){
    string S;
    getline(cin,S);
    
    size_t pos1 = S.find(',');
    size_t pos2 = S.find(',',pos1 + 1);
    string s = S.substr(0,pos1);
    string s1 = S.substr(pos1 + 1,pos2 - pos1 - 1);
    string s2 = S.substr(pos2 + 1);
    // cout << s << ' ' << s1 << ' ' << s2 << endl;
    
    if((pos1 = s.find(s1)) != string::npos){//若s中存在s1
        s = s.substr(pos1 + s1.length());//截去s1
        reverse(s.begin(),s.end());//反转剩下的s
        // cout << s << endl;
        reverse(s2.begin(),s2.end());//反转s2
        if((pos2 = s.find(s2)) != string::npos){//若剩下的s中存在s2,在反转后的s中找反转的s2(记录索引值pos2)
            cout << (s.length() - pos2 - s2.length()) << endl;//正向输出最大索引跨距
        }else cout << -1;
    }else cout << -1;
    
    return 0;
}
相关推荐
c++之路18 分钟前
C++信号处理
开发语言·c++·信号处理
_深海凉_2 小时前
LeetCode热题100-寻找两个正序数组的中位数
算法·leetcode·职场和发展
故事还在继续吗2 小时前
C++20关键特性
开发语言·c++·c++20
青少儿编程课堂3 小时前
2026青少儿信息素养大赛备赛指南!Python/Scratch/C++备考要点
开发语言·c++·python
旖-旎3 小时前
深搜练习(电话号码字母组合)(3)
c++·算法·力扣·深度优先遍历
谭欣辰3 小时前
C++快速幂完整实战讲解
算法·决策树·机器学习
Mr_pyx3 小时前
【LeetHOT100】随机链表的复制——Java多解法详解
算法·深度优先
AIFarmer4 小时前
【无标题】
开发语言·c++·算法
John_ToDebug4 小时前
WebHostView 与 TabStrip 交互机制深度解析
c++·chrome·windows
AGV算法笔记4 小时前
CVPR 2025 最新感知算法解读:GaussianLSS 如何用 Gaussian Splatting 重构 BEV 表示?
算法·重构·自动驾驶·3d视觉·感知算法·多视角视觉