1503. Integer Inquiry
2017-07-21 18:28
204 查看
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.
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
注意:
求和的sum数组大小至少sum[102];
题目中有no VeryLongInteger will be negative 这样的描述,所以输入中可能有以0开头的数,判断输入结束应该使用strcmp(a,”0”)语句,而非a[0]!=0;
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
思路
这道题比较简单,单独考虑每一位的相加,处理一下进位就ok了!注意:
求和的sum数组大小至少sum[102];
题目中有no VeryLongInteger will be negative 这样的描述,所以输入中可能有以0开头的数,判断输入结束应该使用strcmp(a,”0”)语句,而非a[0]!=0;
代码
#include <iostream> #include <string.h> using namespace std; int main() { char a[101]; int sum[102] = { 0 }; int i = 0,j = 0; cin >> a; while (strcmp(a,"0")) { for (i = 0; i < strlen(a); i++) { sum[i] += a[strlen(a) - i - 1] - '0'; if (sum[i] >= 10) //进位的处理 { sum[i] -= 10; sum[i + 1] += 1; j = i + 1; } else { j = i; } } cin >> a; } for (i = j; i >= 0; i--) //将sum逆序输出 { cout << sum[i]; } return 0; }
相关文章推荐
- 北大ACM1503——Integer Inquiry
- POJ1503——字符串——Integer Inquiry
- POJ 1503 || HOJ 1435 Integer Inquiry
- POJ 1503 Integer Inquiry
- HDU 1503 Advanced Fruits
- poj1503java大数
- poj 1503 大数相加
- uva-424 - Integer Inquiry
- dp--hdu 1503
- Integer Inquiry
- UVa 424 - Integer Inquiry
- SP2-1503: Unable to initialize Oracle call interface(同时装10g和11g)
- CSUST 1503 ZZ买衣服
- Integer Inquiry_hdu_1047(大数).java
- (高精度运算4.7.23)POJ 1503 Integer Inquiry(大数累加)
- bzoj 1503: [NOI 2004]郁闷的出纳员
- UVa 424 整数查询 poj 1503
- hdu 1503(最长公共子序列的升级版)
- ORACLE:SP2-1503: 无法初始化 Oracle 调用界面 SP2-1503: 无法初始化 Oracle 问题的解决办法
- UVA - 424 Integer Inquiry