1159. Sum
|
Time Limit: 1sec Memory Limit:32MB
Description
Given several positive integers, calculate the sum of them.
Input
There are several test cases, one line for each case. For each line, the first number, N (N<=100000), is the number of positive numbers and then N positive numbers followed. The sum will not exceed 10
100.
Output
Output each sum in a single line without leading zeros.
Sample Input
3 1 2 31 1232 1111111111111111111111111111111111 1111111111111111111111111111111111
Sample Output
61232222222222222222222222222222222222 Problem Source: ZSUACM Team Member |
代码有参考别人的 不记得是哪里来的了啦 高精度还是需要细心啊 但是不难理解
#include <iostream>
#include <string>
#include <memory.h>
using namespace std;
void input (int num[]) { //从string变成int的数组
string a;
cin>>a;
int size = a.length();
for (int i = 0; i< size; i++) {
num[i] = a.at(size - i -1) - '0';
}
}
void output (int sum[]){
int i;
for (i = 199; sum[i] == 0; i--) {} //这里吧0的位都跳过了(倒序输出)
for (;i >= 0; i--) { //从第一个非0位开始输出
cout<<sum[i];
}
}
void add (int num[], int sum[]) {
for(int i = 0; i < 200; i++) {
sum[i] += num[i];
sum[i+1] += sum[i]/10;
sum[i] = sum[i] % 10;
}
}
int main () {
int n;
while (cin>>n && n) {
int sum[200], num[200];
memset(sum, 0, sizeof(sum)); //头文件#include <memory.h> 不要漏了
memset(num, 0, sizeof(num));
for (int i = 0; i < n; i++) {
input(num);
add(num, sum);
memset(num, 0, sizeof(num));
}
output(sum);
cout<<endl;
}
return 0;
}