Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:这道题用字符串做可能会好点

Posted by BJ061053 at 2007-08-15 10:41:40 on Problem 1504
In Reply To:Re:这道题用字符串做可能会好点 Posted by:BJ061053 at 2007-08-15 10:40:06
我是这样做的怎么不对呀!
#include<stdio.h>
#include<string.h>
void main()
{
	int n,i,length;
	char a[10],b[10],sum[11];
	scanf("%d",&n);
	while(n--)
	{
		for(i=0;i<11;i++)
		{
			sum[i]='0';
		}
		scanf("%s%s",a,b);
		length=strlen(a)<strlen(b)?strlen(a):strlen(b);
		int d=0;
		for(i=0;i<length;i++)
		{	
			sum[i]=a[i]+b[i]-'0'+d;
			d=(sum[i]-'0')/10;
			sum[i]=(sum[i]-'0')%10+'0';
		}
		sum[i]+=d;
		if(strlen(a)>strlen(b))
		{
			for(;i<strlen(a);i++)
			{
				sum[i]+=a[i]-'0';
			}
			sum[i]='\0';
		}
		else if(strlen(a)<strlen(b))
		{
			for(;i<strlen(b);i++)
			{
				sum[i]+=b[i]-'0';
			}
			sum[i]='\0';
		}
		else
		{
			sum[++i]='\0';
		}		
		for(i=0;i<strlen(sum);i++)
		{
			if(sum[i]=='0')
			{
				continue;
			}
			else
			{
				printf("%s\n",&sum[i]);
				break;
			}
		}
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator