输入姓名和成绩并排序输出

输入姓名和成绩并排序输出

#include
using namespace std;
typedef struct {
	string mingZi;
	int chengJi;
}number;

int main()
{
	int i = 0;
	number students[1000];
	cin >> students[i].mingZi >> students[i].chengJi;
	while (students[i].mingZi[0] != '!') {//输入'!'终止输入
		//输入姓名成绩
		cin >> students[++i].mingZi >> students[i].chengJi;
		//进行排序
		if (i > 0) {
			number t;
			for (int j = i; j > 0; j--) 
				if (students[j].chengJi > students[j - 1].chengJi) {
					t = students[j];
					students[j] = students[j - 1];
					students[j - 1] = t;
				}
		}
	}
	//第i个是存放终止输入'!'和无意义成绩,不输出
	for (int j = 0; j < i; j++)
		cout << students[j].mingZi << "  " << students[j].chengJi << endl;

	return 0;
}

输入:

liming 80
lihua 60
wanggang 50
zhangyan 90
zhangsan 100
! 11

输出:

zhangsan  100
zhangyan  90
liming  80
lihua  60
wanggang  50

你可能感兴趣的:(考研笔记,c++)