题目
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
思路
输入一个十进制数,输出一个二进制数。
rt,我果断想到了java中的toBinaryString();
不过,相比而言,用c实现较麻烦!
java代码
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner it=new Scanner(System.in);
while(it.hasNext()){
int n = it.nextInt();
System.out.println(Integer.toBinaryString(n));
}
}
}
c代码
#include<stdio.h>
int main(){
int n;
while(scanf(" %d",&n)!=EOF){
int a[12];
for(int i=0;n>0;i++){
if(n%2==0)a[i]=2;
else a[i]=1;
n=n/2;
}
for(int i=12;i>=0;i--){
if(a[i]==2)printf("0");
if(a[i]==1)printf("1");
}
printf("\n");
}
return 0;
}
个人博客:陪你一起终身学习! | 岳金钊&个人博客