大数相加这种题一般是用字符数组或者 string 将其存储或读入,然后从个位开始一步一步模拟加法。容易遇到的坑就是两个数字之和的最高位进位问题,可以在进行加法之前先得到两个数字的位数,比较之后将小的那一个往大的那一个上加,相当于操作的是位数较大(也就是比较长)的那一个数字。例如是s1存了一个3000位的数字,s2存了2000位的数字,只需要将s1与s2之和通过不停的计算存储在s1对应的每一位中即可。
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
string add(string s1, string s2) {
if(s1.length() < s2.length()) {
string temp = s1;
s1 = s2;
s2 = temp;
}
for(int i = s1.length()-1, j = s2.length()-1; i>=0; i--, j--) {
s1[i] = char(s1[i] + (j >= 0 ? s2[j] - '0' : 0));
if(s1[i] - '0' >= 10) {
s1[i] = char((s1[i] - '0') % 10 + '0');
if(i)
s1[i - 1]++;
else
s1 = '1' + s1;
}
}
return s1;
}
string str[1005];
int main() {
int i, n;
str[1] = "1";
str[2] = "1";
for(i = 3; i < 1005; i++) {
str[i] = add(str[i - 2], str[i - 1]);
//printf("%s\n",str[i]);
}
cin >> n;
while(n--) {
int a;
cin >> a;
cout << str[a] << endl;
}
return 0;
}
这让我想到了刚开始刷 HDU 的时候做过的经典大数相加 HDU-1002 A+BⅡ。当时拿 char 数组顺序把两个数存了进去,然后逆序把个位放到最前面开始一位一位做加法,最后逆序输出,当时 ac 了感觉好厉害哈哈。附上当时 1002 的代码:
#include <stdio.h>
#include <string.h>
int main()
{
char a[1000], b[1000], c;
int m[1000], n[1000], t, cnt=1;
scanf("%d", &t);
getchar();
int k = t;
while(t--){
memset(m, 0, sizeof(m));
memset(n, 0, sizeof(n));
int i;
for(i=0; (c=getchar())!=' '; i++)
a[i] = c;
a[i] = '\0';
for(i=0; (c=getchar())!='\n'; i++)
b[i] = c;
b[i] = '\0';
int a_len = strlen(a), b_len = strlen(b), max_len;
for(int i=0; a_len-i-1>=0; i++)
m[i] = a[a_len-i-1] - '0';
for(int i=0; b_len-i-1>=0; i++)
n[i] = b[b_len-i-1] - '0';
if(a_len < b_len)
for(int i=a_len; i<b_len; i++)
m[i] = 0;
else if(a_len > b_len)
for(int i=b_len; i<a_len; i++)
n[i] = 0;
else{
m[a_len] = 0;
n[b_len] = 0;
}
max_len = (a_len>=b_len) ? a_len:b_len;
for(int i=0; i<max_len; i++){
m[i] += n[i];
if(m[i] >= 10){
m[i] -= 10;
m[i+1] += 1;
}
}
if(m[max_len]==0){
printf("Case %d:\n", cnt);
for(int i=0; i<a_len; i++)
printf("%c", a[i]);
printf(" + ");
for(int i=0; i<b_len; i++)
printf("%c", b[i]);
printf(" = ");
for(int i=max_len-1; i>=0; i--)
printf("%d", m[i]);
}
else{
printf("Case %d:\n", cnt);
for(int i=0; i<a_len; i++)
printf("%c", a[i]);
printf(" + ");
for(int i=0; i<b_len; i++)
printf("%c", b[i]);
printf(" = ");
for(int i=max_len; i>=0; i--)
printf("%d", m[i]);
}
if(cnt<k)
printf("\n\n");
else
printf("\n");
cnt++;
}
return 0;
}