回 帖 发 新 帖 刷新版面

主题:浙江大学ACM 1874

这是我的程序,但是没通过,不知哪错了,
各位帮我找找
#include<iostream>
using namespace std;
int main()
{
    long a,b;
    long n[10],m[10],i,j,carry,sum,r;
    while(cin>>a>>b)
    {
        sum=0;
        if(a==0&&b==0)
            break;
        i=0;
        while(a!=0)
        {
            m[i]=a%10;
            a=a/10;
            i++;
        }
        a=i-1;
        j=0;
        while(b!=0)
        {
            n[j]=b%10;
            b=b/10;
            j++;
        }
        b=j-1;
        carry=0;
        for(i=a,j=b;i>-1||j>-1;i--,j--)
        {
            if(i>=0&&j>=0)
            {
            r=m[i]+n[j]+carry;
            if(carry=r/10)
                sum++;
            }
            else if(i>=0&&j<0)
            {
                r=m[i]+carry;
                if(carry=r/10)
                    sum++;
            }
            else
            {
                r=n[j]+carry;
                if(carry=r/10)
                    sum++;
            }
        }
        if(sum==0)
            cout<<"No carry operation."<<endl;
        else if(sum==1)
        cout<<sum<<" carry operation."<<endl;
        else
            cout<<sum<<" carry operations."<<endl;
    }
    return 1;
}










        

回复列表 (共7个回复)

沙发

题我没仔细看,但是
  if(carry=r/10)
好像应该是
  if(carry==r/10)

板凳

我刚通过了,要上课去了, 回来再帮你看一下了

3 楼

for(i=a,j=b;i>-1||j>-1;i--,j--)
        {
            if(i>=0&&j>=0)
            {
            r=m[i]+n[j]+carry;
            if(carry=r/10)
                sum++;
            }
            else if(i>=0&&j<0)
            {
                r=m[i]+carry;
                if(carry=r/10)
                    sum++;
            }
            else
            {
                r=n[j]+carry;
                if(carry=r/10)
                    sum++;
            }
        }
改一下
就会通过的

4 楼

请说明程序功能!

5 楼

你的程序错在细节,如9731 269,准确答案应该是4 carry operations,而不是3 carry operations.

6 楼

C不会.......

7 楼

[em1]我回去看看这些代码啊,我的好象和你的不一样的[em16]

我来回复

您尚未登录,请登录后再回复。点此登录或注册