codeup-进制转换

1943: 进制转换

Time Limit: 1 Sec  Memory Limit: 32 MB
Submit: 1559  Solved: 579
[Submit][Status][Web Board][Creator:Imported]

Description

将一个长度最多为30位数字的十进制非负整数转换为二进制数输出。

Input

多组数据,每行为一个长度不超过30位的十进制非负整数。
(注意是10进制数字的个数可能有30个,而非30bits的整数)

Output

每行输出对应的二进制数。

Sample Input

985
211
1126

Sample Output

1111011001
11010011
10001100110


 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main(){
 5     char s[40];
 6     int c[40];
 7     while(scanf("%s", s) != EOF){
 8         int len=strlen(s), i=0;
 9         //q表示是否向高位借位  temp先保存高位
10         int ans[100], num=0, q, temp;  
11         //将字符串先转化为整数数组 
12         for(int k=0; k<len; k++){
13             c[k] = s[k] - '0';
14         }
15         while(i<len){
16             q = 0;
17             ans[num++] = (c[len-1]) % 2;
18             //生成一个新数组
19             for(int j=i; j<len; j++){
20                 temp = c[j];
21                 c[j] = (c[j]+q) / 2;
22                 if(temp % 2 == 0){ //判断高位是否除尽 
23                     q = 0;
24                 }else{
25                     q = 10;
26                 }
27             }
28             if(c[i] == 0) i++;
29         }
30         for(int i=num-1; i>=0; i--){
31             printf("%d", ans[i]);
32         }
33         printf("\n");
34     }
35     return 0;
36 }

猜你喜欢

转载自www.cnblogs.com/heyour/p/12149871.html