hdu_problem_2051_Bitset

题意为:输入一个十进制的数,将它转换为二进制并输出
因为十进制到二进制的方法为除二取余,然后逆序输出,所以最开始得到的数字是个位,然后是十位,百位,所以每算一次,将得到的数乘 1 0 n 10^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;
}

猜你喜欢

转载自blog.csdn.net/CoderMaximum/article/details/86656004
今日推荐