A+B Problem(高精)(洛谷-P1601)
生活随笔
收集整理的這篇文章主要介紹了
A+B Problem(高精)(洛谷-P1601)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目描述
高精度加法,x相當(dāng)于a+b problem,不用考慮負(fù)數(shù)。
輸入輸出格式
輸入格式:
分兩行輸入a,b<=10^500
輸出格式:
輸出只有一行,代表A+B的值
輸入輸出樣例
輸入樣例#1:
1
1
輸出樣例#1:
2
源代碼
#include<iostream> #include<cstring> #include<string> using namespace std; int main() {string str1,str2;int a[100],b[100],c[100];int len1,len2;int i;cin>>str1;cin>>str2;len1=str1.length();len2=str2.length();/*補(bǔ)0工作,保證兩個(gè)字符串相同*/if(len1<len2)for(i=1;i<=len2-len1;i++)str1="0"+str1;elsefor(i=1;i<=len1-len2;i++)str2="0"+str2;len1=str1.length();len2=str2.length();for(i=0;i<=len1-1;i++)//加數(shù)str1存入數(shù)組aa[len1-i]=str1[i]-'0';for(i=0;i<=len2-1;i++)//加數(shù)str2存入數(shù)組bb[len2-i]=str2[i]-'0';int x=0;int lenc=1;while( (lenc<=len1) || (lenc<=len2) ){c[lenc]=a[lenc]+b[lenc]+x;//第i位相加并加上次的進(jìn)位x=c[lenc]/10;//向高位進(jìn)位c[lenc]%=10;//存儲(chǔ)第i位的值lenc++;//位置下標(biāo)}/*處理最高進(jìn)位*/c[lenc]=x;if(c[lenc]==0)lenc--;for(i=lenc;i>0;i--)//倒序輸出cout<<c[i];cout<<endl;return 0; }總結(jié)
以上是生活随笔為你收集整理的A+B Problem(高精)(洛谷-P1601)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 理论基础 —— 排序 —— 基数排序
- 下一篇: 暑期训练日志----2018.8.11