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

注意前导零,可用C++中的string类型,要用G++交 附代码

Posted by ACAccepted at 2018-09-23 13:18:02 on Problem 1503
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[100005],b[100005],len,m;
string read;

int Max(int a,int b)
{
	if(a>b)return a;
	return b;
}

void gjadd()
{
	int mx=Max(len,m),c=0;
	m=mx;
	for(int i=1;i<=mx;i++)
	{
		a[i]=a[i]+b[i]+c;
		c=a[i]/10;
		a[i]%=10;
	}
	if(c>0)a[++m]=c;
}

int main()
{
	while(true)
	{
		memset(b,0,sizeof(b));
		cin>>read;
		if(read=="0")break;//string 判断是否read中只有'0'
		len=read.length();
		for(int i=1;i<=len;i++)b[i]=read[len-i]-48;
		gjadd();
	}
	for(int i=m;i>=1;i--)printf("%d",a[i]);
	printf("\n");
	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