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

被前导0坑死,注意,输入输出都有前导0

Posted by tw_luo at 2019-07-10 00:51:31 on Problem 1503
#include<iostream>
#include<string>
using namespace std;
struct BigInteger{
    int data[210];
    int length;
};
void Init(BigInteger &a){
    for(int i=0;i<210;i++){
        a.data[i]=0;
    }
    a.length=0;
}
BigInteger Add(BigInteger a,BigInteger b){
    BigInteger c;
    Init(c);
    int l=max(a.length,b.length);
    c.length=l;
    for(int i=0;i<l-1;i++){
        c.data[i]+=(a.data[i]+b.data[i]);
        if(c.data[i]>=10){
            c.data[i+1]++;
            c.data[i]%=10;
        }
    }
    c.data[l-1]+=(a.data[l-1]+b.data[l-1]);
    if(c.data[l-1]>=10){
        c.length++;
        c.data[l]++;
        c.data[l-1]%=10;
    }
    return c;
}
void Disp(BigInteger a){
    int flag=0;
    for(int i=a.length-1;i>=0;i--){
        cout<<a.data[i];
    }
    cout<<endl;
}
int main(){
    ios::sync_with_stdio(false);
    BigInteger a[200];
    BigInteger ans;
    for(int i=0;i<200;i++){
        Init(a[i]);
    }
    Init(ans);
    string str;
    int i=0;
    while(cin>>str){
        if(str=="0") break;
        int l=str.size();
        for(int k=0;k<l;k++){
            a[i].data[k]=str[l-1-k]-'0';
        }
        a[i].length=l;
        ans=Add(ans,a[i]);
        //Disp(ans);
        i++;
    }
    Disp(ans);
    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