作者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://www.ptt.cc/bbs/Programming/M.1533125860.A.42C.html
→ MOONRAKER: if (商為0) { break; } 218.161.46.90 08/01 21:17
→ hare1039: { int num = 1000; 140.113.66.19 08/06 22:02
→ hare1039: auto str = std::bitset<64>(num) 140.113.66.19 08/06 22:03
→ hare1039: .to_string(); 140.113.66.19 08/06 22:03
→ hare1039: auto pos = str.find_first_of('1'); 140.113.66.19 08/06 22:04
→ hare1039: std::cout << str.substr(pos); } 140.113.66.19 08/06 22:05