xueruo @ 2022-11-06 10:47:03
//二分
//#include<iostream>
//using namespace std;
//const long long INF=2*(1e9+10);
//long long a,b;
//int main(){
// cin>>a>>b;
// long long mid,left=(0-INF),right=INF;
// while(left<=right&&mid!=(a+b)){
// mid=(left+right)>>1;
// if(mid<a+b)left=mid+1;
// if(mid>a+b)right=mid-1;
// }
// cout<<mid;
// return 0;
//}
//深搜
#include<iostream>
using namespace std;
long long a,b;
long long dfs(long long x){
if(x==1)return 1;
else return dfs(x-1)+1;
}
int main(){
cin>>a>>b;
cout<<dfs(a)+dfs(b);
return 0;
}
函数递归调用太多了,占空间溢出,求优化!!doge
by youdu666 @ 2022-11-06 10:50:15
@dengjiaqiang
by xixisuper @ 2022-11-06 10:51:22
???大佬装萌新???
我表示不理解
by coldy_rainy @ 2022-11-06 10:51:49
@dengjiaqiang
...A+B至于吗?
按你这种代码的思路,总的递归层数就是输入的数字之和。题目中说了:
那肯定会爆空间啊(递归了
by Lovely_Doggie @ 2022-11-06 10:53:35
开个O2?
(为啥要这样做)
by xueruo @ 2022-11-06 10:54:26
算了,用上面的二分把 悲
by Lovely_Doggie @ 2022-11-06 10:54:45
要练深搜去做迷宫模板题呀
这个数据不爆才怪
by xixisuper @ 2022-11-06 10:55:22
@dengjiaqiang
by Lovely_Doggie @ 2022-11-06 10:55:59
水题
本蒟蒻自己编的。。。。。。
纯模板
by Lovely_Doggie @ 2022-11-06 10:56:21
dfs初学专用
by xixisuper @ 2022-11-06 11:00:48
#include <iostream>
#include <cmath>
using namespace std;
long long a,b;
long long dfs(long long x){
if(abs(x)==1||abs(x)==0) return x;
int y;
if(x<0) y=-1;
else y=1;
if(x%2) return dfs(x/2)+dfs(x/2)+y;
return dfs(x/2)+dfs(x/2);
}
int main(){
cin>>a>>b;
cout<<dfs(a)+dfs(b);
return 0;
}
@dengjiaqiang 倒是不