Home | Problems | Discuss | Login

  

求指教 网上说的特殊数据我都试过能过啊 但是为什么不行呢

1002: A + B Problem (Big integer version)

wjllz | 2017-04-22 00:18:17 | delete | edit
#include <iostream>
#include <cstring>
using namespace std;
#define maxn 1000+10
int main()
{
	int a[maxn];
	int b[maxn];
	char c[maxn];
	char d[maxn];
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	while(cin>>c>>d)
	{
		
		if(c[0]=='0'&&d[0]=='0')
		{
			cout<<"0"<<endl;
			continue;
		}
		
		int len1=strlen(c);
		int len2=strlen(d);
		//字符串长度的较大值 
		int len3=max(len1,len2);
		if(len3>1000)
		{
			cout<<"0"<<endl;
			continue;
		}
		int r=0,x=0;
	    for(int i=len1-1;i>=00;i--)
		a[r++]=c[i]-'0';
		for(int i=len2-1;i>=0;i--)
	    {
	    	b[x++]=d[i]-'0';
		}
	    for(int i=0;i<len3;i++)
	    {
	    	a[i]=a[i]+b[i];
	    	 
	    	if(a[i]>9)
	    	{
	    		a[i]-=10;
	    		a[i+1]++;
			}
		}
	    int i=len3+1;
	    while(i--)
	    {
	    	if(a[i]!=0) break;
		}
		while(i>=0)
		{
			cout<<a[i];
			i--;
		}
		cout<<endl;
	}
	return 0;
} 

Please login to reply.