0%

北大 acm 1053 Integer Inquiry 解题报告

Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12864 Accepted: 5066

Description

One of the first users of BIT’s new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.
“This supercomputer is great,” remarked Chip. “I only wish Timothy were here to see these results.” (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).

The final input line will contain a single zero on a line by itself.

Output

Your program should output the sum of the VeryLongIntegers given in the input.

Sample Input

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900

Sample Output

370370367037037036703703703670

思路:先将字符转化为数字,再逆向保存在整型数组里,然后从i=1开始每一个数组每一个数组的各个位与i=0的各个位相加,再做进位

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

using namespace std;

int add(int a[],int b[])
{
int i;
for(i=0;i<=300;i++)
a[i]+=b[i];

for(i=0;i<301;i++) //进位
if(a[i]>=10)
{
a[i+1]+=a[i]/10;
a[i]%=10;
}
return 0;
}

int main()
{
char str[103][103];
int num[103][300]={0};
int i=0,s=0,j,len;

freopen("t.txt","rt",stdin);

while(cin>>str[s]&&strcmp(str[s],"0")!=0)
s++;

for(i=0;i<s;i++) //转化为整型数组
{
len=strlen(str[i]);
for(j=0;j<len;j++)
num[i][j]=str[i][len-j-1]-'0'; //逆向保存
}

for(i=1;i<s;i++)
add(num[0],num[i]); //每个数组与num[0]相加

len=0;

for(i=299;i>=0;i--)
{
if(len==0&&num[0][i]==0)
continue;

len=1;
cout<<num[0][i]; //逆序输出
}
cout<<endl;


return 0;
}
谢谢您的打赏,我的大英雄 ^_^
Thank you for your generosity, my big hero ^_^

更多内容请关注公众号「西小玛」