求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;
}
相关推荐
布莱克6051 小时前
C/C++ 中变量的声明与定义:区别、联系与常见误区
c语言·开发语言·c++·声明·定义
ZhengEnCi3 小时前
大模型的分类到底怎么分:从基础模型到推理模型、从通用到专用的完整图谱
算法
取名真是3 小时前
Python 算法知识点从基础到进阶
开发语言·python·算法
罗西的思考4 小时前
【轻量化复刻大模型 / KIMI K3】KDA 模块交互式演示
人工智能·算法·机器学习
沫璃染墨6 小时前
从零入门计算机网络系列(十二):HTTP长连接与会话管理详解——从Connection到Cookie与Session
linux·服务器·c++·tcp/ip·ubuntu·http·交互
nm_zfyjt6 小时前
信息流广告投放优化实践:线索成本评估模型与转化链路重构(家装建材案例)
算法·重构·内蒙古朋友圈广告服务商·内蒙古朋友圈获客
xiaoye-duck7 小时前
《C++ 项目:手写大模型接入 SDK》DeepSeek接口设计(下):流式响应实现和测试
开发语言·网络·c++·语言模型
w_zero_one7 小时前
算法--构造
java·数据结构·算法
其实防守也摸鱼7 小时前
信创安全竞赛知识总结与题库
大数据·网络·人工智能·算法·安全·php
旖旎夜光8 小时前
【C++ 基础入门】类和对象(上):封装、对象模型与 this 指针
开发语言·c++·笔记·学习