我现在做的是第三专题编号为1015的试题,具体内容如下所示:
Problem O
Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 59 Accepted Submission(s) : 40
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
解题思路:就是利用求二进制的计算方法,对数据进行除2取余,将余数放进一个数组,最后将数组进行反向输出。
编写代码:
#include <iostream>
using namespace std;
int main()
{
int num;
while (cin >> num)
{
int k = 0;
int a[1000];
int data;
while (1)
{
a[k] = num % 2;
k++;
num = num / 2;
if (num == 0)
break;
}
int b = 0;
for (int j=k-1; j>=0; j--)
b = b * 10 + a[j];
cout << b << endl;
}
return 0;
}