#615: 請問這樣寫錯哪裡`請各位幫忙一下


bird (GG)

學校 : 长郡中学
編號 : 2681
來源 : [111.255.206.95]
最後登入時間 :
2010-10-23 20:18:46
c014. 10035 - Primary Arithmetic -- UVa10035 | From: [140.117.182.115] | 發表日期 : 2008-09-28 09:06

#include<iostream>
using namespace std;
int input(char a[])//回傳共有幾位數
{
    char s[100];
    for(int i=0;i<99;i++)
    s[i]='0';
    cin>>s;
    for(int i=0;i<strlen(s);i++)
    a[i]=s[strlen(s)-1-i];
    return strlen(s);
}
int main()
{
    char a[100],b[100];
    int c[100]={0};
    int c1,c2,c3;
    while(1)
    {
        c1=input(a);
        c2=input(b);
        if(a[0]=='0'&&b[0]=='0')break;
        int carry=0,count=0;
          if(c1>c2)
        {
            for(int i=c2;i<c1;i++)
            b[i]='0';
            c3=c1;
        }
        else if(c2>c1)
        {
            for(int i=c1;i<c2;i++)
            a[i]='0';
            c3=c2;
        }
        else
        c3=c1;
        for(int i=0;i<c3;i++)
        {
            c[i]=(a[i]-'0')+(b[i]-'0')+carry;
            if(c[i]>=10){
            carry=c[i]/10;
            c[i]%=10;
            count++;}
        }
        if(count==0)
        cout<<"No carry operation."<<endl;
        else if(count==1)
        cout<<"1 carry operation."<<endl;
        else
        cout<<count<<" carry operations."<<endl;

    }
    
#620: Re:請問這樣寫錯哪裡`請各位幫忙一下


m80126colin (許胖)

學校 : 新北市立板橋高級中學
編號 : 2331
來源 : [35.203.227.81]
最後登入時間 :
2020-12-25 10:47:06
c014. 10035 - Primary Arithmetic -- UVa10035 | From: [220.135.169.23] | 發表日期 : 2008-09-28 15:11

#include<iostream>using namespace std;int input(char a[])//回傳共有幾位數{    char s[100];    for(int i=0;i<99;i++)    s[i]='0';    cin>>s;    for(int i=0;i<strlen(s);i++)    a[i]=s[strlen(s)-1-i];    return strlen(s);}int main(){    char a[100],b[100];    int c[100]={0};    int c1,c2,c3;    while(1)    {        c1=input(a);        c2=input(b);        if(a[0]=='0'&&b[0]=='0')break;        int carry=0,count=0;          if(c1>c2)        {            for(int i=c2;i<c1;i++)            b[i]='0';            c3=c1;        }        else if(c2>c1)        {            for(int i=c1;i<c2;i++)            a[i]='0';            c3=c2;        }        else        c3=c1;        for(int i=0;i<c3;i++)        {            c[i]=(a[i]-'0')+(b[i]-'0')+carry;            if(c[i]>=10){            carry=c[i]/10;            c[i]%=10;            count++;}        }        if(count==0)        cout<<"No carry operation."<<endl;        else if(count==1)        cout<<"1 carry operation."<<endl;        else        cout<<count<<" carry operations."<<endl;    }    }

 

你有試過999+1嗎

 
ZeroJudge Forum