回傳
1004 成績排名 (20point(s))
讀入 n(>0)名學生的姓名、學號、成績,分別輸出成績最高和成績最低學生的姓名和學號。
輸入格式:
每個測驗輸入包含 1 個測驗用例,格式為
第 1 行:正整數 n
第 2 行:第 1 個學生的姓名 學號 成績
第 3 行:第 2 個學生的姓名 學號 成績
... ... ...
第 n+1 行:第 n 個學生的姓名 學號 成績
其中姓名和學號均為不超過 10 個字符的字串,成績為 0 到 100 之間的一個整數,這里保證在一組測驗用例中沒有兩個學生的成績是相同的。
輸出格式:
對每個測驗用例輸出 2 行,第 1 行是成績最高學生的姓名和學號,第 2 行是成績最低學生的姓名和學號,字串間有 1 空格。
輸入樣例:
3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95
輸出樣例:
Mike CS991301
Joe Math990112
#include<stdio.h>
struct student{
char name[10];
char code[10];
int score;
};
int main(void)
{
int student_num;
scanf("%d",&student_num);
struct student students[student_num];
int i;
struct student first_student = {"","",-1};
struct student last_student;
for (i=0;i<student_num;i++)
{
scanf("%s %s %d",students[i].name,students[i].code,&students[i].score);
if (students[i].score > first_student.score)
first_student = students[i];
if (i==0) last_student = first_student;
else if (students[i].score < last_student.score)
last_student = students[i];
else
;
}
printf("%s %s\n",first_student.name,first_student.code);
printf("%s %s",last_student.name,last_student.code);
return 0;
}
Submit Time Status Score Problem Compiler Run Time User
3/13/2020, 11:37:30
Partially Accepted
2 1004 C (gcc) 4 ms lu_xianche
Case Result Run Time Memory
0
Wrong Answer
2 ms 256 KB
1
Accepted
4 ms 256 KB
2
Wrong Answer
4 ms 384 KB
uj5u.com熱心網友回復:
int student_num;scanf("%d",&student_num);
struct student students[student_num];
C語言不支持這樣定義陣列,必須給定一個常量值
uj5u.com熱心網友回復:
#include<stdio.h>
#include <stdlib.h>
struct student{
//char name[10];
//char code[10];
char name[32];
char code[32];
int score;
};
int main(void)
{
int student_num;
scanf("%d",&student_num);
struct student *students = (struct student *)malloc(sizeof(struct student) * student_num);
if (!students)
exit(0);
int i;
struct student first_student = {"","",-1};
struct student last_student;
for (i=0;i<student_num;i++)
{
scanf("%s %s %d",students[i].name,students[i].code,&students[i].score);
if (students[i].score > first_student.score)
first_student = students[i];
if (i==0)
last_student = first_student;
else if (students[i].score < last_student.score)
last_student = students[i];
//else
// ;
}
printf("%s %s\n",first_student.name,first_student.code);
printf("%s %s\n",last_student.name,last_student.code);
return 0;
}
供參考~
C語言不支持變長陣列,另外,name, code對于測驗用例而言,已經越界了。所以建議增加陣列長度(name, code)
uj5u.com熱心網友回復:
變長陣列,可以支持的轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/114591.html
標籤:C語言
上一篇:使用DuiLib出現的鏈接錯誤
下一篇:C語言記憶體磁區
