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

求解释,本程序经过测试没有问题的,但是提示WA,求大牛帮解释

Posted by denganliang at 2011-03-22 15:01:53 on Problem 1503
#include <stdio.h>
#include <string.h>
char str[150][110],a[150][2],result[200];
void change(int n){
	int i;
	char temp;	
	for(i=0;i<strlen(str[n])/2;i++)
	  {
		  temp=str[n][i];
		  str[n][i]=str[n][strlen(str[n])-1-i];
		  str[n][strlen(str[n])-1-i]=temp;
	}	
}

int main()
{
	int i=0,n=0,max=0,j;
	memset(str,'\0',16500);
	for(i=0;i<150;i++)
	  a[i][0]=a[i][1]=0;
	for(i=0;i<200;i++)
	   result[i]=0;
	scanf("%s",str[n++]);
	while(str[n-1][0]!='0'||str[n-1][1]!='\0')
	{
		scanf("%s",str[n++]);
		}
	n--;
	for(i=0;i<n;i++){
        change(i);//将输入的大数按照相反的方向保存,让小的在左边,大的在右边
        if(max<strlen(str[i]))
           max=strlen(str[i]);
        }
    //进行一次计算,算出每一个数的第一位,第二位,第三位,保存在100×3的字符串数组中
    for(i=0;i<max;i++)
    {
		a[i][0]=a[i][1]=a[i][2]=0;
		for(j=0;j<n;j++)
		  {
			  if(str[j][i]=='\0')
			    continue;
			  a[i][0]+=str[j][i]-'0';
              a[i][1]+=a[i][0]/10;
              a[i][0]=a[i][0]%10;
              if(a[i][1]>=10) 
              a[i+1][0]+=a[i][1]/10;
              a[i][1]=a[i][1]%10;
			  }
		}
    //进行第二次计算结果,保存到110位的字符型数组中,还是倒序,等待输出的时候正序的输出
    result[0]=a[0][0];
    result[1]=a[1][0]+a[0][1];
    if(result[1]>=10)
    {
		result[2]+=result[1]/10;
		result[1]=result[1]%10;
		}
	for(i=2;i<max+2;i++){
		result[i]+=a[i][0]+a[i-1][1];
		if(result[i]>=10)
		{
			result[i+1]+=result[i]/10;
			result[i]=result[i]%10;
			}
		}
		 if(result[max+1]!=0) {
			 i=max+1;
			 if(result[max+1]>=10){
             result[max+2]=result[max+1]/10;
             result[max+1]=result[max+1]%10;
			 i=max+2;
			 }
			 }
		 else if(result[max]!=0){
			 i=max;
			 if(result[max]>=10) {
				 result[max+1]=result[max]/10;
				 result[max]=result[max]%10;
				 i=max;
				 }
			 }
		 else i=max-1;
      for(;i>-1;i--)
        printf("%c",result[i]+'0');
      printf("\n");
 
 return 0;
 }

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