hdu_problem_2051_Bitset

本文介绍了一种将十进制数转换为二进制数的算法,并提供了详细的实现代码。通过除二取余法,每次获取的余数即为二进制数的位值,再通过适当的操作避免了逆序输出的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意为:输入一个十进制的数,将它转换为二进制并输出
因为十进制到二进制的方法为除二取余,然后逆序输出,所以最开始得到的数字是个位,然后是十位,百位,所以每算一次,将得到的数乘10n10^n10n再加上去就可以不用逆序输出

/*
*
*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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值