python解法
大整数加法
题目描述
Time Limit: 1000 ms
Memory Limit: 256 mb
写程序求两个相同位数的大整数之和
输入描述:
多组数据输入
两个大整数(位数不超过1000)
输出描述:
两个大整数的和
代码
while True:
try:
a,b = map(int,input().split())
c = a+b
print(c)
except:
break
C++解法
代码
#include <stdio.h>
#include <string>
//#include <map>
#include <algorithm>
#include <iostream>
//#include <numeric>
//#include <bits/stdc++.h>
using namespace std;
string Add(string a, string b) {
int carry = 0;
int al = a.length(), bl = b.length();
if (al < bl)
a.swap( b);
string result(al, 0);
b.insert(0, al - bl, '0');
for (int i = al - 1; i >= 0; i--) {
int sum = (a[i]-48) + (b[i] -48) + carry;
carry = sum / 10;
result[i] = sum % 10 + 48;
}
if(carry!=0){
result.insert(result.begin(),carry+48);
}
return result;
}
int main() {
string a;
string b;
while (cin>>a>>b) {
string c = Add(a,b);
printf("%s\n",c.c_str());
//cout<<Add(a,b)<<endl;
}
return 0;
}
总结
注意scanf和printf的输入输出string的用法
大整数乘法
题目描述
Time Limit: 1000 ms
Memory Limit: 256 mb
在一些应用中,特别是现在的密码学中,常常需要用超过100位的整数来做乘法,以此来对数据加密。 现在有两个小于等于100位的大整数a和b(位数相同),请写程序计算出这两个大整数乘积的结果。
输入描述:
输入有三行
第一行是大整数位数
第一个大整数
第二个大整数
输出描述:
两个大整数的结果
代码