问题 K: 【高精度】采购员的烦恼
时间限制: 1 Sec 内存限制: 64 MB提交: 4 解决: 4
[提交] [状态] [讨论版] [命题人:外部导入]
题目描述
电器厂最近赶制一批电器,需要大量采购原材料,各个车间均根据生产需要向采购组递交需求清单。由于数量宠大,采购员无法准确算出购买所有原材料的总金额。请你编写一个程序,帮助采购员算出购买所有原材料的总金额。
输入
第1行是原材料种数n(1≤n≤10000),接下来有n×2行,每两行分别代表某材料的数量sl(1≤sl≤10^100)及单价x(1≤x≤1000),即文件第2行是第一种原材料的数量,第3行是第一种原材料的单价,依次类推。
输出
购买所有原材料的总金额。
样例输入
2
1234567735456
256
48746465463
986
样例输出
364113355223254
高精度的加法运算与高精度乘以单精度的结合
1 #include <iostream> 2 #include<string> 3 #include<algorithm> 4 using namespace std; 5 string fun_plus(string s,string t) 6 { 7 string ans; 8 int a[1000]={0},b[1000]={0}; 9 int len1=s.size(),len2=t.size(); 10 int len=max(len1,len2); 11 for(int i=len1-1;i>=0;i--) a[len1-i-1]=s[i]-'0'; 12 for(int i=len2-1;i>=0;i--) b[len2-i-1]=t[i]-'0'; 13 for(int i=0;i<len;i++) 14 { 15 a[i]+=b[i]; 16 a[i+1]+=a[i]/10; 17 a[i]%=10; 18 } 19 if(a[len]) len++; 20 for(int i=len-1;i>=0;i--) 21 ans+=a[i]+'0'; 22 return ans; 23 } 24 string fun_multi(string s,int n) 25 { 26 string ans; 27 int len=s.size(),a[200]={0}; 28 for(int i=len-1;i>=0;i--) a[len-i-1]=s[i]-'0'; 29 int w=0; 30 for(int i=0;i<len;i++) 31 { 32 a[i]=a[i]*n+w; 33 w=a[i]/10; 34 a[i]%=10; 35 } 36 while(w) a[len++]=w%10,w/=10; 37 for(int i=len-1;i>=0;i--) 38 ans+=a[i]+'0'; 39 return ans; 40 } 41 int n,x; 42 string s; 43 int main() 44 { 45 cin>>n; 46 string ans,temp; 47 while(n--) 48 { 49 cin>>s>>x; 50 temp=fun_multi(s,x); 51 ans=fun_plus(temp,ans); 52 } 53 cout<<ans<<endl; 54 return 0; 55 }