蓝桥杯/上机题目(第二周) 打卡Day1

蓝桥杯/上机题目(第二周) 打卡Day1

题目1 AcWing3389. N 的阶乘
题目2 AcWing3448. 基本算术
题目3 AcWing3453. 整数查询

1.AcWing3389. N 的阶乘

思路: 高精度乘法复习题,注意一下预处理打表即可

#include<iostream>
#include<string>
#include<vector>

using namespace std;
const int N = 1010;

vector<int> f[N];

vector<int> mul(vector<int>& temp, int b)
{
    vector<int>res;
    int t = 0;
    for(int i = 0; i < temp.size() || t; i ++){
        if(i < temp.size()) t += temp[i] * b;
        res.push_back(t % 10);
        t /= 10;
    }
    while(res.size() > 1 && res.back() == 0) res.pop_back();
    return res;
}


int main()
{
    int n;
    f[0] = {1}, f[1] = {1};
    for(int i = 2; i <= N; i ++){
        f[i] = mul(f[i -1] , i);
    }
    while(cin >> n){
        auto temp = f[n];
        for(int i = temp.size() - 1; i >= 0; i --) cout << temp[i];
        cout << endl;
    }
    
    return 0;
}

2.AcWing3448. 基本算术

思路 :高精度加法复习题

#include<iostream>
#include<vector>
#include<string>

using namespace std;



int add(vector<int> temp1, vector<int>temp2){
    int t = 0;
    int s = 0;
    vector<int> res;
    for(int i = 0; i < temp1.size() || i < temp2.size(); i ++){
        if(i < temp1.size()) t += temp1[i];
        if(i < temp2.size()) t += temp2[i];
        if(t >= 10) s ++;
        res.push_back(t % 10);
        t /= 10;
    }
    if(t){
        res.push_back(1);
    }
    return s;
}

int main()
{
    
    while(true){
        vector<int>a ,b;
        string s1, s2;
        cin >> s1 >> s2;
        if(s1 == "0" && s2 == "0") break;
        for(int i = s1.size() - 1; i >= 0; i --) a.push_back(s1[i] - '0');
        for(int i = s2.size() - 1; i >= 0; i --) b.push_back(s2[i] - '0');
        
        int res = add(a,b);
        if(res > 1){
           printf("%d carry operations.\n",res); 
        }else if(res == 1){
            printf("%d carry operation.\n",res); 
        }else{
            puts("No carry operation.");
        }
    } 
    
    return 0;
}

3.AcWing3453. 整数查询

思路:高精度加法复习题

#include<iostream>
#include<vector>
#include<string>
#include<queue>

using namespace std;

vector<int>add(vector<int>temp1, vector<int>temp2){
    int t = 0;
    vector<int>res;
    for(int i = 0; i < temp1.size()|| i < temp2.size(); i ++){
        if(i < temp1.size()) t += temp1[i];
        if(i < temp2.size()) t += temp2[i];
        res.push_back(t % 10);
        t /= 10;
    }
    if(t) res.push_back(1);
    return res;
}



int main()
{
    vector<int>res = {0};
    queue<vector<int>>q;
    string temp;
    while(cin >> temp, temp !="0"){
        vector<int> b;
        for(int i = temp.size() - 1; i >= 0; i -- ) b.push_back(temp[i] - '0');
        q.push(b);
    }
    
    while(q.size()){
        auto t = q.front();
        q.pop();
        res = add(t,res);
    }
    while(res.size() > 1 && res.back() == 0) res.pop_back();
    for(int i = res.size() - 1; i >= 0; i --) cout << res[i];
    cout << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值