hdu_problem_2051_Bitset

題意爲:輸入一個十進制的數,將它轉換爲二進制並輸出
因爲十進制到二進制的方法爲除二取餘,然後逆序輸出,所以最開始得到的數字是個位,然後是十位,百位,所以每算一次,將得到的數乘10n10^n再加上去就可以不用逆序輸出

/*
*
*Problem Description
*Give you a number on base ten,you should output it on base two.(0 < n < 1000)
*
*
*Input
*For each case there is a postive number n on base ten, end of file.
*
*
*Output
*For each case output a number on base two.
*
*
*Sample Input
*1
*2
*3
*
*
*Sample Output
*1
*10
*11
*
*
*Author
*8600 && xhd
*
*
*Source
*校慶杯Warm Up
*
*
*Recommend
*linle
*
*/
#include<iostream>
using namespace std;
int main() {
 int num, result, a;
 while (cin >> num) {
  result = 0, a = 1;
  while (num) {
   result += a * (num % 2);
   num /= 2;
   a *= 10;
  }
  cout << result << endl;
 }
 system("pause");
 return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章