大学程序上机问题,尽量只用最简单入门的c语言解决题目(c++都不要用 反正越简单越好),如果方便的话可以在代码行中加入一些注释,最后是一定要自己写的,不要拿论坛中其他人的糊弄,谢了,比较急,后天下午就要上交了
【14】
#include <stdio.h>
#define N 5
//定义一个结构体类型student,包含学号和成绩两个字段
typedef struct student{
int id;
int score;
}student;
//定义一个函数fun,参数是结构体数组s和b,返回值是分数最高的学生的人数
int fun(student s[], student b[]){
//定义一个变量max,用来存储分数最高的值,初始为s[0]的成绩
int max = s[0].score;
//定义一个变量count,用来计数分数最高的学生的人数,初始为0
int count = 0;
//定义一个变量i,用来遍历结构体数组s
int i;
//用一个for循环,从0到N-1,遍历结构体数组s
for(i = 0; i < N; i++){
//如果当前学生的成绩大于max,说明找到了新的最高分
if(s[i].score > max){
//更新max为当前学生的成绩
max = s[i].score;
//重置count为1,因为只有一个学生是最高分
count = 1;
//把当前学生的数据复制到b[0]中
b[0] = s[i];
}
//否则,如果当前学生的成绩等于max,说明有多个学生是最高分
else if(s[i].score == max){
//增加count的值,表示多了一个最高分的学生
count++;
//把当前学生的数据复制到b[count-1]中,注意数组下标从0开始
b[count-1] = s[i];
}
//否则,如果当前学生的成绩小于max,说明不是最高分,不做任何操作
}
//返回count作为函数的结果
return count;
}
//定义一个常量N,表示学生的人数
#define N 5
//定义主函数main
int main(){
//定义一个结构体数组s,用来存储N名学生的数据
student s[N];
//定义一个结构体数组b,用来存储分数最高的学生的数据
student b[N];
//定义一个变量i,用来遍历结构体数组s和b
int i;
//定义一个变量num,用来接收函数fun的返回值
int num;
//用一个for循环,从0到N-1,输入结构体数组s的数据
for(i = 0; i < N; i++){
printf("请输入第%d名学生的学号和成绩:\n", i+1);
scanf("%d %d", &s[i].id, &s[i].score);
}
//调用函数fun,传入结构体数组s和b,并把返回值赋给num
num = fun(s, b);
//输出结果
printf("分数最高的学生有%d名,他们的数据如下:\n", num);
for(i = 0; i < num; i++){
printf("学号:%d\t成绩:%d\n", b[i].id, b[i].score);
}
return 0;
}
写了半天,终于写好了,如果有用,还请麻烦赏个采纳,谢谢啦
14
#include <stdio.h>
#include <string.h>
typedef struct {
char name[20];
int score;
} student;
void fun(student s[], student b[]) {
int max_score = 0;
int num_max_score = 0;
// 找到最高分数
for (int i = 0; i < 5; i++) {
if (s[i].score > max_score) {
max_score = s[i].score;
}
}
// 找到最高分数的学生并复制到b中
for (int i = 0; i < 5; i++) {
if (s[i].score == max_score) {
strcpy(b[num_max_score].name, s[i].name);
b[num_max_score].score = s[i].score;
num_max_score++;
}
}
}
int main() {
student s[5] = {
{"张三", 85},
{"李四", 90},
{"王五", 80},
{"马六", 90},
{"车二愣子", 45}
};
student b[5];
fun(s, b);
printf("最高分数的学生是:\n");
for (int i = 0; i < 5; i++) {
if (b[i].score != 0) {
printf("%s,成绩是 %d\n", b[i].name, b[i].score);
}
}
return 0;
}
41
#include <stdio.h>
double fun(int n) {
double sum = 0.0;
int denominator = 0;
for (int i = 1; i <= n; i++) {
denominator += i;
sum += 1.0 / denominator;
}
return sum;
}
int main() {
int n;
printf("请输入一个正整数n:");
scanf("%d", &n);
double result = fun(n);
printf("1+1/(1+2)+1/(1+2+3)+...+1/(1+2+3+...+%d) = %lf\n", n, result);
return 0;
}
13
#include <stdio.h>
#include <string.h>
typedef struct {
char name[20];
int score;
} student;
void fun(student s[], student *b) {
int max_score = 0;
student *max_student = NULL;
// 找到最高分数的学生
for (int i = 0; i < 5; i++) {
if (s[i].score > max_score) {
max_score = s[i].score;
max_student = &s[i];
}
}
// 将最高分数的学生指针通过b返回
memcpy(b, max_student, sizeof(student));
}
int main() {
student s[5] = {
{"张三", 85},
{"李四", 90},
{"王五", 80},
{"马六", 90},
{车二愣子", 45}
};
student b;
fun(s, &b);
printf("最高分数的学生是:%s,成绩是 %d\n", b.name, b.score);
return 0;
}
30
#include <stdio.h>
double fun(double *a, int n) {
double max = *a, min = *a;
double sum = 0.0;
// 找到最高分和最低分
for (int i = 0; i < n; i++) {
if (a[i] > max) {
max = a[i];
}
if (a[i] < min) {
min = a[i];
}
sum += a[i];
}
// 减去最高分和最低分,求平均值
double avg = (sum - max - min) / (double)(n - 2);
return avg;
}
int main() {
double a[] = {80.5, 90.0, 70.2, 85.5, 95.8};
int n = sizeof(a) / sizeof(double);
double result = fun(a, n);
printf("平均分是:%lf\n", result);
return 0;
}
以下完全是我自己写的,尽量最简洁的代码,仅供参考,若有疑问请提出来!谢谢!
///////////////////////////////////////////////////////////////
14.题
#include<stdio.h>
#define N 5
typedef struct
{
long num;
double score;
} stu;
int fun(stu * s, stu * b, int n)
{
int num = 0;
double max = s[0].score;
for (int i = 0; i < n; i++)
if (s[i].score > max)
{
max = s[i].score;
}
for (int i = 0; i < n; i++)
if (s[i].score == max)
{
b[num++] = s[i];
}
return num;
}
int main(void)
{
stu a[N] = {
1001, 98.8,
1002, 90.3,
1003, 86.8,
1004, 99.0,
1005, 99.0
};
stu b[N];
printf("最高分学生人数:%d\n", fun(a, b, N));
return 0;
}
///////////////////////////////////////////////////////////////
13.题
#include<stdio.h>
// #define N 5
typedef struct
{
long num;
double score;
} stu;
stu fun(stu * a, int n)
{
int index = 0;
double max = a[0].score;
for (int i = 0; i < n; i++)
if (a[i].score > max)
{
max = a[i].score;
index = i;
}
return a[index];
}
int main(void)
{
stu stud[5] = {
1001, 98.8,
1002, 90.3,
1003, 86.8,
1004, 99.3,
1005, 99.0
};
stu max = fun(stud,5);
printf("最高分学生信息:\n学号%ld\n分数%.1lf\n", max.num, max.score);
return 0;
}
///////////////////////////////////////////////////////////////
41.题
#include<stdio.h>
double fun(int n)
{
double s = 0.0, t;
for (int i = 1; i <= n; i++)
{
t = 0.0;
for (int j = 1; j <= i; j++)
{
t += j;
}
s += 1.0 / t;
}
return s;
}
int main(void)
{
int n;
scanf("%d", &n);
printf("%lf\n", fun(n));
return 0;
}
///////////////////////////////////////////////////////////////
30.题
#include<stdio.h>
#define N 10
double fun(double *a, int n)
{
double max = a[0], min = a[0], sum = 0.0;
for (int i = 0; i < n; i++)
{
if (a[i] > max)
max = a[i];
if (a[i] < min)
min = a[i];
sum += a[i];
}
return (sum - max - min) / (n - 2);
}
int main(void)
{
double a[N];
for (int i = 0; i < N; i++)
scanf("%lf", &a[i]);
printf("%lf\n", fun(a, N));
return 0;
}
///////////////////////////////////////////////////////////////