IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    poj 2572

    崔佳星发表于 2010-08-19 08:26:00
    love 0

    大整数的加法。与以前高精度算法不同的是这次要一下子读入一个算式。然后简便的是,这次不用再交换顺序了,直接加。但是结果的判断稍微麻烦一点,要把开头的0都去掉才行。一下是我的代码。哎,刚开始把自己弄得挺混乱的,后来愤怒了,就把所有代码全删掉重新写了一遍。然后一次AC
    #include
    #include
    #include
    using namespace std;
    char a[12],b[12],c[12];
    char temp[25];
    bool add(char *first,char *second)
    {
    int len1=strlen(first);
    int len2=strlen(second);
    first[len1]='0';
    int i,j;
    for(i=0;i
    {
    second[i]-='0';
    }
    for(j=0;j<=len1;j++)
    {
    first[j]-='0';
    }
    for(i=0;i

    {
    first[i]+=second[i];
    }
    for(i=0;i
    {
    if(first[i]>=10)
    {
    first[i+1]+=first[i]/10;
    first[i]%=10;
    }
    }
    for(i=0;i<=len1;i++)
    first[i]+='0';
    while(first[len1]=='0')
    len1--;
    int len3=strlen(c);
    len3--;
    while(c[len3]=='0')
    len3--;
    if(len1!=len3)
    return false;
    while(len3>=0)
    {
    if(c[len3]!=first[len3])
    return false;
    len3--;
    }
    return true;
    }
    int main()
    {
    while(gets(temp))
    {
    int kk=0;
    while(temp[kk]!='+')
    {
    a[kk]=temp[kk];
    kk++;
    }
    a[kk]='\0';
    kk++;
    int kkk=0;
    while(temp[kk]!='=')
    {
    b[kkk]=temp[kk];
    kk++;kkk++;
    }
    b[kkk]='\0';
    kk++;
    kkk=0;
    while(temp[kk]!='\0')
    {
    c[kkk]=temp[kk];
    kk++;kkk++;
    }
    c[kkk]='\0';

    int len1=strlen(a);
    int len2=strlen(b);
    if(len1==1&&len2;==1&&a;[0]=='0'&&b;[0]=='0')
    {
    cout<<"True"<
    break;
    }
    if(len1>len2)
    if(add(a,b))
    cout<<"True"<

    else
    cout<<"False"<

    else
    if(add(b,a))
    cout<<"True"<

    else
    cout<<"False"<

    }
    return 0;

    }



    崔佳星 2010-08-19 16:26 发表评论


沪ICP备19023445号-2号
友情链接