POJ 1503 Integer Inquiry
发布时间:2020-12-14 02:53:44 所属栏目:大数据 来源:网络整理
导读:来源:http://poj.org/problem?id=1503 Integer Inquiry Time Limit: ?1000MS ? Memory Limit: ?10000K Total Submissions: ?30480 ? Accepted: ?11870 Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exp
来源:http://poj.org/problem?id=1503
Integer Inquiry
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 Source
East Central North America 1996
题意: 输入n行正整数数以0结束,输出这n行数的和。 题解: 简单大数加法,我用 压位高精度写的~~ AC代码: #include<iostream> #include<string> #include<cmath> #include<cstring> #define carry 100000000 #define maxn 115 using namespace std; string str,sum; void str2num(string &x,int *num){ int len = x.size(),pos = 0; for (int i = 1; i <= len / 8; i++){ string temp = x.substr(len - 8 * i,8); for (int i = 0; i<8; i++) num[pos] += (temp[7 - i] - '0')*pow(10,i); pos++; } int left = len % 8; if (left){ for (int i = 0; i < left; i++) num[pos] += (x[left - i-1] - '0')*pow(10,i); pos++; } } void num2str(int x,string &str){ int temp[10] = { 0 },pos=0; while (x){ temp[pos++] = x % 10; x /= 10; } for (int i = 7; i >= 0; i--) str += temp[i] + '0'; } void Add(string x,string & y) { string rest = ""; int num1[maxn] = { 0 },num2[maxn] = { 0 }; int len = x.size() > y.size() ? x.size() : y.size(); str2num(x,num1); // cout << "num1[0]=" << num1[0] << endl; str2num(y,num2); for (int i = 0; i <= len/8; i++){ num1[i] += num2[i]; if (num1[i] >= carry){ num1[i + 1] += num1[i] / carry; num1[i] %= carry; } } int k; for (k = len; k>0 && !num1[k]; k--); for (; k>= 0; k--) num2str(num1[k],rest); y = rest; } int main() { cin.sync_with_stdio(false); sum = "0"; while (cin >> str,str != "0"){ Add(str,sum); } int i; for (i = 0; i <sum.size();i++) if (sum[i] != '0')break; sum = sum.substr(i,sum.size() - i+1); cout << sum << endl; return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |