求一个数据结构方面的问题怎么解决? 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

c as acm
-1

2个回答

0
#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;
}
0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!