PAT Basic Level 1004





1
#include <stdio.h> 2 struct student 3 { 4 int score; 5 char nameChar[11]; 6 char stunoChar[11]; 7 }; 8 int main () 9 { 10 struct student student[1000]; //不规范,并没有告诉你数据大小。 11 int count = 0; //测试数量 12 scanf("%d",&count); 13 int i = count-1; 14 while (i>=0) 15 { 16 scanf("%s",&student[i].nameChar); 17 scanf("%s",&student[i].stunoChar); 18 scanf("%d",&student[i].score); //代码冗余了,scanf特点是可以一次输入多个数据可以合并成一句scanf("%s %s %d",&student[i].nameChar,&student[i].stunoChar,&student[i].score) 19 i--; 20 } 21 int max = student[0].score; 22 int min = student[0].score; 23 int maxNo = 0; 24 int minNo = 0; 25 i = count-1; 26 while (i >= 0) 27 { 28 if(student[i].score>max) 29 { 30 max = student[i].score; 31 maxNo = i; 32 } 33 if(student[i].score<min) 34 { 35 min = student[i].score; 36 minNo = i; 37 } 38 i--; 39 } 40 printf("%s %s ",student[maxNo].nameChar,student[maxNo].stunoChar); 41 printf("%s %s",student[minNo].nameChar,student[minNo].stunoChar); 42 return 0; 43 44 45 }

 此题其实解得并不规范,不应用数组保存输入的数据,因为在后续的过程中并没有用到数据,只需输入时直接判断大小就行了。因此以后的解题中,不要直接看见数据直接保存起来。

原文地址:https://www.cnblogs.com/Ponytai1/p/5975432.html