C程序设计--案例(2014年江苏大学程序设计考研试题 -- 编程题)

版权声明:本文由 Micheal 超 博客 创作,转载请附带链接,有问题欢迎交流。 https://blog.csdn.net/qq_42887760/article/details/84503575

题目(总):

在这里插入图片描述

解答(答案为博主自已所写,并非最优代码,仅供参考)

第一题

  • 题目

Hermite Polynomials(埃尔米特多项式)
在这里插入图片描述

  • 参考代码(答案并非最优代码,仅供参考)
#include<stdio.h>

double Hermite(int n,double x);
int main(){
	double x,result;
	int n;
	printf("请输入参数(n,x):");
	scanf("%d %lf",&n,&x);

	result=Hermite(n,x);
	printf("H%d(%lf)=%8.2lf\n",n,x,result);
	
	return 0;
}

double Hermite(int n,double x){
	if(n==0)
		return 1;
	else if(n==1)
		return 2*x;
	else if(n>1)
		return 2*x*Hermite(n-1,x)-2*(n-1)*Hermite(n-2,x);
	else{}
}
  • 代码说明

推荐参考:

  1. https://blog.csdn.net/run_out/article/details/45605607
  2. https://blog.csdn.net/qq_41035588/article/details/81639031

拓展阅读:埃尔米特多项式是一组正交的多项式,可以作为拓展阅读对该多项式进行进一步了解。

  1. https://blog.csdn.net/liyuanbhu/article/details/62904994
  2. https://blog.csdn.net/lusongno1/article/details/78708881
  • 运行结果
    在这里插入图片描述

第二题

  • 题目

对于一个充分大的正整数n,自然底数 e e 可以由 1 + 1 n n (1+\frac{1}{n})^n 公式近似求出 e 1 + 1 n n n e≈(1+\frac{1}{n})^n (n为充分大的正整数)

  • 参考代码(答案并非最优代码,仅供参考)
#include<stdio.h>
#include<math.h>

double func(int);

int main(){
	int n=1;
	double flag,result;
	do{
		n++;
		flag=fabs(func(n)-func(n-1));
	}while(flag>=1E-6);
	result=func(n);
	printf("e=%lf ; n=%d ; \n",result,n);
	return 0;
}
double func(int n){
	double temp=1+(double)1/n;
	return pow(temp,n);
}
  • 代码说明
  1. e小数点后面几位:
    e=2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320
  2. 关于自然底数 e e 的求解方式还有很多种,读者可以参考其他方法求解,作为拓展阅读。
    拓展阅读:
  1. https://blog.csdn.net/Solitarily/article/details/78557244
  2. https://blog.csdn.net/bbyz1023/article/details/80203346
  3. https://blog.csdn.net/somanlee/article/details/65936649
  • 运行结果
    在这里插入图片描述
    读者可以自行修改精度:如: while(flag>=1E-20); 条件下的 e e 值更加接近标准值:
    在这里插入图片描述

第三题

  • 题目

见上(题目总)

  • 参考代码(答案并非最优代码,仅供参考)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_N 1000

//swap()函数重载,可以参考:https://blog.csdn.net/qq_42887760/article/details/83930403
void swap(char *p,char *q);
void swap(int &a,int &b);
void swap(float &a,float &b);

struct student{
	char id[10];
	char name[20];
	float math_score;
	float chinese_score;
	float english_score;
	float sum_score;
	int rank;
};

void input(struct student stu[],int n);
void sort(struct student stu[],int n);
void print(struct student stu[],int n);

int main(){
	struct student stu[MAX_N];
	int n;
	printf("请输入学生个数(n): ");
	scanf("%d",&n);

	input(stu,n);//输入函数
	sort(stu,n);//排序函数
	print(stu,n);//输出函数
	
	return 0;
}
void input(struct student stu[],int n){
	printf("请输入每个学生的信息(学号、姓名、数学成绩、语文成绩、英语成绩)\n");
	for(int i=0;i<n;i++){
		//输入语句的注意点:stu[i].id 不能写成 &stu[i].id 而 &stu[i].math_score不能写成 stu[i].math_score
		scanf("%s%s%f %f %f",stu[i].id,stu[i].name,&stu[i].math_score,&stu[i].chinese_score,&stu[i].english_score);
		stu[i].sum_score=stu[i].math_score+stu[i].chinese_score+stu[i].english_score;
	}
}

void sort(struct student stu[],int n){
	int i,j,max;
	for(i=0;i<n-1;i++){//选择排序法
		max=i;
		for(j=i+1;j<n;j++){
			if(stu[max].sum_score<stu[j].sum_score)
				max=j;
		}
		if(max!=i){
			swap(stu[max].id,stu[i].id);
			swap(stu[max].name,stu[i].name);
			swap(stu[max].math_score,stu[i].math_score);
			swap(stu[max].chinese_score,stu[i].chinese_score);
			swap(stu[max].english_score,stu[i].english_score);
			swap(stu[max].sum_score,stu[i].sum_score);
		}
	}
	//安排名次
	int k=1;
	stu[0].rank=k;
	for(i=1;i<n;i++){
		if(stu[i].sum_score!=stu[i-1].sum_score)
			k=i+1;
		stu[i].rank=k;
	}
}

void print(struct student stu[],int n){
	printf("\n每个学生的信息(学号、姓名、数学成绩、语文成绩、英语成绩、总成绩、名次)\n");
	FILE *fp;
	if((fp=fopen("student.bat","wb"))==NULL){//测试时我改成 fp=fopen("student.txt","w")
		printf("无法打开此文件\n");
		exit(0);//需要头文件: #include<stdlib.h>
	}
	for(int i=0;i<n;i++){
		fprintf(fp,"%8s %8s %5.2f %5.2f %5.2f %5.2f %d\n",stu[i].id,stu[i].name,stu[i].math_score,stu[i].chinese_score,stu[i].english_score,stu[i].sum_score,stu[i].rank);
		printf("%8s %8s %5.2f %5.2f %5.2f %5.2f %d\n",stu[i].id,stu[i].name,stu[i].math_score,stu[i].chinese_score,stu[i].english_score,stu[i].sum_score,stu[i].rank);
	}
}


void swap(char *p,char *q){
	char temp;
	do{
		temp=*p;
		*p=*q;
		*q=temp;
	}while(*++p|*++q);
}
void swap(int &x,int &y){//函数重载
	int temp=x;
	x=y;
	y=temp;
}
void swap(float &x,float &y){//函数重载
	int temp=x;
	x=y;
	y=temp;
}
  • 代码说明
    推荐参考:

无(注意知识点的运用就行)

  • 运行结果
    在这里插入图片描述
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42887760/article/details/84503575