输出字符串

输入:每个学生一行,先输入学号,接着是各科成绩(0~5),科目门次不定,中间用空格隔开。
输出:每个学生的平均分(一位小数)从大到小排列输出,挂科(分数<=2)2门以上的同学按门次从小到大输出。
样例:
Input:
1, 4, 5, 4, 5
2, 5
3, 2, 2, 1
4, 2, 1, 3
Output:
平均分:
2,  5
1, 4.5
4, 2
3, 1.7
挂科:
4, 2
3, 3


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
const int MAXN = 100;


using namespace std;


struct student {
	int ID;
	float average;
	int numFail;
};


student stu[100];


bool compare_aver(student stu1, student stu2){
	if(stu1.average != stu2.average)
		return (stu1.average > stu2.average);
}


bool compare_failed(student stu1, student stu2){
	if(stu1.numFail != stu2.numFail)
		return (stu1.numFail > stu2.numFail);
}


int main(){


	string str;
	freopen("F:\\input.txt", "r", stdin);
	int count = 0;
	while(cin>>str){
//		int ID = str[0];
		student stu1;


		int sum = 0, numfailed = 0;
		for(int i = 2; i < str.length(); i=i+2){
			int number = stoi(str.substr(i, 1));
			if(number <= 2) numfailed++;
			sum += number;
		}
		float aver = (float)sum*2/(str.length()-1);
		stu1.ID = str[0] - '0';
		stu1.average = aver;
		stu1.numFail = numfailed;
		stu[count] = stu1;
		count ++;
	}


	sort(stu, stu + count, compare_aver);
	for(int i = 0; i < count; ++i){
		cout<< stu[i].ID<< "," << stu[i].average<< endl;
	}


	sort(stu, stu + count, compare_failed);
	for(int i = 0; i < count; ++i){
		if(stu[i].numFail != 0)
					cout<< stu[i].ID<< "," << stu[i].numFail<< endl;
	}


	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值