A1062 Talent and Virtue (25 分)

  1. 16分代码
#include <iostream>
#include <algorithm> 
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

const int MAX = 100010;

struct user{
    
    
	string id;
	int vir;
	int ten;
}sage[MAX], nobleman[MAX], fool[MAX], other[MAX];

int low, high;

bool cmp(user a, user b){
    
    
	if((a.vir+a.ten) != (b.vir+b.ten)){
    
    
		return (a.vir+a.ten) > (b.vir+b.ten);
	} else if(((a.vir+a.ten) == (b.vir+b.ten)) && (a.vir != b.vir)){
    
    
		return a.vir > b.vir;
	} else {
    
    
		a.id < b.id;
	}
}

int main(int argc, char** argv) {
    
    

	int n, count=0;
	cin >> n >> low >> high;
	string id;
	int vir, ten;
	int i = 0, j = 0, k = 0, m = 0;
	while(n--){
    
    
		cin >> id >> vir >> ten;
		if(vir >= high && ten >= high){
    
     //德才兼备 
			sage[i].id = id;
			sage[i].vir = vir;
			sage[i++].ten = ten;
			count++; 
		} else if(vir >= high && ten < high && ten >=low){
    
     //德大于80 且 德大于才 
			nobleman[j].id = id;
			nobleman[j].vir = vir;
			nobleman[j++].ten = ten;
			count++; 
		} else if(vir >=low && ten >=low && vir > ten){
    
     //德不大于80 且 德大于才 
			fool[k].id = id;
			fool[k].vir = vir;
			fool[k++].ten = ten;
			count++; 
		} else if(vir >=low && ten >=low && vir <= ten ){
    
     //得不大于80 且 才大于德 
			other[m].id = id;
			other[m].vir = vir;
			other[m++].ten = ten;
			count++; 
		}
	}
	
	sort(sage, sage+i, cmp);
	sort(nobleman, nobleman+j, cmp);
	sort(fool, fool+k, cmp);
	sort(other, other+m, cmp);
	cout << count << endl;

	for(int q = 0; q < i; q++){
    
    
		cout << sage[q].id << " " << sage[q].vir << " "<< sage[q].ten << endl ;
	}
	
	for(int q = 0; q < j; q++){
    
    
		cout << nobleman[q].id << " " << nobleman[q].vir << " "<< nobleman[q].ten << endl ;
	}
	
	for(int q = 0; q < k; q++){
    
    
		cout << fool[q].id << " " << fool[q].vir << " "<< fool[q].ten << endl ;
	}
	
	for(int q = 0; q < m; q++){
    
    
		cout << other[q].id << " " << other[q].vir << " "<< other[q].ten << endl ;
	}
	
	 
	return 0;
}
  1. 20分代码
#include <iostream>
#include <algorithm> 
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

const int MAX = 100010;

struct user{
    
    
	string id;
	int de;
	int cai;
	int sum;
	int flag; //1-5类人 
}stu[MAX];


bool cmp(user a, user b){
    
    
	if(a.flag!=b.flag){
    
    
		return a.flag < b.flag; //类别小的在前 
	} else if(a.sum != b.sum){
    
    
		return a.sum > b.sum; //分数大的在前 
	} else if(a.de != b.de){
    
    
		return a.de > b.de; //德高的在前 
	} else {
    
    
		return a.id < b.id;
	}
} 

int main(){
    
    
	int n, low, high;
	cin >> n >> low >> high;
	int m = n; //m为及格人数
	for(int i = 0; i < n; i++){
    
    
		cin >> stu[i].id >> stu[i].de >> stu[i].cai;
		stu[i].sum = (stu[i].de + stu[i].cai);
		if(stu[i].de < low || stu[i].cai < low){
    
    
			stu[i].flag = 5;
			m--;
		}
		else if(stu[i].de >= high && stu[i].cai >= high){
    
     //d>80 c>80 d>c 
			stu[i].flag = 1;
		} else if(stu[i].de >= high && stu[i].cai < high){
    
     //d>80 c<80 d>c
			stu[i].flag = 2;
		} else if(stu[i].de < high && stu[i].cai < high && stu[i].de >= stu[i].cai){
    
     //d<80 c <80 d > c`
			stu[i].flag = 3;
		} else {
    
    
			stu[i].flag = 4;
		}
	} 
	
	sort(stu, stu+n, cmp);
	
	cout << m << endl;
	
	for(int i = 0; i < m; i++){
    
    
		cout << stu[i].id << " " << stu[i].de << " "<< stu[i].cai << endl ;
	}
}

猜你喜欢

转载自blog.csdn.net/alovelypeach/article/details/114282329