欢迎来到天天文库
浏览记录
ID:38808413
大小:21.28 KB
页数:15页
时间:2019-06-19
《电子科大C语言实验4》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第九章编程题1/*******************************************************************************功能:整数组排序**selection_sort函数原理:1,找出数组中最大的数,把它移动到数组最后;**2,递归的调用本身对前n-1个数组元素排序。******************************************************************************/#includevoidselection_sort(intm,inta[]){intmax=a[
2、0];inti,j;for(i=1,j=0;i2)selection_sort(m-1,a);/*递归调用本身*/}intmain(void){intn=0,a[99],i;printf("Entersomenumberstobesorted:");printf("InSortedorder:");for(i=0;i3、hile(getchar()!='');selection_sort(n,a);第九章编程题5/***********************用户输入数n后,调用函数创建幻方***********************/#includevoidcreate_magic_square(intn,charmagic_square[99][99]);voidprint_magic_square(intn,charmagic_square[99][99]);intmain(void){charmagic_square[99][99]={0};intsize;printf("T4、hisprogramcreatesamagicsquareofaspecifiedsize.");printf("Thesizemustbeanoddnumberbetween1and99.");printf("Entersizeofmagicsquare:");scanf("%d",&size);create_magic_square(size,magic_square);print_magic_square(size,magic_square);return0;}voidcreate_magic_square(intn,charmagic_square[99][99])/*此函数5、用1,2,…,n²填充数组*/{intmiddle,i,j=0;middle=(n-1)/2;magic_square[j][middle]=1;for(i=2;i<=n*n;i++){j--;middle++;if(j==-1)j=n-1;if(middle==n)middle=0;if(magic_square[j][middle]==0)magic_square[j][middle]=i;else{if(j==n-1&&middle==0){j=-1;middle=n;}else{if(j==n-1)j==-1;if(middle==0){middle=n-1;j--;}}j=j+2;6、middle--;magic_square[j][middle]=i;}}}voidprint_magic_square(intn,charmagic_square[99][99]){inti,j;for(i=0;iintpower(intx,intn);intmain7、(void){intx,n;chars;printf("Enter(x^n):");scanf("%d%c%d",&x,&s,&n);printf("%d^%d=%d",x,n,power(x,n));return0;}intpower(intx,intn){if(n%2==0){if(n==0)return1;elsereturnpower(x,n/2)*power(x,n/2);}else{if(n==0
3、hile(getchar()!='');selection_sort(n,a);第九章编程题5/***********************用户输入数n后,调用函数创建幻方***********************/#includevoidcreate_magic_square(intn,charmagic_square[99][99]);voidprint_magic_square(intn,charmagic_square[99][99]);intmain(void){charmagic_square[99][99]={0};intsize;printf("T
4、hisprogramcreatesamagicsquareofaspecifiedsize.");printf("Thesizemustbeanoddnumberbetween1and99.");printf("Entersizeofmagicsquare:");scanf("%d",&size);create_magic_square(size,magic_square);print_magic_square(size,magic_square);return0;}voidcreate_magic_square(intn,charmagic_square[99][99])/*此函数
5、用1,2,…,n²填充数组*/{intmiddle,i,j=0;middle=(n-1)/2;magic_square[j][middle]=1;for(i=2;i<=n*n;i++){j--;middle++;if(j==-1)j=n-1;if(middle==n)middle=0;if(magic_square[j][middle]==0)magic_square[j][middle]=i;else{if(j==n-1&&middle==0){j=-1;middle=n;}else{if(j==n-1)j==-1;if(middle==0){middle=n-1;j--;}}j=j+2;
6、middle--;magic_square[j][middle]=i;}}}voidprint_magic_square(intn,charmagic_square[99][99]){inti,j;for(i=0;iintpower(intx,intn);intmain
7、(void){intx,n;chars;printf("Enter(x^n):");scanf("%d%c%d",&x,&s,&n);printf("%d^%d=%d",x,n,power(x,n));return0;}intpower(intx,intn){if(n%2==0){if(n==0)return1;elsereturnpower(x,n/2)*power(x,n/2);}else{if(n==0
此文档下载收益归作者所有