somek_ @ 2023-10-12 19:06:55
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
long long l = 1, r = pow(2, 31) - 2;
while (l < r) {
long long mid = (l + r) / 2;
if (a + b <= mid)
r = mid;
else
l = mid + 1;
}
cout << r;
}
by zhanghe724_1 @ 2023-10-20 19:38:00
搞错了,再来
#include<bits/stdc++.h>
using namespace std;
inline __int128 read(){
__int128 x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline void write(__int128 x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9)write(x/10);
putchar(x%10+'0');
}
int main(){
__int128 a=read();
__int128 b=read();
write(a+b);
return 0;
}
by zhanghe724_1 @ 2023-10-20 19:43:15
@lijingshu_304775
我还真是刚知道这题用int能AC,以前都用long long/高精度做的
by xiao12chen @ 2023-11-17 21:47:01
多此一举
by zengyukai2012 @ 2024-01-04 21:31:29
:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
long long l = -2e9, r = 2e9;
//这里用long long是因为若
//l=1000000000,r=2000000000时
//l+r会越界,导致程序运行时间变长!!!
while (l < r)
{
int mid = (l + r) >> 1;
//不用/2是因为c++中除法是向零取整,
//对负数操作时会有问题,如:
//(-123)/2=-61
if (a + b <= mid)
r = mid;
else
l = mid + 1;
}
cout << r;
return 0;//好习惯(虽然我一般不加)
}
by K1ondra @ 2024-02-18 11:09:57
高精没考虑负数的范围啊
by K1ondra @ 2024-02-18 11:23:20
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
char ch[10010];
int a[10010],b[10010],la,lb;
int main() {
cin>>ch+1;
if(ch[1]=='-' ){
la=strlen(ch+1)-1;
for( int i=2;i<=la+1;++i)a[la+1-i+1]=-(ch[i]-'0');
} else {
la = strlen(ch+1);
for( int i=1;i<=la;++i)a[la-i+1]=ch[i]-'0';
}
cin>>ch+1;
if(ch[1]=='-'){
lb=strlen(ch+1)-1;
for(int i=2;i<=lb+1;++i)b[lb+1-i+1]=-(ch[i]-'0');
} else {
lb = strlen(ch+1);
for( int i=1;i<=lb;++i)b[lb-i+1]=ch[i]-'0';
}
la=max(la,lb)+1;
for(int i=1;i<la;++i)a[i]+=b[i];
for(int i = 1; i<la;++i){
a[i+1]+=a[i]/10;
a[i]%=10;
if(a[i]<0){
a[i]+=10;
--a[i+1];
}
}
while(la>1&&a[la]==0)--la;
if(a[la]<0){
cout<<'-';
for( int i=1;i<=la;++i)a[i]=-a[i];
for( int i=1;i<la;++i){
if(a[i]<0){
a[i]+=10;
--a[i+1];
}
}
while(la>1&&a[la]==0)--la;
for(int i=la;i>=1;--i)cout<<a[i];
} else
for(int i=la;i>=1;--i )cout<<a[i];
cout<<endl;
return 0;
}
你的高精度没有讨论负数的范围
by HUDSI1265 @ 2024-02-19 15:08:24
@somek_ 因为它相加可能是负数,或者超int,所以你要开longlong,左端点变成-2^63-1,右端点变成2^63-1