Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
有没有哪位大神帮我看看这代码,,跟求最小步数的题类似,但需要考虑的情况太多了#include<cstdio> #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int n,k,sum; void dfs(int x,int y,int s) { if(x<=0) return ; if(x==y) { sum=min(sum,s); return ; } if(s>sum) return ; s++; dfs(x+1,y,s); dfs(x-1,y,s); dfs(2*x,y,s); } int main() { while(~scanf("%d%d",&n,&k)) { sum=100000; if(n>=k) printf("%d\n",n-k); else { if(n==0) { n=1; dfs(n,k,0); printf("%d\n",sum+1); } else { dfs(n,k,0); printf("%d\n",sum); } } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator