标签:style blog class code java javascript width color int 2014 http
十进制转二进制的递归实现算法:
1 #include <stdio.h>
2 void dectobi(int a);
3
4 int main()
5 {
6 int i;
7 printf("please input a decimical number\n");
8 scanf("%d",&i);
9 printf("the binary number is:");
10 dectobi(i);
11 return 0;
12 }
13 void dectobi(int a)
14 {
15 int temp=a%2;
16 if(0!=a/2)
17 dectobi(a/2);
18 printf("%d",temp);
19 }
实现依据:
十进制转二进制非递归实现:
思想:用数组存储每次取余的结果,a0~a15 (模拟二进制结果用两个字节来保存) 每次存储的时候是最低位存储,所以输出时应倒序输出。
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int i,m,r,s;
6 int result[16]={0};//初始化为0
7 printf("please input a decimical number!\n");
8 scanf("%d",&i);
9 for(m=0;m<15;m++)
10 {
11 r=i%2;
12 s=i/2;
13 i=s;
14 result[m]=r;
15 }
16 for(m=15;m>=0;m--)
17 {
18 printf("%d",result[m]);
19 if(m%4 == 0)
20 printf(" ");
21 }
22
23
24 return 0;
25 }
标签:style blog class code java javascript width color int 2014 http
原文地址:http://www.cnblogs.com/xiaoying1245970347/p/3702861.html