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

求大神帮忙!!!为什测试数据过了,但是有些数据(比如5,6,7)结果有问题

Posted by 1004111222 at 2012-08-08 13:30:25 on Problem 3982
求神帮助,谢谢~~~~~
思路:使用数组模拟加法,有详细注释。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#define MAXN 200
using namespace std;
char a[MAXN],b[MAXN],c[MAXN];   //存放三个需要相加的因子
char s[MAXN];  //存放三个数相加的和

void func()
{
    int i,j,m;
    //循环结束时sum 就是A99的结果了。
    for(i=3; i<100; i++)
    {
        m = 0;
        //模拟数组相加
        for(j=0; j<MAXN; j++)
        {
            m = ((a[j]-'0') + (b[j]-'0') + (c[j]-'0') + m);
            s[j] = (m%10+'0');
            m /= 10;
        }
        //把b赋给c,把a赋值给b,把sum 赋值给a,准备下一轮的递归加法
        strcpy(c,b);
        strcpy(b,a);
        strcpy(a,s);
    }
    //逆序找到s中第一个不为‘0’的字符、
    for(i=MAXN-1; i>0; i--)
        if(s[i] != '0')
        {
            j = i;
            break;
        }
    //输出其余的字符,即结果
    for(i=j; i>=0; i--)
        printf("%c",s[i]);
    printf("\n");
}

int main()
{
    char a0[MAXN],a1[MAXN],a2[MAXN];
    int i,j;
    while(scanf("%s%s%s",a0,a1,a2) != EOF)
    {
        //如果三个0,那么输出0
        if(a0[0] == '0' && a1[0] == '0' && a2[0] == '0')
        {
            cout << '0' << endl;
            continue;
        }
        //对所有的数组"归零"
        memset(a,'0',sizeof(a));
        memset(b,'0',sizeof(b));
        memset(c,'0',sizeof(c));
        memset(s,'0',sizeof(s));
        //把a0,a1,a2,分别存放在a,b,c中,但是要逆序存放,
        //这样在使用数组模拟相加的时候变得简单
        int len0 = strlen(a0);
        int len1 = strlen(a1);
        int len2 = strlen(a2);
        for(j=0,i=len0-1;i>=0; i--)    a[j++] = a0[i];
        for(j=0,i=len1-1;i>=0; i--)    b[j++] = a1[i];
        for(j=0,i=len2-1;i>=0; i--)    c[j++] = a2[i];

        func();
    }
    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