HDOJ1047. Integer Inquiry(字符串+高精度加法)

Integer Inquiry

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23024    Accepted Submission(s): 6512


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.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.
 

Sample Input
 
 
11234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900
 
Sample Output
 
 
370370367037037036703703703670

【分析】题意:N个大整数m相加。N<=100 m的位数<=100且m非负。

        问题实质即高精度加法。解决此问题需要注意以下几点:

        (1)因数据规模要求,数的位数最高可到100位,因此考虑使用字符数组(字符串)保存输入的大整数;

        (2)数据并没有要求N个大整数位数相同,因此可进行预处理:先逆序(便于后续从低位向高位相加),然后找到最长的大整数,其余大整数高位补'0';

        (3)m个大整数由低位向高位,逐位相加,随时记录当前位及向高位的进位,直到最高位。若最高位仍有进位,原来的最高位"后退",进位作为新的最高位。

        (4)"0"作为一组输入的结束标志。需特别注意:若第一个大整数就是"0",则计算结果为0,并进入下一组输入。

        此外,题目要求多组输入间换一行。

#include <stdio.h>
#include <string.h>
#define maxn 105
#define maxlen 105
int N;
char temp[maxlen];
char num[maxn][maxlen];
char ret[2*maxlen];
//串"逆序" 
void Reverse(char *s)
{
	int i,j;
	char temp;
	i=0,j=strlen(s)-1;
	while(i<=j)
	{
		temp=s[i];
		s[i]=s[j];
		s[j]=temp;
		i++,j--;
	}
}
int main()
{
	int i,j;
	int ans,longest;    //ans-大整数数量 longest-最长的大整数长度 
	int sum,jw,retlen;  //sum-逐位和 jw-向高位的进位 retlen-结果的长度 
	scanf("%d",&N);
	while(N--)
	{
		ans=0;
		//"0"作为输入结束标志 
		while(scanf("%s",temp)!=EOF)
		{
			if(strcmp(temp,"0")==0)
				break;
			strcpy(num[ans++],temp);
		}
		//第一个输入的大整数为"0"
		if(ans==0)
			printf("0\n");
		else
		{
			//确定最长的大整数长度 
			longest=strlen(num[0]);
			for(i=1;i<ans;i++)
			{
				if(strlen(num[i])>longest)
					longest=strlen(num[i]);
			}
			//先逆序,再高位补"0" 
			for(i=0;i<ans;i++)
			{
				Reverse(num[i]);
				if(strlen(num[i])<longest)
				{
					for(j=strlen(num[i]);j<longest;j++)
						num[i][j]='0';
					num[i][j]='\0';
				}
			}
			//逐位相加,并计算数位及进位 
			jw=retlen=0;
			for(j=0;j<longest;j++)
			{
				sum=0;
				for(i=0;i<ans;i++)
					sum+=(num[i][j]-'0');
				ret[retlen++]=((sum+jw)%10)+'0';
				jw=(sum+jw)/10;
			}
			//若当前最高位仍有进位,则进位作为最高位 
			while(jw>0)
			{
				ret[retlen++]=(jw%10)+'0';
				jw/=10;
			}
			ret[retlen]='\0';
			//逆序后的串ret即为最终结果 
			Reverse(ret);
			printf("%s\n",ret);
		}
		//两组输入间换一行 
		if(N>0)
			printf("\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值