http://acm.hdu.edu.cn/showproblem.php?pid=1001

Sum Problem

Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 104540    Accepted Submission(s): 23648


Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).

In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
 


 

Input
The input will consist of a series of integers n, one integer per line.
 


 

Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
 


 

Sample Input
1 100
 


 

Sample Output
1 5050
 
对每位数逐个相加,
memset(a,ch,int t)对字符串前t位全赋值为ch
#include<stdio.h>
#include<string.h>
#define MAX_LEN 10001
int an1[MAX_LEN+10];
int an2[MAX_LEN+10];
char szLine1[MAX_LEN+10];
char szLine2[MAX_LEN+10];
int main()
{
	int T,i,j,k,nLen1,nLen2,t=1;
	scanf("%d",&T);
	while(T--)
	{
		memset(an1,0,sizeof(an1));
		memset(an2,0,sizeof(an2));
		scanf("%s",szLine1);
		scanf("%s",szLine2);
		nLen1=strlen(szLine1);
		j=0;
		for(i=nLen1-1;i>=0;i--)
			an1[j++]=szLine1[i]-48;
		nLen2=strlen(szLine2);
		j=0;
		for(i=nLen2-1;i>=0;i--)
			an2[j++]=szLine2[i]-48;
		for(i=0;i<MAX_LEN;i++)
		{
			an1[i]+=an2[i];
			if(an1[i]>=10)
			{
				an1[i]-=10;
				an1[i+1]++;
			}
		}
		printf("Case %d:\n%s + %s = ",t,szLine1,szLine2);
		k=0;
		for(i=MAX_LEN;i>=0;i--)
		{
			if(k)
				printf("%d",an1[i]);
			else if(an1[i])
			{
				printf("%d",an1[i]);
				k=1;
			}
		}
		t++;
		if(T>0)
			printf("\n\n");
		else
			printf("\n");
	}
	return 0;
}
posted on 2011-04-24 17:46  pcoda  阅读(893)  评论(0编辑  收藏  举报