OJ练习题——jlh吃水果
Description:
jlh很喜欢吃水果,苹果是他最喜欢的,其次是梨。他天天想着吃水果,竟然感动了女娲大神,女娲大神给了他n个篮子,让他选择其中的m个(m<=n)个篮子。每个篮子里有a个苹果和b个梨。请你们帮jlh选择篮子吧。
Input:
输入一个t(t<=10),表示有t组测试数据,再输入n和m(0=<m<=n<=100000),接下来的n行,输入a和b表示苹果和梨的数量。
Output:
按jlh选择的顺序(先选苹果多的,苹果数量相同选梨多的,两者相同选序号小的)篮子的序号(1-n),m个数用空格隔开。
Sample Input:
2
2 1
2 0
1 4
3 2
3 4
2 6
3 5
Sample Output:
1
3 1
理解和解释
代码
#include <stdio.h>
typedef struct LanZi{
int apple;
int pear;
int flag; //flag = 1时还存在
}LanZi;
int main() {
int t, n, m, count;
LanZi lz[100000];
scanf("%d",&t);
//t组测试数据
for(int T=1;T<=t;T++)
{
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d %d", &lz[i].apple, &lz[i].pear);
lz[i].flag = 1;
}
count = 0;
for(int j = 1; j <= m; j++) {
int maxIndex;
for(int d = 0; d <= n-1; d++){
if( lz[d].flag == 1 ){
maxIndex = d;
break;
}
}
for (int i = 1; i < n; i++) {
if (lz[maxIndex].apple < lz[i].apple && lz[i].flag == 1) {
maxIndex = i;
} else if (lz[maxIndex].apple == lz[i].apple && lz[i].flag == 1) {
if (lz[maxIndex].pear < lz[i].pear) {
maxIndex = i;
}
}
}
//输出最大的, 并标志0
if(count == 0){
printf("%d", maxIndex + 1);
lz[maxIndex].flag = 0;
count = count + 1;
} else {
printf(" %d", maxIndex + 1);
lz[maxIndex].flag = 0;
count++;
}
}
printf("\n");
}
return 0;
}