HDUOJ 1047(多个大数求和)

HDUOJ 1047多个大数求和(这题有点坑)

Integer Inquiry

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


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 ./*这句话一定要理解,意思是每组输入的最后一个数据为0,0即是加数也是结束该组数据输入结束的标志*/
                                                                                                               /*否则在做题的时候会忽略输出0的情况,最后总是a不过*/
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
  
  
1
 
 
123456789012345678901234567890 123456789012345678901234567890 123456789012345678901234567890 0
 
Sample Output
  
  
370370367037037036703703703670
 
My solution:
 
/*2015.7.24*/
 
#include<stdio.h>
#include<string.h>
int main()
{
	int i=0,j,j1,n,t2,k,t,t1=-1,m;
	int c1[110][110];
	char c[110][110];  /*用二维数组存放每组输入的多个字符串*/
         scanf("%d",&n);
         getchar();
	for(m=0;m<n;m++)	
	{
		
		while(scanf("%s",c[i++])!=EOF)   
		{
			i-=1;  /*由于执行了i++后,i自动加1,所以要给i减1,才能对刚输入的数据进行处理*/
			getchar();
			if(strcmp(c[i],"0")==0)
		    	break;
		//	if(strlen(c[i])==1)         /*这两种方法都可以用来判断输入的字符是不是0,若是结束输入,否则继续输入*/
		//	{
		//		if(c[i][0]=='0')/*这种方法先判断输入的字符串是不是一个字符,再判断是不是0,因为输入1也是有可能的,*/
		//		break;    /*不过刚开始我没有想到这种情况,认为输入的数据除0以外都是大数(样例容易给人造成这种错误理解)*/
		//	}                    /*所以只判断了字符串中的字符个数是不是1,若是,就默认为0,事实证明这种理解是错误的。*/ 
			i+=1;   /*加1即用下一个空的数组元素来装新的数据*/ 	
		}
		for(t=0;t<=i;t++)/*转化和求和*/
		{   
		   t1+=1;/*这个变量用来,控制大数求和,当转化第一个数据时或该组数据只输入了一个数据(0)时,不进行后面的求和运算(否则会出错)*/
		   memset(c1[t],0,sizeof(c1[t]));/*该函数功能把所有元素都赋值0*/	
		   t2=strlen(c[t]);	
		   k=0;	
	 	   for(j=t2-1;j>=0;j--)
	    	     c1[t][k++]=c[t][j]-'0';
		   if(t1>0)
		     {	
	    	        for(j=0;j<j1+2||j<t2+2;j++)/*+2是考虑最高位可能需要进位,所以多预留一位*/
		         {
			    c1[0][j]+=c1[t][j];
			    if(c1[0][j]>=10)   /*判断进位*/
			     {
				    c1[0][j]-=10;
				    c1[0][j+1]+=1;
		              }
	     	         }
			 j1=j;/*求和后数的总长度*/	
	    	      }
			else
			j1=t2;/*当转化第一个数据时,把j1=t2,从而为下次求和提供限制条件*/ 
	        }
		for(;j>=0&&c1[0][j]==0;j--);/*去除整数前的无效数字000,如0001233*/
	        if(j>=0)
		      for(;j>=0;j--)
		         printf("%d",c1[0][j]);
		else       /*做题时一直没有考虑输出结果为0的情况,也就是当只输入0时的情况,结果调试了一整天就是通不过*/
	  	  printf("0");
		printf("\n");
		i=0; /*刚开始没有注意到这两个变量,在输入数据进行测试时,结果总是出错,只有输入的第一组数据能得出正确结果,第二组开始*/
		t1=-1; /*结果总是出错。刚开始只输入一组测试数据,结果对了,就提交,可是总 wrong answer  */
		         <span style="font-family: 'Courier New', Courier, monospace; font-size: 12px;"> /*每循环一次,就要将其赋为初值*/</span>
                if(m!=n-1)  
		printf("\n");/*注意输出格式*/
       }
  return 0;		
}

 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值