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

稳妥AC,输入数据前导有0,还是用strcmp吧

Posted by 201592116 at 2018-06-22 15:46:56 on Problem 1503
#include<stdio.h>
#include<string.h>
const int max=101;
struct bigInteger{
	int digit[max];
	int size;
	void init(){
		for(int i=0;i<max;i++){
			digit[i]=0;
		}
		size=0;
	}
	//trans char to bigInteger 
	void set(char str[]){
		init();
		int len=strlen(str);
		//j为了统计个数,t临时,c为了进位 
		int j=0,t=0,c=1;
		for(int i=len-1;i>=0;i--){
			t+=(str[i]-'0')*c;
			j++;
			c*=10;
			if(j==4||i==0){
				digit[size++]=t;
				j=0;
				t=0;
				c=1;
			}
		}
	}
	void output(){
		for(int i=size-1;i>=0;i--){
			if(i!=size-1){
				printf("%04d",digit[i]);
			}else{
				printf("%d",digit[i]);
			}
		}
		printf("\n");
	}
	bigInteger operator + (const bigInteger &A) const{
		int carry=0;
		bigInteger res;
		res.init();
		for(int i=0;i<A.size||i<size;i++){
			int tmp=A.digit[i]+digit[i]+carry;
			carry=tmp/10000;
			tmp%=10000;
			res.digit[res.size++]=tmp;
		}
		if(carry!=0){
			res.digit[res.size++]=carry;
		}
		return res;
	}
}a,b,c;
char str[max];
int main() {
	a.set("0");
	while(scanf("%s",str)!=EOF&&strcmp(str,"0")!=0){
		b.set(str);
		a=a+b;
	}
	a.output();
	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