2018-11-26 09:03

# 求一个数据结构方面的问题怎么解决？ Difficulty control

Problem Description
LL is very sensitive to the difficulty of ACM problems. He can assign each problem a positive integer from [1,10000000] to denote its difficulty ( although he has only ACed about 1000 problems ^_^ ). Now, there are N problems in his mind. He want to choose some of them to hold a contest and the total difficulty of all problems in the contest is as near as possible to M.

Input
For each case, the first line is two positive integers N and M (1<=N<=26). Followed by N lines. Each line contains the problem's ID ( a single capital letter ) and its difficulty.

Output
For each case, ouput how many and which problems he should choose in the format of the sample. If more than one combinations of problems reach the request, output the one which comes earliest in lexicography order.

Sample Input
3 10
A 4
B 12
C 8

Sample Output
2
A C

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答

#### 2条回答默认 最新

• blownewbee 2019-01-31 23:47
已采纳
• mkchiny 2018-11-29 01:38
``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_N 26

int main(void)
{
int problem_num = 0;
char problem_id[MAX_N] = {0};
int problem_match[MAX_N] = {0};
int problem_difficulty = 0;
int problem_M = 0;
int count = 0;
int i = 0;

setbuf(stdin, NULL); //clear input

scanf("%d %d", &problem_num, &problem_M);
setbuf(stdin, NULL); //clear \n
if(problem_num < 1 || problem_num > MAX_N)
{
printf("problem num must in 1~%d\n", MAX_N);
return -1;
}

for(i = 0; i < problem_num; i++)
{
scanf("%c %d", &problem_id[i], &problem_difficulty);
setbuf(stdin, NULL); //clear \n
if(problem_difficulty <= problem_M)
{
problem_match[i] = 1;
count++;
}
}

printf("%d\n", count);
for(i = 0; i < problem_num; i++)
{
if(problem_match[i])
printf("%c ", problem_id[i]);
}
printf("\n");

return 0;
}
``````
点赞 打赏 评论