原题链接:蓝桥杯算法提高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;
}