POJ 1503 Integer Inquiry 简单大数相加

举报
谙忆 发表于 2021/05/28 08:30:32 2021/05/28
【摘要】 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 thos...

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

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output

370370367037037036703703703670

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
string doo(string str1,string str2){ string s; int str11=str1.length(); int str22=str2.length(); if(str11>str22){ for(int i=0;i<str11-str22;i++){ str2="0"+str2; } } else{ for(int i=0;i<str22-str11;i++){ str1="0"+str1; } } str11=str1.length(); int cmp=0; int cf=0; for(int i=str11-1;i>=0;i--){ cmp=str1[i]-'0'+str2[i]-'0'+cf; cf=cmp/10; cmp=cmp%10; s=char(cmp+'0')+s; } if(cf!=0){ s=char(cf+'0')+s; } return s;


}
int main(){ string sum="0"; string str; while(cin>>str){ if(str=="0"){ break; } sum=doo(sum,str); } cout<<sum<<endl; return 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

另外二种方法

#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char a[200],b[200],c[200],d[200];
int main()
{ int n1,n2; b[0]='0'; while(1){ scanf("%s",a); if(strcmp(a,"0")==0){ printf("%s\n",b); return 0; } int i; int n1=strlen(a)-1; int n2=strlen(b)-1; int p=0; for(i=0;n1>=0||n2>=0;i++,n1--,n2--) { if(n1>=0&&n2>=0){c[i]=a[n1]+b[n2]-'0'+p;} if(n1>=0&&n2<0){c[i]=a[n1]+p;} if(n1<0&&n2>=0){c[i]=b[n2]+p;} p=0; if(c[i]>'9'){c[i]=c[i]-10;p=1;} } if(p==1){ c[strlen(c)]='1'; } for(int i=0;i<strlen(c);i++){ d[strlen(c)-i-1]=c[i]; } strcpy(b,d); }
}

  
 
  • 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

——————————————————

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char a[200],b[200],c[200];
int main()
{ //int n1,n2; b[0]='0'; while(~scanf("%s",a)) { if(strcmp(a,"0")==0) { printf("%s\n",b); return 0; } int i; int n1=strlen(a)-1; int n2=strlen(b)-1; int p=0; i=n1; if(i<n2) i=n2; for(i; n1>=0||n2>=0; i--,n1--,n2--) { if(n1>=0&&n2>=0) { c[i]=a[n1]+b[n2]-'0'+p; } if(n1>=0&&n2<0) { c[i]=a[n1]+p; } if(n1<0&&n2>=0) { c[i]=b[n2]+p; } p=0; if(c[i]>'9') { c[i]=c[i]-10; p=1; } } if(p==1) { for(i=strlen(c); i>0; i--) c[i]=c[i-1]; c[0]='1'; } strcpy(b,c); }
}

  
 
  • 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

文章来源: chenhx.blog.csdn.net,作者:谙忆,版权归原作者所有,如需转载,请联系作者。

原文链接:chenhx.blog.csdn.net/article/details/49384575

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。