HDU 2051 - Bitset(十进制转二进制)

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");
	}
}

 

    原文作者:进制转换
    原文地址: https://blog.csdn.net/wan_ide/article/details/81408512
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞