C语言程序设计-1004 成绩排名

借鉴:

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




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值