大数加法【蓝桥杯】【算法提高VIP】

原题链接:蓝桥杯算法提高VIP大数加法
此题严重卡 getchar()or gets()输入
1.ch=getchar()输入记录字符
参考代码:

#include<bits/stdc++.h>
using namespace std;
int a[1005];
int b[1005];
int c[1005];
int main(){
    int i=0,len1,len2,j=0;
    char ch;
    int temp;
    while((ch=getchar())!='\n'){
        temp=ch-'0';
        a[i]=temp;
        i++;   
    }
    len1=i; 
    //将个位置到前面,进行交换 
    for(int i=0;i<len1/2;i++){
        temp=a[i];
        a[i]=a[len1-1-i];
        a[len1-1-i]=temp;
    }
    i=0;
    while((ch=getchar())!='\n'){
        temp=ch-'0';
        b[i]=temp;
        i++;   
    }
    len2=i;
    //将个位置到前面,进行交换 
    for(int i=0;i<len2/2;i++){
        temp=b[i];
        b[i]=b[len2-1-i];
        b[len2-1-i]=temp;
    }
    //加法
    int s=0;
    for(int i=0;i=0;i--){
        if(c[i]!=0){
            for(int k=i;k>=0;k--){
                printf("%d",c[k]);
            }  
            break;
        }
     
    }
    printf("\n");
    return 0;
}

2.string sa,sb;

#include<bits/stdc++.h>
using namespace std;
const int maxn=1005;
int a[maxn],b[maxn];
int c[1005];
int main(){
    int i=0,j=0,len1,len2;
    int temp;
    string sa,sb;
    cin>>sa>>sb;
    len1=sa.length();
    len2=sb.length();
    for(int i=len1-1,j=0;i>=0;i--){
        a[j++]=sa[i]-'0';
    } 
    for(int i=len2-1,j=0;i>=0;i--){
        b[j++]=sb[i]-'0';
    }
    int s=0;
    for(int i=0;i=0;i--){
        if(c[i]!=0){
            for(int k=i;k>=0;k--){
                printf("%d",c[k]);
            }  
            break;
        }
     
    }
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值