您可以使用bitset
到十進制轉換爲二進制文件,然後打印出結果。
#include <iostream>
#include <bitset>
using namespace std;
int main() {
for(int i = 0; i <= 127; i++)
{
cout << i << " " << bitset<8>(i) << endl;
}
return 0;
}
編輯:
由於使用bitset
是不是一種選擇,讓你的剖析代碼,並瞭解什麼不妥的地方。
using namespace std;
int main()
{
int dec;
int binary_array [128];
for (dec = 0; dec<128; dec++)
{
for (int I =0; I < 7; i++)
{
binary_array[i]= dec % 2; <--OK
cout<<binary_array[i]; <-- NOT OK, think carefully
dec = dec/2; <-- OK
}
//now you have exited for loop, what is the value of dec?
//think what happened to dec in the second for loop
//your current logic will create a infinite loop
}
}
您的邏輯是正確的道路上,如果你仔細想想,你就可以自己做,感覺良好,而不是沒有試圖瞭解什麼地方出了錯複製可用的解決方案。我建議你嘗試努力思考和實施。如果仍然無法解決問題,請隨時發佈更新後的代碼並告訴我們。
來源
2015-04-04 04:55:14
sam
可能很多想法。你需要問一個更具體的問題,你有更具體的問題。就目前而言,你基本上只是要求某人完成作業。 – 2015-04-04 04:59:10