//二位元轉十位元 >< #include <iostream> #include <string> using namespace std; int BintoDec(string a, int i = 0){ int n = a.length(); if(i == n-1){ return (a[i] - '0'); } return ((a[i] - '0') << (n - i - 1)) + BintoDec(a, i+1); } int main(){ string a; cin >> a; cout << BintoDec(a); //輸入二位元 } |
//十位元轉二位元 >////< #include <iostream> #include <string> using namespace std; int DectoBin(int a){ if(a == 0){ return 0; } return (a % 2) + 10 * DectoBin(a / 2); } int main(){ int a; cin >> a; cout << DectoBin(a); } |
//河內塔 (((?д?))) #include <iostream> using namespace std; //A是起點 C是終點 B是暫存點 int time = 0; void Hanoi(int n, char A, char B, char C){ if(n == 1){ cout << "No. " << n << " move from " << A << " to " << C << endl; time++; }else{ Hanoi(n-1, A, C, B); //n-1個移動到暫存點上 cout << "No. " << n << " move from " << A << " to " << C << endl; //最下面第n個移動到終點 time++; Hanoi(n-1, B, A, C); //把暫存點上的n-1移動到終點蓋上去 } } int main(){ int n; cin >> n; Hanoi(n, 'A', 'B', 'C'); cout << "time = " << time; } |