Posted on 2010-08-17 14:21
Brian 閱讀(349)
評論(0) 編輯 收藏 引用 所屬分類:
POJ
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a[201]={0},b[200]={0},i,j,len,ls1,ls2,f=0; // 相加后結(jié)果放在a內(nèi)
string s;
for (cin>>s,ls1=s.length(),i=ls1-1,j=0; i>=0; i--)
a[j++]=s[i]-'0'; //將第一個數(shù)逆序放入a數(shù)組
for (cin>>s,ls2=s.length(),i=ls2-1,j=0; i>=0; i--)
b[j++]=s[i]-'0'; //將第二個數(shù)逆序放入b數(shù)組
for (i=0,len=ls1>ls2?ls1:ls2; i<len; i++) // 注意len取二者較大的值
{
a[i] += b[i]; //相加結(jié)果放入a數(shù)組
if (a[i] >= 10)
{
a[i] %= 10; //進(jìn)位處理
a[i+1]++;
}
}
if (a[len]) printf("%d",a[len]); //所謂的前導(dǎo)0
for (i=len-1; i>=0; i--)
printf("%d",a[i]); //輸出主體部分
return 0;
}