POJ 1503 Integer Inquiry
2014-12-08 22:24
183 查看
来源: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
Sample Output
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;
}
Integer Inquiry
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 30480 | Accepted: 11870 |
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;
}
相关文章推荐
- POJ 1503 Integer Inquiry JAVA .
- POJ 1503 Integer Inquiry 笔记
- POJ 1503 && HDU 1047 Integer Inquiry(高精度)
- POJ 1503 Integer Inquiry 解题报告
- POJ 1503 Integer Inquiry
- POJ 1503 Integer Inquiry
- POJ 1503 Integer Inquiry(高精度运算)
- (高精度运算4.7.23)POJ 1503 Integer Inquiry(大数累加)
- POJ1503 UVA424 UVALive5399 Integer Inquiry【大数】
- poj 1503 Integer Inquiry
- POJ-1503 Integer Inquiry
- POJ 1503 Integer Inquiry【大数相加 java】
- poj 1503 Integer Inquiry
- POJ 1503 Integer Inquiry(高精度整数)
- POJ--1503:Integer Inquiry 大数加法
- POJ 1503 Integer Inquiry【大数相加 java】
- POJ 1503 Integer Inquiry 美丽的大整数相加 + 测试数据
- poj 1503 Integer Inquiry高精度
- POJ 刷题系列:1503 Integer Inquiry
- POJ 1503 Integer Inquiry 高精度加法