UVa 424 Integer Inquiry(整数查询)
来源:互联网 发布:jquery weui js包下载 编辑:程序博客网 时间:2024/05/16 12:14
Integer Inquiry
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
题目大意:
大整数相加,实际上像是小学的竖式求和,一位一位的相加就可以了。
注意点:WA了两次,后来发现,原来最后输出之后,要有个换行符!!!
后来发现,不一定需要判断最后的一行是0,可以一直读到EOF。
下面贴出两种不同想法的代码:
代码一:
将所有行都读完,再来进行大数计算。
#include <stdio.h>#include <stdlib.h>#include <string.h>#define INF 1e9char str[105][105];int num[105][105];int ans[10010];int main(){ int i, j; int len, count, max = -INF, flag; i = 0; while(~scanf("%s", str[i])){ if(str[i][0] == '0') break; i++; } count = i; memset(num, 0, sizeof(num)); for(i = 0; i<count; i++){ len = strlen(str[i]); for(j = 0; j<len; j++){ num[i][len-j] = (str[i][j]-'0'); } if(max<len) max = len; } memset(ans, 0, sizeof(ans)); for(i = 0; i<count; i++){ for(j = 1; j<=max; j++){ ans[j] += num[i][j]; } } for(i = 1; i<10010;i++){ ans[i] += ans[i-1]/10; ans[i-1] %= 10; } flag = 0; for(i=10009; i>0; i--){ if(flag){ printf("%d", ans[i]); } else if(ans[i]){ flag = 1; printf("%d", ans[i]); } } printf("\n"); return 0;}
没读一行字符串,进行一次大数相加。
#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>char str[100];int num[100];int ans[10000];int main(){ int i, j; int len, flag; memset(ans, 0, sizeof(ans)); while(~scanf("%s", str)){ len = strlen(str); memset(num, 0, sizeof(num)); for(i = 0; i<len; i++) num[len-i] = str[i]-'0'; for(i = 1; i<=len; i++) ans[i] += num[i]; for(i = 1; i<10000; i++){ ans[i] += (ans[i-1]/10); ans[i-1] %= 10; } } flag = 0; for(i=10000-1; i>0; i--){ if(flag) printf("%d", ans[i]); else if(ans[i]){ printf("%d", ans[i]); flag = 1; } } printf("\n"); return 0;}
- UVa 424 Integer Inquiry(整数查询)
- uva 424(Integer Inquiry)高精度大整数加法
- uva 424 - Integer Inquiry
- UVa 424 - Integer Inquiry
- UVa 424 - Integer Inquiry
- uva-424 - Integer Inquiry
- uva 424 - Integer Inquiry
- UVa 424 - Integer Inquiry
- UVA 424 Integer Inquiry
- UVa 424 - Integer Inquiry
- UVa 424 - Integer Inquiry
- UVa 424 - Integer Inquiry
- uva 424 - Integer Inquiry
- UVA - 424 - Integer Inquiry
- UVA 424 Integer Inquiry
- UVa 424 Integer Inquiry
- uva 424 Integer Inquiry
- UVA 424 Integer Inquiry
- 应用界面主题Theme使用方法
- 在社会上闯荡需要牢记的
- Jetty Struts2 Result ‘Null’ Not Found
- JSP页面禁用缓存
- JAVA_链表转换为树
- UVa 424 Integer Inquiry(整数查询)
- 写给cocos2d-x的新人,,所有疑难杂症,全都正解,包括原理,别的不用看了。(一)
- 引擎筹备
- vsftpd 服务器安装及配置
- 韩国泛泰A820L官方4.0ROM音乐播放歌曲名乱码修复记录
- UVa 299 Train Swapping(列车交换)
- C++模板学习
- 如何判断一个数是否为2的N次方
- 黑马程序员——面向对象、内部类、多态