HDU 2175 汉诺塔IX
找规律就好了。
会发现对称性。。。。
用的找规律代码:
#include <iostream> #include <cstdio> using namespace std; int cnt=1; void f (int n, char A, char B, char C) { if (n == 1) { cout <<cnt++<< ": No." << n << " " << A << " -> " << C << endl; } else { f(n-1, A, C, B); cout <<cnt++<< ": No." << n << " " << A << " -> " << C << endl; f(n-1, B, A, C); } } int main() { int n; cin >> n; f (n, 'A', 'B', 'C'); return 0; }
#include <iostream> #include<cmath> using namespace std; int main() { unsigned long long a[65],m,t; int n; a[1]=1; for(int i=2;i<64;i++) a[i]=2*a[i-1]+1; while(scanf("%d%I64d",&n,&m),n||m) { t=a[n]+1; for(int i=n;i>=1;i--) { t/=2; if(t==m) { printf("%d\n",i); break; } if(m>t) m=m-t; else m=t-m; } } return 0; }
新 blog : www.hrwhisper.me