小科同学学习了数组,认真进行编程练习,顺利完成了老师布置的20道课外编程题后,他想起了科比20年的职业生涯数据,查阅数据如下: No Season Age TRB AST PTS 1 1996-97 18 1.9 1.3 7.6 2 1997-98 19 3.1 2.5 15.4 3 1998-99 20 5.3 3.8 19.9 4 1999-00 21 6.3 4.9 22.5 5 2000-01 22 5.9 5 28.5 6 2001-02 23 5.5 5.5 25.2 7 2002-03 24 6.9 5.9 30 8 2003-04 25 5.5 5.1 24 9 2004-05 26 5.9 6 27.6 10 2005-06 27 5.3 4.5 35.4 11 2006-07 28 5.7 5.4 31.6 12 2007-08 29 6.3 5.4 28.3 13 2008-09 30 5.2 4.9 26.8 14 2009-10 31 5.4 5 27 15 2010-11 32 5.1 4.7 25.3 16 2011-12 33 5.4 4.6 27.9 17 2012-13 34 5.6 6 27.3 18 2013-14 35 4.3 6.3 13.8 19 2014-15 36 5.7 5.6 22.3 20 2015-16 37 3.7 2.8 17.6 Career 5.2 4.7 25 请你帮助小科分析一下,职业生涯的数据需要你根据给定的数据在主函数里初始化,输入两个不同的赛季序号a和b(均已满足 1<=a<=20,1<=b<=20),输出范围内的最高得分和次高得分以及对应的赛季名、年龄、得分、助攻、篮板数据。 给定原始数据格式 {1,"1996-97",18,1.9,1.3,7.6}, {2,"1997-98",19,3.1,2.5,15.4}, {3,"1998-99",20,5.3,3.8,19.9}, {4,"1999-00",21,6.3,4.9,22.5}, {5,"2000-01",22,5.9,5,28.5}, {6,"2001-02",23,5.5,5.5,25.2}, {7,"2002-03",24,6.9,5.9,30}, {8,"2003-04",25,5.5,5.1,24}, {9,"2004-05",26,5.9,6,27.6}, {10,"2005-06",27,5.3,4.5,35.4}, {11,"2006-07",28,5.7,5.4,31.6}, {12,"2007-08",29,6.3,5.4,28.3}, {13,"2008-09",30,5.2,4.9,26.8}, {14,"2009-10",31,5.4,5,27}, {15,"2010-11",32,5.1,4.7,25.3}, {16,"2011-12",33,5.4,4.6,27.9}, {17,"2012-13",34,5.6,6,27.3}, {18,"2013-14",35,4.3,6.3,13.8}, {19,"2014-15",36,5.7,5.6,22.3}, {20,"2015-16",37,3.7,2.8,17.6}
时间: 2023-08-16 07:10:04 浏览: 223
好的,我可以帮你解决这个问题。你可以把数据存储在一个结构体数组中,然后编写一个函数来查找指定范围内的最高得分和次高得分以及对应的赛季名、年龄、得分、助攻、篮板数据。以下是一个可能的代码实现:
```c
#include <stdio.h>
#include <string.h>
#define MAX_SEASONS 20
typedef struct {
int no;
char season[8];
int age;
float trb;
float ast;
float pts;
} SeasonData;
void find_max_scores(SeasonData *data, int a, int b, float *max1, float *max2) {
int i;
char max1_season[8], max2_season[8];
int max1_age, max2_age;
float max1_trb, max1_ast, max1_pts;
float max2_trb, max2_ast, max2_pts;
// 初始化
*max1 = data[a-1].pts;
strcpy(max1_season, data[a-1].season);
max1_age = data[a-1].age;
max1_trb = data[a-1].trb;
max1_ast = data[a-1].ast;
max1_pts = data[a-1].pts;
*max2 = -1.0;
strcpy(max2_season, "");
max2_age = 0;
max2_trb = 0.0;
max2_ast = 0.0;
max2_pts = 0.0;
// 查找最高得分和次高得分
for (i = a; i <= b; i++) {
if (data[i-1].pts > *max1) {
*max2 = *max1;
strcpy(max2_season, max1_season);
max2_age = max1_age;
max2_trb = max1_trb;
max2_ast = max1_ast;
max2_pts = max1_pts;
*max1 = data[i-1].pts;
strcpy(max1_season, data[i-1].season);
max1_age = data[i-1].age;
max1_trb = data[i-1].trb;
max1_ast = data[i-1].ast;
max1_pts = data[i-1].pts;
} else if (data[i-1].pts > *max2) {
*max2 = data[i-1].pts;
strcpy(max2_season, data[i-1].season);
max2_age = data[i-1].age;
max2_trb = data[i-1].trb;
max2_ast = data[i-1].ast;
max2_pts = data[i-1].pts;
}
}
// 输出结果
printf("最高得分:%.1f(%s,年龄:%d,篮板:%g,助攻:%g)\n", *max1, max1_season, max1_age, max1_trb, max1_ast);
printf("次高得分:%.1f(%s,年龄:%d,篮板:%g,助攻:%g)\n", *max2, max2_season, max2_age, max2_trb, max2_ast);
}
int main() {
SeasonData data[MAX_SEASONS] = {
{1,"1996-97",18,1.9,1.3,7.6},
{2,"1997-98",19,3.1,2.5,15.4},
{3,"1998-99",20,5.3,3.8,19.9},
{4,"1999-00",21,6.3,4.9,22.5},
{5,"2000-01",22,5.9,5,28.5},
{6,"2001-02",23,5.5,5.5,25.2},
{7,"2002-03",24,6.9,5.9,30},
{8,"2003-04",25,5.5,5.1,24},
{9,"2004-05",26,5.9,6,27.6},
{10,"2005-06",27,5.3,4.5,35.4},
{11,"2006-07",28,5.7,5.4,31.6},
{12,"2007-08",29,6.3,5.4,28.3},
{13,"2008-09",30,5.2,4.9,26.8},
{14,"2009-10",31,5.4,5,27},
{15,"2010-11",32,5.1,4.7,25.3},
{16,"2011-12",33,5.4,4.6,27.9},
{17,"2012-13",34,5.6,6,27.3},
{18,"2013-14",35,4.3,6.3,13.8},
{19,"2014-15",36,5.7,5.6,22.3},
{20,"2015-16",37,3.7,2.8,17.6}
};
int a, b;
float max1, max2;
printf("请输入赛季序号范围(1-20):");
scanf("%d %d", &a, &b);
find_max_scores(data, a, b, &max1, &max2);
return 0;
}
```
这个程序首先定义了一个结构体 `SeasonData` 来存储每个赛季的数据。然后在 `main` 函数中初始化了这个结构体数组 `data`。接着调用了 `find_max_scores` 函数来查找指定范围内的最高得分和次高得分。这个函数接受一个指向 `data` 数组的指针,以及两个整数 `a` 和 `b` 来指定查找范围。函数使用两个 `float` 类型的指针来返回最高得分和次高得分。函数内部使用一个循环来遍历指定范围内的所有赛季,查找最高得分和次高得分,并记录相应的赛季名、年龄、得分、助攻、篮板数据。最后输出结果。
注意,这个程序在输入赛季序号范围时,要求用户输入两个整数,用空格隔开。例如,如果要查找第 5 到第 10 个赛季的数据,用户应该输入“5 10”。
阅读全文