输入包括两个数a和b,其中a和b的位数不超过1000位。
可能有多组测试数据,对于每组数据, 输出a+b的值。
2 6 10000000000000000000 10000000000000000000000000000000
8 10000000000010000000000000000000
//感觉这道题意思不清楚,也没有限制a,b的正负或者整数。
//所以我抱着侥幸的心理默认是两个极大的正整数。
//常规方法解出来的,中途有个bug找了很久。
#include<cstdio>
#include<string.h>
const int maxn=1200;
struct Temp{
char a[maxn];
int len;
}str[2];
int m[maxn],n[maxn], sum[maxn];
int main()
{
while(scanf("%s %s",str[0].a,str[1].a)!=EOF){
str[0].len=strlen(str[0].a);
str[1].len=strlen(str[1].a);
memset(m,0,sizeof(m));memset(n,0,sizeof(n));memset(sum,0,sizeof(sum));
int k=0,h=0;
for(int i=str[0].len-1;i>=0;i--){
m[k++]=str[0].a[i]-'0';
}
for(int j=str[1].len-1;j>=0;j--){
n[h++]=str[1].a[j]-'0';
}
int t=0,q=0,p=0;
while(p<k || q<h ){
sum[t+1]=(sum[t]+m[p]+n[q])/10;
sum[t]=(sum[t]+m[p]+n[q])%10;
t++;p++;q++;
}
while(sum[t]==0){
t--;
}
for(int i=t;i>=0;i--){
printf("%d",sum[i]);
sum[i]=0;
}
printf("\n");
}
return 0;
}
#include<stdio.h>
#include<string.h>
struct bigInteger{
int digit[1000];
int size;
void init(){
for(int i=0;i<1000;i++)
digit[i]=0;
size=0;
}
void set(char str[]){
init();
int L=strlen(str);
for(int i=L-1,j=0,t=0,c=1;i>=0;i--){
t+=(str[i]-'0')*c;
j++;
c*=10;
if(j==4||i==0){
digit[size++]=t;
j=0;
t=0;
c=1;
}
}
}
void output(){
for(int i=size-1;i>=0;i--){
if(i!=size-1)printf("%04d",digit[i]);
else printf("%d",digit[i]);
}
printf("\n");
}
bigInteger operator + (const bigInteger &A) const{
bigInteger ret;
ret.init();
int carry=0;
for(int i=0;i<A.size||i<size;i++){
int tmp=A.digit[i]+digit[i]+carry;
carry=tmp/10000;
tmp%=10000;
ret.digit[ret.size++]=tmp;
}
if(carry!=0){
ret.digit[ret.size++]=carry;
}
return ret;
}
}a,b,c;
char str1[1002],str2[1002];
int main(){
while(scanf("%s%s",str1,str2)!=EOF){
a.set(str1);
b.set(str2);
c=a+b;
c.output();
}
return 0;
}
王道原题
#include<stdio.h>
#include<string.h>
typedef struct {
int d[1010];
int len;
}bign;
bign init(void){
bign a;
memset(a.d,0,sizeof(a.d));
a.len=0;
return a;
}
bign change(char str[]){
bign a;
a=init();
int num=0;
for(int i=0;i<strlen(str);i++){
a.d[a.len++]=str[strlen(str)-i-1]-'0';
}
return a;
}
bign add(bign a,bign b){
bign c;
c=init();
int carry=0;
int temp;
for(int i=0;i<a.len || i<b.len;i++){
temp=a.d[i]+b.d[i]+carry;
c.d[c.len++]=temp%10;
carry=temp/10;
}
if(carry!=0)c.d[c.len++]=carry;
return c;
}
void print(bign a){
for(int i=a.len-1;i>=0;i--){
printf("%d",a.d[i]);
}
}
int main(){
char str1[1010],str2[1010];
bign a,b,c;
while(scanf("%s %s",str1,str2)!=EOF){
a=change(str1);
b=change(str2);
c=add(a,b);
print(c);
}
return 0;
}
纯C
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str1 = in.next();
String str2 = in.next();
System.out.println(bigIntegerAdd(str1, str2));
}
in.close();
}
/**
* bigIntegerAdd
* @return
*/
public static String bigIntegerAdd(String str1, String str2) {
StringBuilder sb1 = new StringBuilder(str1).reverse();
StringBuilder sb2 = new StringBuilder(str2).reverse();
int maxLen = sb1.length() > sb2.length() ? sb1.length() : sb2.length();
int added = 0;
String result = "";
for (int i = 0; i < maxLen; i++) {
if (sb1.length() + 1 < i) {
result = str2.substring(0, str2.length() - i) + result;
break;
} else if (sb2.length() + 1 < i) {
result = str1.substring(0, str1.length() - i) + result;
break;
} else {
int tmp1 = 0, tmp2 = 0;
if (sb1.length() > i) tmp1 = Integer.parseInt(sb1.substring(i, i + 1));
if (sb2.length() > i) tmp2 = Integer.parseInt(sb2.substring(i, i + 1));
int current = tmp1 + tmp2 + added;
if (current / 10 < 1) {
added = 0;
result = current + result;
} else {
added = current / 10;
result = current % 10 + result;
}
}
}
return result;
};
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include<string>
using namespace std;
void add(string a,string b){
int temp=0;
string::iterator i=a.end()-1;
string::iterator j=b.end()-1;
stack<int>c;
while(i>=a.begin() && j>=b.begin()){
c.push(((*i)+temp+(*j)-'0'-'0')%10);
temp=((*i)+temp+(*j)-'0'-'0')/10;
i--;a.pop_back();
j--;b.pop_back();
}
while(!a.empty()){
c.push(((*i)-'0'+temp)%10);
temp=((*i)-'0'+temp)/10;
i--;a.pop_back();
}
while(!b.empty()){
c.push(((*j)-'0'+temp)%10);
temp=((*j)-'0'+temp)/10;
j--;b.pop_back();
}
if(temp==1){
cout<<temp;
}
while(!c.empty()){
cout<<c.top();
c.pop();
}
}
int main(){
string a,b;
while(cin>>a>>b){
add(a,b);
}
return 0;
}
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
int main(void)
{
string a,b;
vector<int> sum;
int c = 0;//进位
while(cin >> a >> b)
{
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
if(a.length() < b.length())//a永远为最长的字符串
swap(a, b);
int i;
for(i = 0;i < b.length();i++)//先整短的字符串
{
int temp = a[i] - '0' + b[i] - '0' + c;//带进位的加法
sum.push_back(temp % 10);//只将个位push
c = temp / 10;//进位更新
}
for(int j = i;j < a.length();j++)
{
int temp = a[j] - '0' + c;
sum.push_back(temp % 10);
c = temp / 10;
}
if(c == 1)//以防最后还有进位
sum.push_back(c);
reverse(sum.begin(),sum.end());
for(vector<int>::iterator it = sum.begin();it != sum.end();it++)
cout << *it;
cout << endl;
}
return 0;
} //可以用java封装好的大整形数来解决
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
BigInteger a = input.nextBigInteger();
BigInteger b = input.nextBigInteger();
System.out.println(a.add(b));
}
}
#include <stdio.h>
#include <string.h>
int convert (char a[],int b[])
{
int len=strlen(a);
int i;
for(i=0;i<len;i++)
{
b[i]=a[len-1-i]-'0';
}
return len;
}
int main()
{
char a[1001],b[1001];
int x[1001],y[1001],ans[1001];
int len_a,len_b,len,carry,i;
while(scanf("%s %s",&a,&b)!=EOF)
{
len_a=convert(a,x);
len_b=convert(b,y);
len=len_a>len_b?len_a:len_b;
for(i=0,carry=0;i<len;i++)
{
ans[i]=(x[i]+y[i]+carry)%10;
carry=(x[i]+y[i]+carry)/10;
}
if(carry!=0) ans[i++]=carry;
while(i>0) printf("%d",ans[--i]);
printf("\n");
}
} #include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
//a+b 大数相加
void Myreverse(char* a){
for(int i = 0; i < strlen(a) / 2 ; i++){
char tmp = a[i];
a[i] = a[strlen(a) - i - 1];
a[strlen(a) - i - 1] = tmp;
}
}
int main(){
char a[1001], b[1001], res[1002];
while(~scanf("%s %s",a,b)){
int len_a = strlen(a), len_b = strlen(b);
//逆转,个位在前
Myreverse(a);
Myreverse(b);
int cnt = 0, carry = 0;
while(cnt < strlen(a) && cnt < strlen(b)){
int tmp = a[cnt] - '0' + b[cnt] - '0' + carry;
if(tmp >= 10){
carry = 1;
tmp = tmp % 10;
}
else
carry = 0;
res[cnt++] = tmp + '0';
}
if(cnt == strlen(a)){
while(cnt < strlen(b)){
res[cnt] = b[cnt] ;
cnt++;
}
}
else{
while(cnt < strlen(a)){
res[cnt] = a[cnt] ;
cnt++;
}
}
res[cnt] = '\0';
Myreverse(res);
for(int i = 0; i < strlen(res); i++)
if(i < strlen(res) - 1)
printf("%c",res[i]);
else
printf("%c\n",res[i]);
}
return 0;
}老哥们,这代码为什么输出一样,但不能通过全部用例呀
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char a[1010], b[1010],c[1010];
int rem;
while (cin>>a>>b)
{
int i = strlen(a) - 1;
int j = strlen(b) - 1;
int k;
i > j ? k = i : k = j;
c[k + 1] = '\0';
int temp = 0;
rem = 0;
while (i != -1 && j != -1)
{
temp = rem + a[i--] - '0' + b[j--] - '0';
c[k--] = temp % 10 + '0';
rem = temp / 10;
}
while (i != -1)
{
temp = rem + a[i--] - '0';
c[k--] = temp % 10 + '0';
rem = temp / 10;
}
while (j != -1)
{
temp = rem + b[j--] - '0';
c[k--] = temp % 10 + '0';
rem = temp / 10;
}
cout << c << endl;
}
return 0;
} 字符串数组模拟,结构有点像归并,时间复杂度o(n)#include<cstdio>
(802)#include<string>
#include<iostream>
(720)#include<stack>
using namespace std;
void add(string p,string q)
{
int m,n,k,s;
stack<int> w;
string::iterator it1,it2,it3;
n=0,it1=p.end(),it2=q.end();
do
{
--it1,--it2;
k=(*it1-'0')+(*it2-'0')+n;
m=k%10,n=k/10;
w.push(m);
}while(it2!=q.begin());
while(it1!=p.begin())
{
--it1;
k=(*it1-'0'+n);
m=k%10,n=k/10;
w.push(m);
}
while(w.empty()!=true)
{
printf("%d",w.top());
w.pop();
}
printf("\n");
return;
}
int main()
{
int m,n;
string p,q;
while(cin>>p)
{
cin>>q;
m=p.size(),n=q.size();
if(m>n) add(p,q);
else add(q,p);
}
return 0;
} #include <vector>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
vector<int> GetNum(string s){
vector<int> res;
reverse(s.begin(),s.end());
for(const auto& x:s){
res.push_back(x-'0');
}
return res;
}
vector<int> AddNum(const vector<int>& a,const vector<int>& b){
vector<int> res;
int c=0;
int max_len=max(a.size(),b.size());
for(int i=0;i<max_len;i++){
if(i<a.size()&&i<b.size()){
res.push_back((a[i]+b[i]+c)%10);
c=(a[i]+b[i]+c)/10;
}
else if(i<a.size()){
res.push_back((a[i]+c)%10);
c=(a[i]+c)/10;
}
else if(i<b.size()){
res.push_back((b[i]+c)%10);
c=(b[i]+c)/10;
}
}
if(c)
res.push_back(c);
return res;
}
int main(){
char buf_1[1005],buf_2[1005];
while(scanf("%s %s",buf_1,buf_2)!=EOF){
auto num_1=GetNum(string(buf_1));
auto num_2=GetNum(string(buf_2));
auto res=AddNum(num_1,num_2);
for(auto i=prev(res.end());i>=res.begin();i--)
printf("%d",*i);
}
return 0;
}
#include<iostream>
using namespace std;
string lengthen(string a,int len)
{
for(int i=0;i<len;i++)
{
a="0"+a;
}
return a;
}
string add(string a,string b)
{
int jin=0;
int answer=0;
for(int i=a.size()-1;i>=0;i--)
{
answer=a[i]-'0'+b[i]-'0'+jin;
jin=0;//加完后这个进位要归0
if(answer>9)
{
answer=answer-10;
jin++;
}
a[i]=answer+'0';
}
if(jin!=0)//如果还有进位的话
a="1"+a;
int pos=0;
while(a[pos]=='0')
pos++;
a=a.substr(pos);
return a;
}
int main()
{
string a,b;
while(cin>>a>>b)
{
int len1=a.length();
int len2=b.length();
if(len1>len2)
b=lengthen(b,len1-len2);
if(len1<len2)
a=lengthen(a,len2-len1);
string answer=add(a,b);
cout<<answer<<endl;
}
return 0;
} //两数相加,位数为加数位数的最大值或者最大值+1
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1,s2;
int a[110]= {0},b[110]= {0};
cin>>s1>>s2;
for(int i=0; i<s1.size(); i++) {
a[i]=s1[s1.size()-i-1]-'0';
}
int len=max(s1.size(),s2.size())+1;
for(int i=0; i<len; i++) {
if(i<s2.size()){
b[i]=s2[s2.size()-i-1]-'0';
}
a[i]+=b[i];
if(a[i]>=10){
a[i+1]++;
a[i]%=10;
}
}
bool flag=true;
for(int i=len-1; i>=0; --i) {
if(flag&&a[i]!=0){
flag=false;
cout<<a[i];
continue;
}
if(!flag) cout<<a[i];
}
return 0;
}
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) System.out.println(new BigInteger(scanner.next()).add(new BigInteger(scanner.next())));
}
}
#include <stdio.h>
#include <string.h>
typedef struct {
int a[1001];
int top;
}Stack;
void push(Stack *s, int c) {
s->a[s->top++] = c;
return;
}
void pop (Stack *s) {
if(s->top == 0)
return;
printf("%d", s->a[--s->top]);
return ;
}
int main() {
char s1[1000], s2[1000];
while(scanf("%s%s", s1, s2) != EOF) {
int i = strlen(s1), j = strlen(s2);
int cf = 0, sum = 0;
Stack s;
while(i != 0 && j != 0) {
int c1 = s1[--i] - '0';
int c2 = s2[--j] - '0';
sum = (c1+c2+cf)%10;
cf = (c1+c2+cf)/10;
push(&s, sum);
}
while(i != 0) {
int c1 = s1[--i] - '0';
sum = (c1+cf)%10;
cf = (c1+cf)/10;
push(&s, sum);
}
while(j != 0) {
int c2 = s2[--j] - '0';
sum = (c2+cf)%10;
cf = (c2+cf)/10;
push(&s, sum);
}
if(cf != 0)
push(&s, cf);
while(s.top != 0)
pop(&s);
printf("\n");
}
return 0;
}
/*
直接利用字符串本身进行计算,先将两个字符串补长成同长度
*/
#include <bits/stdc++.h>
using namespace std;
string add(string a,string b){
if(a.size()>b.size()){
int temp = a.size()-b.size();
while(temp--){
b = "0"+b;
}
}else if(a.size()<b.size()){
int temp = b.size()-a.size();
while(temp--){
a = "0"+a;
}
}
int carry = 0;
for(int i=a.size()-1;i>=0;i--){
int cur = a[i]-'0' + b[i] - '0' + carry;
a[i] = cur%10+'0';
carry = cur/10;
}
while(carry){
a = char(carry%10+'0')+a;
carry/=10;
}
return a;
}
int main(){
string a,b;
while(cin>>a>>b){
cout<<add(a,b)<<endl;
}
return 0;
} #include<iostream>//突然想练手class就用class写了,不习惯同学改成struct也行,思路:大数用string类保存,从最后面开始依次读出4位转化成数字
(1272)#include<iomanip>//然后保存到私有的数据dig[1000]中,一个地址保存4位整数。
using namespace std;
class Big
{
private:
int dig[1000];
int sizel;
public:
Big(){}//构造函数,这里并没有初始化,而用了后面的init函数对类对象进行初始化。到了主函数里再谈
~Big(){}//析构函数
void init()//初始化类对象
{
for (int i = 0; i < 1000; i++)
{
dig[i] = 0;
sizel = 0;
}
}
void get(string str)//从string中4位一保存到数组中去
{
int temp = 0, i = 0,c = 1,len = str.length() - 1;//c位每一位的权重
do
{
temp+=(str[len] - '0')*c;
i++;
c *= 10;
if (i == 4 || len == 0)//四位一保存,或者到最后保存的不一定有4位,只有4位以下都要保存
{
dig[sizel++] = temp;
temp = 0;
i = 0;
c = 1;
}
len--;
} while (len >= 0);
}
void out()
{
for (int i = sizel - 1; i >= 0; i--)//从dig最后一位开始逆序遍历
{
if (i == sizel - 1)//就不用前置0;
cout << dig[i];
else cout << setw(4)<<setfill('0')<<dig[i];//如果不是首位,中间位数是只有个位如8;保存进数组的则是8,但是输出的就是8000或者0008
}
cout << endl;
}
Big operator+(Big& z)重载运算符+号
{
Big s;
s.init();
int carry = 0;//四位数如果相加成5位数 /10000,为向前进1,%10000为剩下的4位数保存下来
for (int i = 0; i < z.sizel||i<sizel; i++)
{
int t = dig[i] + z.dig[i] + carry;
carry =t / 10000;
t %= 10000;
s.dig[s.sizel++] = t;
}
if (carry != 0)
s.dig[sizel++] = carry;
return s;
}
};
int main()
{
Big t1,t2,t3;
string str1,str2;
while (cin >> str1>>str2)
{
t1.init();//这里就是为何不用构造函数初始化类对象,如果用构造函数初始化,没有init函数,则每次输入数据都要重新创建新的类对象,浪费内存空间
t1.get(str1);
t2.init();
t2.get(str2);
t3.init();
t3 = t1 + t2;
t3.out();
}
return 0;
} #include<iostream>
#include<stack>
#include<queue>
#include<string>
using namespace std;
stack<int> num1, num2;
stack<int> result;
int main()
{ // 用string接收数字,然后转化成数字压入到栈中 string str1, str2; cin >> str1 >> str2; for(int i=0; i<str1.length(); i++) num1.push((int)str1.at(i)-48); for(int i=0; i<str2.length(); i++) n num2.push((int)str2.at(i)-48); // 处理 int tempadd = 0;
// 余数要一直更新保存 int tempresult = 0; while(!num1.empty() || !num2.empty()) { int add1 = 0; int add2 = 0; if(!num1.empty()) { add1 = num1.top(); num1.pop(); } if(!num2.empty()) { add2 = num2.top(); num2.pop(); } // 计算一位的结果 tempresult = (add1 + add2 + tempadd) % 10; tempadd = (add1 + add2 + tempadd) / 10; result.push(tempresult); } // 输出结果 while(!result.empty()) { cout << result.top(); result.pop(); } return 0;
}