Untitled
raw download clone
TEXT
views 30
,
size 2370 b
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct a_node {
	char name[25];
	int a_productivity;
	int b_productivity;
	int use;
}firm;
void compare_ans(const void* a, const void* b) {
	firm** pa = (const firm**)a;
	firm** pb = (const firm**)b;
	return strcmp((*pa)->name, (*pb)->name);
}
void compare_a(const void* a, const void* b)
{
	firm** pa = (const firm**)a;
	firm** pb = (const firm**)b;
	return (*pb)->a_productivity - (*pa)->a_productivity;
}
void compare_b(const void* a, const void* b)
{
	firm** pa = (const firm**)a;
	firm** pb = (const firm**)b;

	return (*pb)->b_productivity - (*pa)->b_productivity;
}
int main()
{
	int n, a, b;
	firm* all_firm;
	firm** firm_1;
	firm** firm_2;
	firm** firm_ans;
	scanf("%d%d%d", &n, &a, &b);
	all_firm = (firm*)malloc(sizeof(firm) * n);
	firm_1 = (firm**)malloc(sizeof(firm*) * n);
	firm_2 = (firm**)malloc(sizeof(firm*) * n);
	firm_ans = (firm**)malloc(sizeof(firm*) * a);
	for (int i = 0; i < n; i++) {
		scanf("%s%d%d", all_firm[i].name, &all_firm[i].a_productivity, &all_firm[i].b_productivity);
		firm_1[i] = &all_firm[i];
		firm_2[i] = &all_firm[i];
	}
	
	qsort(firm_1, n, sizeof(firm*), compare_a);
	qsort(firm_2, n, sizeof(firm*), compare_b);
	int a_s = a;
	int b_s = b;
	for (int i = 0; i < a; i++) {
		firm_1[i]->use = 1; //設定是要生產哪一個車子  1--a 2--b
	}
	for (int i = 0; i < b; i++) {
		if (firm_2[i]->use == 1) { //比較看哪一個方式對整體profit較好
			if (firm_2[i]->a_productivity + firm_2[b_s]->b_productivity < \
				firm_1[a_s]->a_productivity + firm_2[i]->b_productivity) {
				firm_2[i]->use = 2;
				firm_1[a_s]->use = 1;
				a_s++;
			}  
			else {
				firm_2[b_s]->use = 2;
				b_s++;
			}
		}
		else {
			firm_2[i]->use = 2;
		}
	}
	int x = 0; //將汽車a的新增到一個陣列裡頭去排序
	for (int i = 0; i < n; i++){
		if(x>=a) break;
		if (firm_1[i]->use == 1) {
			firm_ans[x] = firm_1[i];
			x++;
		}
	}
	qsort(firm_ans, a, sizeof(firm*), compare_ans);
	for (int i = 0; i < a; i++) {
		printf("%s\n", firm_ans[i]->name);
	}
	free(all_firm);
	free(firm_1);
	free(firm_2);
	free(firm_ans);
	return 0;
}
/*
10 3 4
TOYOTA 10 100
GM 90 90
FORD 40 80
Volkswagen 50 70
Daimler 30 60
Honda 60 50
Nissan 70 40
PEUGEOT 80 30
FIAT 20 20
BMW 100 10
*/
close fullscreen
Login or Register to edit or fork this paste. It's free.