將十進制整數轉換成二進制數
對于每個n,以11位的寬度右對齊輸出n值,然后輸出"-->",然后輸出二進制數。
輸入樣例:
2
0
-12
1
輸出樣例:
? ? ? ? 2-->10
? ? ? ? 0-->0
? ? ?-12-->-1100
? ? ? ? 1-->1
#include<iostream>
#include<fstream>
#include<iomanip>
#include<stack>
using namespace std;int main()
{ifstream cin("test.txt");//向OJ提交時,注釋此句int n;while (cin >> n){cout << setw(11) << n << "-->";bool flag = n < 0 ? true : false;n = flag ? n*-1 : n;stack<int> s;while (n){s.push(n % 2);n /= 2;}if (s.empty())cout << 0;else{if (flag)cout << "-";while (!s.empty()){int tmp = s.top();s.pop();cout << tmp;}}cout << endl;}system("pause");//向OJ提交時,注釋此句return 0;
}