作者ntpuisbest (阿龙)
看板Programming
标题[问题] cpp十进位转二进位
时间Wed Aug 1 20:17:37 2018
int main() {
int x=5;
int n=31 ;
int number[n];
while (x>=0){
for ( int i=n;i>=0;i--){
number [i]=x%2;
x=x/2;
}
}
for ( int i=n;i>=0;i--){
cout<<number[i]<<endl;
}
}
写了一个小程式
目标是把10进位的数字,转成二进位
但是我这个程式的问题是
会有一堆零不需要的零
请问该怎麽处理?
还有如何上当我的商(X)是0的时候
就不要执行程式呢?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.167.48.118
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Programming/M.1533125860.A.42C.html
1F:→ MOONRAKER: if (商为0) { break; } 218.161.46.90 08/01 21:17
2F:→ hare1039: { int num = 1000; 140.113.66.19 08/06 22:02
3F:→ hare1039: auto str = std::bitset<64>(num) 140.113.66.19 08/06 22:03
4F:→ hare1039: .to_string(); 140.113.66.19 08/06 22:03
5F:→ hare1039: auto pos = str.find_first_of('1'); 140.113.66.19 08/06 22:04
6F:→ hare1039: std::cout << str.substr(pos); } 140.113.66.19 08/06 22:05