Bitset
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 28161 Accepted Submission(s): 20775
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
题意:
将十进制转化为二进制。
思路:
可以用字符串存,也可以用数组存。
字符串:
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,sum,len;
char a[1010];
while(scanf("%d",&n)!=EOF)
{
len=0;
while(n>0)
{
a[len++]=n%2+'0';
n/=2;
}
a[len]='\0';
strrev(a);
puts(a);
}
return 0;
}
数组:
#include<stdio.h>
int main()
{
int i,n,sum;
int b[1010];
while(scanf("%d",&n)!=EOF)
{
i=0;
while(n>0)
{
b[i++]=n%2;
n/=2;
}
i--;
while(i>=0)
printf("%d",b[i--]);
printf("\n");
}
}