3015
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,定义一个数组,将结果存在数组内,输出的时候倒序输出
AC代码:
#include <stdio.h>
int main(void)
{
int ten,i,j;
char two[100];
while(scanf("%d",&ten)!=EOF)
{
i=0;
while(ten!=0)
{
if(ten<2)
two[i]=ten+'0';
else
two[i]=ten%2+'0';
ten/=2;
i++;
}
for(j=i-1;j>=0;j--)//j--
{
putchar(two[j]);
}
putchar('\n');
}
return 0;
}