练习03-17-02
输出样例:
The matrix of score:
22.00 75.00 74.00 53.00
9.00 95.00 48.00 60.00
85.00 18.00 32.00 30.00
The average score of whole student = 50.08
The scores of first student:
The score of class 1 = 22.00
The score of class 2 = 75.00
The score of class 3 = 74.00
The score of class 4 = 53.00
--------------------------------
Process exited after 0.05259 seconds with return value 0
请按任意键继续. . .
代码如下:
//有一个班,三个学生,各学四门课,计算总平均分数和第n个学生的成绩
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main()
{
float average(float *p,int n);
void search(float (*p)[4],int n);
float score[3][4];
srand((unsigned)time(NULL));
int i,j;
printf("The matrix of score:\n");
for(i=0;i<3;++i){
for(j=0;j<4;++j){
score[i][j]=rand()%100;
printf("%7.2f",score[i][j]);
}putchar('\n');
}
float ave=average(*score,12);
printf("The average score of whole student =%7.2f\n",ave);
printf("The scores of first student:\n");
search(score,0);
return 0;
}
float average(float *p,int n)
{
float sum=0,ave;
float *i=p;
for(;i<p+n;++i){
sum+=*i;
}
ave=sum/n;
return ave;
}
void search(float (*p)[4],int n)
{
int i;
for(i=0;i<4;++i){
printf("The score of class %d = %7.2f\n",i+1,*(*(p+n)+i));
}
}
练习hanoi塔问题
//The problem of hanoi
#include<stdio.h>
int main()
{
void hanoi(int n,char one, char two, char three);
int n;
printf("Input the number of discs:\n");
scanf("%d",&n);
printf("The steps to move:\n");
hanoi(n,'A','B','C');
return 0;
}
void hanoi(int n,char one, char two, char three)
{
void move(char x, char y);
if(n==1){
move(one,three);
}else{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one, three);
}
}
void move(char x,char y)
{
printf("%c->%c\n",x,y);
}