_tao_tie_ @ 2025-01-11 18:39:58
为什么不过?求条!
#include<bits/stdc++.h>
using namespace std;
int main()
{
int l2,l1,i,c[210],b[210],a[210],mx;
string z1,z2;
memset(c,0,sizeof(c));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cin>>z1>>z2;
l1=z1.length();
l2=z2.length();
for(i=0;i<l1;i++)
a[i]=z1[l1-1-i]-'0';
for(i=0;i<l2;i++)
b[i]=z2[l2-1-i]-'0';
mx=max(l1,l2)+1;
for(i=0;i<=mx-1;i++)
{
c[i]+=b[i]+a[i];
if(c[i]>9)
{
c[i]%=10;
c[i+1]++;
}
}
while(c[mx]==0&&mx>0)
mx--;
for(i=mx;i>=0;i--)
cout<<c[i];
return 0;
}
by be_the_person @ 2025-01-11 18:46:08
@_taotie 你确定要用这么长的代码?
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<a+b<<endl;
return 0;
}
by liuzhuoran141516 @ 2025-01-11 19:01:37
@_taotie 友情提醒:
输入两个整数
注意
integer
会爆掉哦!int
类型,而且 C 最后要 return 0
。这不仅对洛谷其他题目有效,而且也是 NOIP/CSP/NOI 比赛的要求!by liuzhuoran141516 @ 2025-01-11 19:03:06
所以,你的高精度要算有负数的情况,要不然会WA
by liuzhuoran141516 @ 2025-01-11 19:15:39
@_taotie 高精度示例代码:
#include <bits/stdc++.h>
using namespace std;
bool cmp(string a, string b) {
if (a.length() != b.length()) return a.length() > b.length();
return a > b;
}
string sub(string a, string b) {
string res;
int c = 0;
int i = a.length() - 1;
int j = b.length() - 1;
while (i >= 0 || j >= 0) {
int d1 = (i >= 0) ? (a[i--] - '0') : 0;
int d2 = (j >= 0) ? (b[j--] - '0') : 0;
int diff = d1 - d2 - c;
if (diff < 0) {
diff += 10;
c = 1;
} else {
c = 0;
}
res.push_back(diff + '0');
}
while (res.length() > 1 && res.back() == '0') res.pop_back();
reverse(res.begin(), res.end());
return res;
}
string add(string a, string b) {
bool na = (a[0] == '-');
bool nb = (b[0] == '-');
string x = na ? a.substr(1) : a;
string y = nb ? b.substr(1) : b;
if (!na && !nb) {
string res;
int c = 0;
int i = x.length() - 1;
int j = y.length() - 1;
while (i >= 0 || j >= 0 || c) {
int s = c;
if (i >= 0) s += x[i--] - '0';
if (j >= 0) s += y[j--] - '0';
c = s / 10;
res.push_back((s % 10) + '0');
}
reverse(res.begin(), res.end());
return res;
} else if (na && nb) {
return "-" + add(x, y);
} else {
bool xg = cmp(x, y);
string res;
if (xg) {
res = sub(x, y);
if (na) {
res = "-" + res;
}
} else {
res = sub(y, x);
if (nb) {
res = "-" + res;
}
}
return res;
}
}
string a, b;
int main() {
cin >> a >> b;
cout << add(a, b);
return 0;
}
AC记录
by _tao_tie_ @ 2025-01-11 19:15:49
@liuzhuoran141516 试了一下,可以有负数耶...
by liuzhuoran141516 @ 2025-01-11 19:16:32
@_taotie 这句什么意思
by liuzhuoran141516 @ 2025-01-11 19:17:13
是说数据有负数还是说你的代码能算负数
by _tao_tie_ @ 2025-01-11 19:24:02
@liuzhuoran141516 我自测了一下,负数的数据也可以
by liuzhuoran141516 @ 2025-01-11 19:25:30
@_taotie 说清楚点,是我的代码还是你的