Input
Code: Select all
1
1
1
1
1
1
1
1
1
1
0
Code: Select all
10
![8)](./images/smilies/icon_cool.gif)
Moderator: Board moderators
Code: Select all
1
1
1
1
1
1
1
1
1
1
0
Code: Select all
10
Code: Select all
1a
0
Code: Select all
29
Code: Select all
-2
-1
0
Code: Select all
*3
Code: Select all
000
0
but we aren't accustomed to problem statements always being right. It could be misleading in saying that you should discard input that is negative or contains characters other than digits. For example, problem 1585 where it says "There is no spaces between `O' and `X'. " , when there could be spaces elsewhere.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).
Code: Select all
#include<stdio.h>
#include<string.h>
#define siz 120
int main()
{
char num[102],sum[siz],tmp;
int i,j,ln,res,carry;
memset(sum,0,siz);
while(1){
scanf("%s",num);
if(strcmp(num,"0")==0){
break;
}
ln=strlen(num);
for(i=0;i<ln/2;i++){
tmp = num[i];
num[i]=num[ln-1-i];
num[ln-1-i]=tmp;
}
carry = 0;
for(i=0;i<siz;i++){
if(i<ln){
j=num[i]-'0';
}
else{
j=0;
}
sum[i]= carry+sum[i]+j;
if(sum[i]>9){
sum[i]=sum[i]%10;
carry = 1;
}
else{
carry=0;
}
}
}
for(i=siz-1;i>=0;i--){
if(sum[i]!=0)break;
}
for(;i>=0;i--){
printf("%d",sum[i]);
}
return 0;
}
Code: Select all
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int main() {
vector<int> sum(1000, 0);
char str[101];
int i;
while(scanf("%s", str) && str[0] != '0') {
i = 0;
int n = strlen(str);
while(i < n) {
sum[i] += (str[n - 1 - i] - '0');
i++;
}
}
int last = 0;
for(i = 0;i < 1000;i++) {
sum[i + 1] += (sum[i] / 10);
sum[i] %= 10;
if (sum[i] != 0)
last = i;
}
for(i = last;i >= 0;i--) {
printf("%d", sum[i]);
}
return 0;
}