chen111228 @ 2024-01-01 16:56:43
#include<iostream>
#include<string>
using namespace std;
const int N = 510;
int a[N], b[N], c[N];
int main()
{
string str1;
string str2;
cin >> str1;
cin >> str2;
for (int i = 0; i < str1.size(); i ++)
a[str1.size()-1 - i] = str1[i] - '0';
for (int i = 0; i < str2.size(); i ++)
b[str2.size()-1 - i] = str2[i] - '0';
int ans = max(str1.size(), str2.size());
for (int i = 0; i < ans; i ++){
c[i] += a[i] + b[i];
c[i+1] = c[i] / 10;
c[i] %= 10;
}
ans += 1;
if (c[ans-1] == 0 && ans > 1)
ans -= 1;
for (int i = 0; i < ans; i ++)
cout << c[ans-1-i];
return 0;
}
by yzm0325 @ 2024-01-01 16:58:53
@chen111228 有负数,要处理
by yzm0325 @ 2024-01-01 16:59:12
那题只有正数
by chen111228 @ 2024-01-01 18:43:36
谢谢啦
by chen111228 @ 2024-01-01 19:58:25
过了谢谢啊
using namespace std; char a[1000005]; char b[1000005]; int fa, fb; int la, lb;
void show(int ed) { if (fa) printf("-"); for (int i = ed; i >= fa; --i) printf("%d", a[i]); } void add() { int i, j, t = 0, s, l; for (i = fa; i < la || i < lb; ++i) { s = a[i] + b[i] + t; t = s / 10; a[i] = s % 10; } a[i] = t ; l = t ? i : i - 1; show(l); } int cmp() { int la = strlen(a), lb = strlen(b); if (la - fa > lb - fb) return 1; else if (la - fa < lb - fb) return 0; else { int i ; for ( i = 0; i < la && a[i + fa] == b[i + fb]; ++i); return a[i + fa] > b[i + fb]; } }
void minu() { int i, j, c = 0, l = -1, s; for (i = 0; i + fa < la; ++i) { s = a[i + fa] - b[i + fb] - c >= 0 ? 0 : 1; a[i + fa] = (10 + a[i + fa] - b[i + fb] - c) % 10; l = a[i + fa] ? i + fa : l; c = s; } if (l < 0) printf("0"); else show(l); }
int main() { scanf("%s%s", a, b); fa = ('-' == a[0]), fb = ('-' == b[0]); if ( !cmp()) swap(a, b), swap(fa, fb); la = strlen(a), lb = strlen(b); reverse(a + fa, a + la), reverse(b + fb, b + lb); int i = fa, j = fb; while (i < la) a[i] -= '0', i++; while (j < lb) b[j] -= '0', j++; if (fa ^ fb) minu(); else add(); return 0; }
by chen111228 @ 2024-01-01 19:58:49
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
char a[1000005];
char b[1000005];
int fa, fb;
int la, lb;
void show(int ed) {
if (fa)
printf("-");
for (int i = ed; i >= fa; --i)
printf("%d", a[i]);
}
void add() {
int i, j, t = 0, s, l;
for (i = fa; i < la || i < lb; ++i) {
s = a[i] + b[i] + t;
t = s / 10;
a[i] = s % 10;
}
a[i] = t ;
l = t ? i : i - 1;
show(l);
}
int cmp() {
int la = strlen(a), lb = strlen(b);
if (la - fa > lb - fb)
return 1;
else if (la - fa < lb - fb)
return 0;
else {
int i ;
for ( i = 0; i < la && a[i + fa] == b[i + fb]; ++i);
return a[i + fa] > b[i + fb];
}
}
void minu() {
int i, j, c = 0, l = -1, s;
for (i = 0; i + fa < la; ++i) {
s = a[i + fa] - b[i + fb] - c >= 0 ? 0 : 1;
a[i + fa] = (10 + a[i + fa] - b[i + fb] - c) % 10;
l = a[i + fa] ? i + fa : l;
c = s;
}
if (l < 0)
printf("0");
else
show(l);
}
int main() {
scanf("%s%s", a, b);
fa = ('-' == a[0]), fb = ('-' == b[0]);
if ( !cmp())
swap(a, b), swap(fa, fb);
la = strlen(a), lb = strlen(b);
reverse(a + fa, a + la), reverse(b + fb, b + lb);
int i = fa, j = fb;
while (i < la)
a[i] -= '0', i++;
while (j < lb)
b[j] -= '0', j++;
if (fa ^ fb)
minu();
else
add();
return 0;
}
by __youzimo2014__ @ 2024-01-05 16:57:14
这道题不用高精度吧
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a + b;
}
by ge_yiyang_001_DT @ 2024-01-13 16:36:19
@youzimo别管这些装kun大佬
by Chinese_Dragon @ 2024-01-14 11:51:06
@geyiyang001 ???
by wenmuchen105 @ 2024-01-24 18:53:05
@chen11228 就一道加法题,你弄得太麻烦了,看我的:![](D:/温沐晨C++作业/屏幕截图 2024-01-24 184959.png)就好了。
by __youzimo2014__ @ 2024-01-30 10:34:28
这道题有负数