第一题
用递归把数组的n个数实现“倒序” 主函数输入n个数,用递归将其顺序颠倒,并输出。
#include <stdio.h>
#include <math.h>
#define N 100
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void reverse(int a[],int low,int high)
{
if (low<high)
{
swap((a+low),(a+high)); //交换两个数 函数形式
reverse(a,low+ 1,high-1); //递归调用
}
}
int main(){
int a[N];
int n;
printf(“需要输入的数(n个)\n”);
scanf(“%d”,&n);
int i,j;
for(i=0;i<n;i++) scanf(“%d”,&a[i]);
reverse(a,0,n-1);
for(j=0;j<n;j++) printf("%d ",a[j]);
return 0;
}
第二题
男,女,小孩一共40人,共花了50元。男的花3元,女的花了2元,小孩花1元问男,女,小孩各多少人。
#include <stdio.h>
#include <math.h>
#define N 40
int main(){
int man,women,child;
for(man=0;man<=N;man++){
for(women=0;women<=N;women++){
for(child=0;child<=N;child++){
int person_sum=child+women+man;
int money_sum=child+women2+man3;
if(person_sum40&&money_sum50){
printf(“男有%d人,女有%d人,小孩有%d人\n”,man,women,child);
}
}
}
}
return 0;
}
第三题
10个负责打分的人,分值0-100,去掉最高分和最低分,余下8个分数平均分 为最终分数 要求: 1 输入50个选手名字,与10个评委打分 2 按最终得分求名次,分一样则名次一样 3 最终结果输出在result.txt,包括姓名,10位评委的打分,最终得分和名次。且上下对齐。 最终得分相同的名次相同。 如四名选手 80 75 75 70 名次 1 2 2 4
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define N 5
struct student{
char name[20];
int score[10];//10个评委打分;
double endscore;//最后的分数
int rank;//成绩排名
}stu[N];
void reverse(){
int i,j,index;
struct student temp,k;
for(i=0;i<N;i++){
k=stu[i];index=i;
for(j=i+1;j<N;j++){
if(k.endscore<stu[j].endscore){
k=stu[j];
index=j;
}
}
if(i!=index){
temp=stu[i];
stu[i]=k;
stu[index]=temp;
}
}
}
void sortrank(){
int i,j,rank=0;
for(i=0;i<N;i++){
if(rank0){
stu[i].rank=rank+1;
rank=rank+2;
}else{
if(stu[i].endscorestu[i-1].endscore){
stu[i].rank=stu[i-1].rank;
rank++;
}else{
stu[i].rank=rank;
rank++;
}
}
}
}
int main(){
int i,j;
FILE *fp;
if((fp=fopen(“result.txt”,“w+”))==NULL){
printf(“cannot open the file:\n”);
exit(0);
}
printf(“请输入%d名人员信息:\n”,N);
for(i=0;i<N;i++){
scanf(“%s”,stu[i].name);//输入姓名
int sum=0,max=0,min=0;
for(j=0;j<10;j++){//输入10名评委的打分
scanf(“%d”,&stu[i].score[j]);
sum+=stu[i].score[j];
if(max<=stu[i].score[j]) max=stu[i].score[j];
if(min>=stu[i].score[j]) min=stu[i].score[j];
}
sum=sum-min-max;
stu[i].endscore=sum/8.0;//最终得分
}
//按照最终得分进行排序
reverse();
//按照现有的排序进行排名
sortrank();
//写入文件
for(i=0;i<N;i++){
fprintf(fp,“%-s”,stu[i].name);
for(j=0;j<10;j++)
fprintf(fp,“%-5d”,stu[i].score[j]);
fprintf(fp,“%-5lf %-5d\n”,stu[i].endscore,stu[i].rank);
}
//在屏幕上显示信息
for(i=0;i<N;i++){
printf(“%-10s”,stu[i].name);
for(j=0;j<10;j++)
printf(“%-5d”,stu[i].score[j]);
printf(“%-5lf %-5d\n”,stu[i].endscore,stu[i].rank);
}
fclose(fp);
return 0;
}