# include<stdio.h>//二进制转十进制c程序intmain(){intbin_to_deci(int a);int a =101111;int deci =bin_to_deci(a);printf("%d\n",deci);return0;}intbin_to_deci(int a){int factor =1;int sum =0,tem =0;while(a>0){
tem =(a%10)*factor;//101111 = 1*2^0 + 1*2^1 + 1*2^2 + 1*2^3 + 0*2^4 + 1*2^5
sum = sum + tem;
factor = factor*2;
a = a /10;}return sum;}
2.十进制转二进制程序
# include<stdio.h>intmain(){intdeci_to_bin(int a);int a =19;deci_to_bin(a);printf("\n");return0;}intdeci_to_bin(int a){int bin =0;int m[20];int i =0,j;while(a>0){
m[i]= a%2;//除2取余数法
i++;
a = a/2;}for(j=i-1;j>=0;j--){//i需要减去1,因为每次结束后,i都加1,这里多了printf("%d",m[j]);}return bin;}
# include<stdio.h>//给你一串数字,实现其排序,然后把排序后重复的数字只留下一个。如(3,5,5,2,1,4,6)--->(1,2,3,4,5,6)。intmain(){voidbubble(int a[10]);voidremove_repeat(int*a);int i;int a[10];printf("请输入10个整数(中间用空格隔开):\n");for(i=0;i<10;i++){scanf("%d",&a[i]);}bubble(a);//先排序remove_repeat(a);//去重复printf("\n");return0;}//交换数据voidswap(int*x,int*y){int tem =0;
tem =*x;*x =*y;//注意交换,传递数值,必须指针,直接传递数值,不行*y = tem;}//去重voidremove_repeat(int*a)//传递地址,数组a[]的首个元素地址就是a{int b[10];int i ,j =1;
b[0]= a[0];for(i=1;i<10;i++){if(a[i]!= a[i-1]){
b[j]= a[i];
j++;}}for(i=0;i<j;i++){printf("%d ",b[i]);}printf("\n");}//排序voidbubble(int a[10])//冒泡排序算法{voidswap(int*x,int*y);int i ,j ;for(i=0;i<10;i++){for(j=0;j<10-i-1;j++){if(a[j]>a[j+1])swap(&a[j],&a[j+1]);}}for(i=0;i<10;i++){printf("%d ",a[i]);}printf("\n");}
4.求出一年中的第n天是几月几号并输出
# include<stdio.h>//求出一年中的第n天是几月几号并输出 intmain(){voiddate_fun(int year,int n);int year =0, n =0;printf("请输入年份和天数,中间用空格隔开:");scanf("%d%d",&year,&n);date_fun(year,n);return0;}voiddate_fun(int year,int n){int arr1[]={31,28,31,30,31,30,31,31,30,31,30,31};int arr2[]={31,29,31,30,31,30,31,31,30,31,30,31};int i=0;int count = n;//闰年2月29天,其余2月28天if(year%4==0&&year%100!=0||year%400==0){for(i;i<12;i++){if(n>arr2[i]){
n = n-arr2[i];//减去每个月的天数,求过了几个月}elsebreak;}}else{for(i;i<12;i++){if(n>arr1[i]){
n = n-arr1[i];}elsebreak;}}if((i+1)>12){printf("输入天数超过一年的总天数,请重新输入:\n");}else{printf("%d年中的第%d天是%d月%d日\n",year,count,i+1,n);}}
5.递归函数调用,求n的阶乘
# include<stdio.h>//递归函数调用intmain(){intrecursion_fun(int n);int n =0,res=0;scanf("%d",&n);
res =recursion_fun(n);printf("%d\n",res);return0;}intrecursion_fun(int n){if(n==0)return0;if(n==1)return1;//递归终止条件return n *recursion_fun(n-1);//递归调用}
1.二进制数转十进制数程序# include <stdio.h>//二进制转十进制c程序int main(){ int bin_to_deci(int a); int a = 101111; int deci = bin_to_deci(a); printf("%d\n",deci); return 0;}int bin_to_deci(int a){ int factor = 1; int sum = 0,tem = 0; while(a>0){