九度OJ 1119:Integer Inquiry(整数相加) (大数运算)
2015-10-23 23:00
405 查看
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:679
解决:357
题目描述:
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.)
输入:
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.
输出:
Your program should output the sum of the VeryLongIntegers given in the input.
样例输入:
样例输出:
提示:
注意输入数据中,VeryLongInteger 可能有前导0
来源:2008年北京大学图形实验室计算机研究生机试真题
思路:
大数运算类题目,用字符串或数组来做。
代码:
内存限制:32 兆
特殊判题:否
提交:679
解决:357
题目描述:
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.)
输入:
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.
输出:
Your program should output the sum of the VeryLongIntegers given in the input.
样例输入:
123456789012345678901234567890 123456789012345678901234567890 123456789012345678901234567890 0
样例输出:
370370367037037036703703703670
提示:
注意输入数据中,VeryLongInteger 可能有前导0
来源:2008年北京大学图形实验室计算机研究生机试真题
思路:
大数运算类题目,用字符串或数组来做。
代码:
#include <stdio.h> #include <string.h> #define M 105 void print(int *a) { int i, j; for (i=0; i<M; i++) { if (a[i] != 0) break; } if (i == M) printf("0\n"); else { for (j=i; j<M; j++) printf("%d", a[j]); printf("\n"); } } int main(void) { int n, i, j; int a[M], b[M]; char s[M]; memset(a, 0, M*sizeof(int)); while (scanf("%s", s) != EOF) { if (strcmp(s, "0") == 0) break; memset(b, 0, M*sizeof(int)); n = strlen(s); j = M-n; for (i=0; i<n; i++) b[j++] = s[i]-'0'; for (i=M-1; i>0; i--) { a[i] += b[i]; if (a[i] >= 10) { a[i-1] ++; a[i] -= 10; } } } print(a); return 0; } /************************************************************** Problem: 1119 User: liangrx06 Language: C Result: Accepted Time:0 ms Memory:912 kb ****************************************************************/
相关文章推荐
- SpannableStringBuilder的使用
- Guidelines for the Design of Small Sewage Treatment Plants
- iOS -UIColor随机生成颜色的方法
- Handler任务模型之MessageQueue
- iOS -UIColor随机生成颜色的方法
- NGUI入门学习笔记
- 01-复杂度2 Maximum Subsequence Sum (25分)
- CoreBluetooth的实现代码
- UIView笔记
- NSOPeration 之UItabView无沙盒缓存——UITabView下载图片后 从操作缓冲池清除下载操作
- java.sql.SQLException: Can not issue empty query.
- GUI.tooltip
- UITableView
- CGContextRef CGMutablePathRef UIBezierPath
- NSOperation之为UItabView制作图片缓存——在didReceiveMemoryWarning方法中做图片缓存的清理操作
- UI中涉及到定时器的显示
- N-Queens and N-Queens II
- iOS笔记UI--UILabel
- poj 1141 Brackets Sequence(区间DP记录路径)
- hdoj--2767--Proving Equivalences (scc+缩点)