[原题链接]https://pintia.cn/problem-sets/994805260223102976/problems/994805321640296448
emmm,这题比较简单,也没啥好说的,字符串掌握的不好 主要是总结一下各种输入方法吧
我用的结构体(其实也有点需要注意的地方 name ID的大小必须至少是11要为\0留出空间 总之往大里设置就好)
第一种:
#include <iostream>
#include <cstdio>
const int Maxnum=10000;
struct Student
{
char name[15]; //字符串存储格式
char ID[15];
int score;
}Student[10000];
int main()
{
int n,i=0;
int score,Minscore=100,Maxscore=-1;
int min=0,max=0;
scanf("%d",&n);
while(n--)
{
scanf("%s%s%d",Student[i].name,Student[i].ID,&Student[i].score);
if(Student[i].score>Maxscore)
{
Maxscore=Student[i].score;
max=i;
}
if(Student[i].score<Minscore)
{
Minscore=Student[i].score;
min=i;
}
i++;
}
printf("%s %s\n",Student[max].name,Student[max].ID);
printf("%s %s",Student[min].name,Student[min].ID);
return 0;
}
第二种:
const int Maxnum=10000;
struct Student
{
char name[15]; //字符串存储格式
char ID[15];
int score;
};
int main()
{
struct Student Stu[Maxnum];
scanf("%s%s%d",Stu[i].name,Stu[i].ID,&Stu[i].score);
}
第三种:
typedef struct{
char name[15];
char num[15];
int score;
} Student; //Student 现在就变成了一种类型 类似int,double这种
int main()
{
Student stu[n];
scanf("%s%s%d",stu[i].name,stu[i].num,&stu[i].score);
}
第四种:c++ string输入(学习大神 原贴:https://www.liuchuo.net/archives/516)
#include <iostream>
using namespace std;
int main() {
int n, max = -1, min = 101, score;
cin >> n;
string maxname, minname, maxnum, minnum, name, num;
for (int i = 0; i < n; i++) {
cin >> name >> num >> score;
if (max < score) {
max = score;
maxname = name;
maxnum = num;
}
if (min > score) {
min = score;
minname = name;
minnum = num;
}
}
cout << maxname << " " << maxnum << endl << minname << " " << minnum;
return 0;
}
hhh 大神代码好好看 字符串用c++输入输出效率好高学到了学到了