C语言学习代码〈四〉
1 /*=========================================================== 4 *名称:hanoi塔问题 功能:无 5 *日期:2013-05-10 by:ant 6 *备注: 9 *=========================================================== */ 12 #include<stdio.h> 13 14 han(int num,char src,char mid,char dest) 15 { 16 if(1==num) 17 { 18 19 20 printf("把盘子%d从%c柱移动到%c柱\n",num,src,dest); 21 }else 22 { 23 han(num - 1,src,dest,mid); 24 25 26 printf("把盘子%d从%c柱移动到%c柱\n",num,src,dest); 27 han(num - 1,mid,src,dest); 28 } 29 } 30 31 32 int 33 34 main() 35 { 36 han(24,'a','b','c'); 37 return 0; 38 }
1 /*============================================== 2 *名称:递归练习3 功能:无 3 *日期:2013-05-10 by:ant 4 *备注:自乘 5 *============================================ */ 6 7 #include<stdio.h> 8 int sum(int num) 9 { 10 if(1 == num) 11 { 12 return 1; 13 } 14 else 15 { 16 return num * sum(num - 1); 17 } 18 } 19 int main() 20 { 21 printf("sum(5)是%d\n",sum(5)); 22 return 0; 23 }
/*=========================================== *名称:数组求平方 功能:无 *日期:2013-05-10 by:ant *备注: *=========================================== */ #include<stdio.h> void square(int value[],int num) { int loop = 0; for (loop = 0;loop<=num-1;loop++) value[loop] = value[loop]* value[loop]; } int main() { int value[]={3,4,5,6},loop=0; square(value,4); for (loop=0;loop<4;loop++) { printf("%d\n",value[loop]); } }
1 /*============================================ 2 *名称:递归练习2 功能:无 3 *日期:2013-05-10 by:ant 4 *备注:输入任何一个正整数,打印0到这个数的和: 5 *============================================ */ 6 #include<stdio.h> 7 int num=0,sum=0; 8 void show(int i) 9 { 10 11 if(i==0) 12 { 13 return; 14 }else 15 { 16 printf("%d : %d\n",sum*=num,num++); 17 18 show(--i); 19 } 20 } 21 22 int main() 23 { 24 show(5); 25 return 0; 26 }
1 /*============================================== 2 *名称:递归练习1 功能:无 3 *日期:2013-05-10 by:ant 4 *备注:输入任何一个正整数,打印到0的所有数出来。 5 *============================================= */ 6 #include<stdio.h> 7 void show(int i) 8 { 9 10 if(i==0) 11 { 12 return; 13 }else 14 { 15 printf("%d\n",i); 16 show(--i); 17 } 18 } 19 20 int main() 21 { 22 show(5); 23 return 0; 24 }
1 /*============================================= 2 *名称:函数练习 功能:无 3 *日期:2013-05-10 by:ant 4 *备注:求某个整数的相反数 5 *============================================ */ 6 #include<stdio.h> 7 8 int nge(int value[],int num)//求相反数函数 9 { 10 int loop = 0; 11 for (loop = 0;loop <= num-1;loop++) 12 { 13 value[loop] = 0 - value[loop]; 14 } 15 return 0; 16 } 17 18 int main() 19 { 20 int value[3] = {4,-7,9},loop = 0; 21 nge(value,3); 22 printf("%d,%d,%d\n",value[0],value[1],value[2]); 23 return 0; 24 }
/*============================================== *名称:时钟 功能:无 *日期:2013-05-10 by:ant *备注:秒到59分进1,否则秒++,分时雷同,但不自加,由秒进位才加。 *============================================ */ #include<stdio.h> int hour=0,minute=0,second=0; int main() { for(;1>0;) { if(second==59)//如果秒为59向分进位 { minute++;//分加1 second=0;//秒清零 }else { second++; } if(minute==59) { hour++;//小时加1 minute=0;//分钟清零 } if(hour==23) { hour=0;//小时清零 } printf("%d时%d分%d秒\n",hour,minute,second); sleep(1); //main(); } }