杭电Oj刷题(2051)

Bitset(参考2031题)

题目描述:

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>
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
    	int y=0,yu,i=1;
       	while(1){
        	yu=n%2;        //余数 
        	n=n/2;         //被除数除以2 
        	y+=yu*i;       //i初始为1 
        	i*=10;         //i乘以10 
        	if(n<2){
        		y+=n*i;
        		break;
			}
		}
        printf("%d\n",y);
    }
	return 0;     
}
发布了76 篇原创文章 · 获赞 3 · 访问量 1876

猜你喜欢

转载自blog.csdn.net/ZhangShaoYan111/article/details/104262931