借鉴:
https://blog.csdn.net/qq_40840749/article/details/81780814
https://blog.csdn.net/alike_meng/article/details/86751319
用 c 语言产生段错误,不明原因。
#1. 定义变量
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
cin >> n;
string min_name, min_number, max_name, max_number, name, number;
int min_score = 101, max_score = -1, score;
for(int i = 0; i < n; i++){
cin >> name >> number >> score;
if(score < min_score){
min_name = name;
min_number = number;
min_score = score;
}
if(score > max_score){
max_name = name;
max_number = number;
max_score = score;
}
}
cout << max_name << " " << max_number << endl;
cout << min_name << " " << min_number << endl;
return 0;
}
2. 结构体01
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
struct Student{
char name[10],id[10];
int score;
};
int main(){
int n;
scanf("%d", &n);
Student stu[n];
int max = -1, min = 101;
int max_stu = 0, min_stu = 0;
for(int i = 0; i < n; i++){
scanf("%s %s %d", stu[i].name, stu[i].id, stu[i].score);
if(stu[i].score > max){
max = stu[i].score;
max_stu = i;
}
if(stu[i].score < min){
min = stu[i].score;
min_stu = i;
}
}
printf("%s %s\n", stu[max_stu].name, stu[max_stu].id);
printf("%s %s\n", stu[min_stu].name, stu[min_stu].id);
return 0;
}
2.结构体02
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name, number;
int score;
};
int main()
{
student s[10000];
int n,max=-1,min=101,max_position,min_position;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> s[i].name >> s[i].number >> s[i].score;
if (s[i].score > max)
{
max = s[i].score;
max_position = i;
}
if (s[i].score < min)
{
min = s[i].score;
min_position = i;
}
}
cout << s[max_position].name << " " << s[max_position].number<<endl;
cout << s[min_position].name << " " << s[min_position].number << endl;
return 0;
}
————————————————
版权声明:本文为CSDN博主「alike_meng」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/alike_meng/article/details/86751319