码迷,mamicode.com
首页 > 其他好文 > 详细

poj1503 Integer Inquiry

时间:2021-01-27 13:01:44      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:nta   int   sel   tree   ogr   string   weight   计算   c++   

Integer Inquiry

poj1503

题目

Problem 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

题目大意:输入数字,输入0终止,输出输入的数字之和。

解题思路

每输入一次,计算输入数字与上一次结果的和。

用字符数组存储输入的字符串(方便取数字的每一位),用整型数组逆序存储结果。

注意处理进位以及前缀0.

最后逆序输出结果

代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int result[1000] = {0};
	int length = 0;
	char num[100];
	while(cin >> num, strcmp(num, "0"))
	{
		int len = strlen(num);
		for(int i = len-1, index = 0; i >= 0; i--, index++)
		{
			result[index] += num[i] - ‘0‘;
			result[index+1] += result[index]/10;
			result[index] = result[index]%10;
		}
		if (len >= length)
		{
			length = len + 1;
		}
	}
	
	while(!result[length-1])
	{
		length--;
	}	
	for(int i = length-1; i >= 0; i--)
	{
		cout << result[i];
	}
	return 0;
}

poj1503 Integer Inquiry

标签:nta   int   sel   tree   ogr   string   weight   计算   c++   

原文地址:https://www.cnblogs.com/Jernewson/p/14327041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!