C语言编程题5道

17、某班有5个学生,三门课。分别编写3个函数实现以下要求:
(1) 求各门课的平均分;
(2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
(3) 找出三门课平均成绩在85-90分的学生,并输出其学号和姓名
主程序输入5个学生的成绩,然后调用上述函数输出结果。
#define SNUM 5 /*student number*/
#define CNUM 3 /*course number*/
#include<stdio.h>
#include<conio.h>

/*disp student info*/
void DispScore(char num[][6],char name[][20],float score[][CNUM])
{
int i,j;
printf("\n\nStudent Info and Score:\n");
for(i=0;i<SNUM;i++)
{
printf("%s ",num[i]);
printf("%s ",name[i]);
for(j=0;j<CNUM;j++)
printf("%8.2f",score[i][j]);
printf("\n\n");
}
}

/*calculate all student average score*/
void CalAver(float score[][CNUM])
{
float sum,aver;
int i,j;
for(i=0;i<CNUM;i++)
{
sum=0;
for(j=0;j<SNUM;j++)
sum=sum+score[j][i];
aver=sum/SNUM;
printf("Average score of course %d is %8.2f\n",i+1,aver);
}
}

/*Find student: two courses no pass*/
void FindNoPass(char num[][6],float score[][CNUM])
{
int i,j,n;
printf("\nTwo Course No Pass Students:\n");
for(i=0;i<SNUM;i++)
{
n=0;
for(j=0;j<CNUM;j++)
if(score[i][j]<60)
n++;
if(n>=2)
{
printf("%s ",num[i]);
for(j=0;j<CNUM;j++)
if(score[i][j]<60)
printf("%8.2f",score[i][j]);
printf("\n");
}
}
}

/*Find student: three courses 85-90*/
void FindGoodStud(char num[][6],char name[][20],float score[][CNUM])
{
int i,j,n;
printf("\nScore of three courses between 85 and 90:\n");
for(i=0;i<SNUM;i++)
{
n=0;
for(j=0;j<CNUM;j++)
if(score[i][j]>=85&&score[i][j]<=90)
n++;
if(n==3)
printf("%s %s\n",num[i],name[i]);
}
}

/*input student info*/
void main()
{
char num[SNUM][6],name[SNUM][20]; //array num refers to student number
float score[SNUM][CNUM]; //and its length is 6
int i,j;

clrscr();
printf("\nPlease input student num and score:\n");

for(i=0;i<SNUM;i++)
{
printf("\n\nStudent%d number: ",i+1);
scanf("%s",num[i]);
printf("\nStudent%d name: ",i+1);
scanf("%s",name[i]);
printf("\nStudent%d three scores: ",i+1);
for(j=0;j<CNUM;j++)
scanf("%f",&score[i][j]);
}

DispScore(num,name,score);
CalAver(score);
FindNoPass(num,score);
FindGoodStud(num,name,score);
getch();
}

18、编写一人个求X的Y次幂的递归函数,X为double型,y为int型,要求从主函数输入x,y的值,调用函数求其幂。
#include<stdio.h>
#include<conio.h>

double fact(double x,int y)
{
if(y==1)
return x;
else
return x*fact(x,y-1);
}

void main()
{
double x;
int y;
clrscr();
printf("\nPlease x,y:");
scanf("%lf%d",&x,&y);
printf("\nx^y=%.2lf",fact(x,y));
getch();
}


19、打印魔方阵。
所谓魔方阵是指这样的的方阵:
它的每一行、每一列和对角线之和均相等。
输入n,要求打印由自然数1到n2的自然数构成的魔方阵(n为奇数)。
例如,当n=3时,魔方阵为:
  8 1 6
  3 5 7
  4 9 2
魔方阵中各数排列规律为:
① 将“1”放在第一行的中间一列;
② 从“2”开始直到n×n为止的各数依次按下列规则存放:每一个数存放的行比前一个数的行数减1,列数同样加1;
③ 如果上一数的行数为1,则下一个数的行数为n(最下一行),如在3×3 方阵中,1在第1行,则2应放在第3行第3列。
④ 当上一个数的列数为n时,下一个数的列数应为1,行数减1。如2在第3行第3列,3应在第2行第1列。
⑤如果按上面规则确定的位置上已有数,或上一个数是第1行第n列时,则把下一个数放在上一个数的下面。如按上面的规定,4应放在第1行第2列,但该位置已被1占据,所以4就放在3的下面。由于6是第1行第3列(即最后一列),故7放在6下面。
#include<stdio.h>
#include<conio.h>
#define Max 15

void main()
{
int i,row,col,odd;
int m[Max][Max];

clrscr();
printf("\nPlease input an odd:");
scanf("%d",&odd);
if(odd<=0||odd%2==0)
{
printf("\nInput Error!\n");
getch();
return 0;
}
printf("\nodd=%d\n\n",odd);
row=0;
col=odd/2; //1 placed in the middle of the first row
for(i=1;i<=odd*odd;i++)
{
m[row][col]=i;
if(i%odd==0) //to the last col
if(row==odd-1) //to the last row
row=0;
else
row++;
else //outmost else
{
if(row==0)
row=odd-1;
else
row--;
if(col==odd-1)
col=0;
else
col++;
} //end of outmost else
} //end of for
for(row=0;row<odd;row++)
{
for(col=0;col<odd;col++)
printf("%4d",m[row][col]);
printf("\n\n");
}
getch();
return 0;
}

20、找出一个二维数组中的“鞍点”,即该位置上的元素在该行中最大,在该列中最小(也可能没有“鞍点”),打印出有关信息。
#define N 20
#define M 20
#include<stdio.h>
#include<conio.h>

void main( )
{
int a[N][M]; //int a[][]; not allowed here
int i,j,k,row,col,n,m,find=0;

clrscr();
printf("\nEnter n & m:\n\n");
scanf("%d%d",&n,&m);
printf("\nEnter a[0][0]--a[%d][%d]\n\n",n-1,m-1);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);

printf("\n\nThe array you have just entered is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%5d",a[i][j]);
printf("\n\n");
}

//find the point
for(i=0;i<n;i++)
{
for(col=0,j=1;j<m;j++)
if(a[i][col]<a[i][j]) //find col,select sort according to col
col=j;
for(row=0,k=1;k<n;k++)
if(a[row][col]>a[k][col]) //find row,select sort according to row
row=k;
if(i==row)
{
find=1;
printf("The point is a[%d][%d].\n",row,col);
}
}
if(!find)
printf("\nNo solution.\n");
getch();
}


21、马克思在《数学手稿》中提出如下问题:有30个人(包括男人、女人和小孩)在一家饭店吃饭共花50先令,其中每个男人花3先令,每个女人花2先令,
每个小孩花1先令,问男人、女人、小孩各有多少人?
#include<stdio.h>
#include<conio.h>

void main()
{
int man,woman,child,money=50,count=30;
int i,j,k;

clrscr();
printf("They consist of:\n\n");
for(i=0;i<=count;i++)
for(j=0;j<=count;j++)
for(k=0;k<=count;k++)
if(i+j+k==count&&3*i+2*j+k==money)
{
printf("man=%2d woman=%2d child=%2d\n",i,j,k);
printf("\n");
}
getch();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值