1 条题解
-
0
C :
#include<string.h> #include<stdio.h> int main() { char a[101]; char b[101]; char c[101]; memset(a, '0', sizeof(a)); memset(b, '0', sizeof(b)); memset(c, '0', sizeof(c)); gets(a); gets(b); int i, j; int flag; int d; int temp_1, temp_2 = 0; int len_a, len_b, len_c; len_a = strlen(a); len_b = strlen(b); if(len_a >= len_b) { len_c = len_a; flag = 1; d = len_a - len_b; } else { len_c = len_b; flag = 0; d = len_b - len_a; } if(flag) { for(i = len_c-1; i >= 0; i--) { if(i >= d) { temp_1 = a[i] - '0' + b[i-d] - '0' + temp_2; temp_2 = temp_1 / 10; c[i+1] = temp_1 % 10 + '0'; if(temp_2) { c[i] = temp_2 + '0'; } } else { temp_1 = a[i] - '0' + temp_2; temp_2 = temp_1 / 10; c[i+1] = temp_1 % 10 + '0'; if(temp_2) { c[i] = temp_2 + '0'; } } } } else { for(i = len_c-1; i >= 0; i--) { if(i >= d) { temp_1 = a[i-d] - '0' + b[i] - '0' + temp_2; temp_2 = temp_1 / 10; c[i+1] = temp_1 % 10 + '0'; if(temp_2) { c[i] = temp_2 + '0'; } } else { temp_1 = b[i] - '0' + temp_2; temp_2 = temp_1 / 10; c[i+1] = temp_1 % 10 + '0'; if(temp_2) { c[i] = temp_2 + '0'; } } } } for(i = 0; i <= len_c; i++) { if(i == 0 && c[i] == '0') { continue; } printf("%c", c[i]); } return 0; }
C++ :
#include<iostream> #include<algorithm> #include<string> using namespace std; const int N = 105; int a[N],b[N],c[N]; void input(int a[]){ fill(a,a+N,0); string s; cin>>s; a[0]=s.size(); for (int i=1; i<=a[0]; i++) a[i]=s[a[0]-i]-48; } void add(int a[],int b[],int c[]){ fill(c,c+N,0); c[0]=a[0]>b[0]?a[0]:b[0]; for (int i=1; i<=c[0]; i++) { c[i]+=a[i]+b[i]; c[i+1]+=c[i]/10; c[i]%=10; } if (c[c[0]+1]) c[0]++; } void output(int a[]){ for (int i=a[0]; i>=1; i--) cout<<a[i]; cout<<endl; } int main(){ input(a); input(b); add(a,b,c); output(c); return 0; }
- 1
信息
- ID
- 507
- 时间
- 1000ms
- 内存
- 3MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者