1 条题解

  • 0
    @ 2025-4-12 21:36:06

    C :

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	int T, M, result[1024], time, price, i;
    	
    	while (EOF != scanf("%d%d", &T, &M)){
    		memset(result, 0, sizeof(result));
    		while (M--){
    			scanf("%d%d", &time, &price);
    			if (time > T){
    				continue;
    			}
    			for (i = T; i >= time; i--){
    				if (result[i-time]+price > result[i]){
    					result[i] = result[i-time]+price;
    				}else{
    					result[i] = result[i];
    				}
    			}
    		}
    		printf("%d\n", result[T]);
    	}
    	return 0;
    }
    
    

    C++ :

    #include <stdio.h>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int V = 100 + 50;
    int t, n, dp[V];
    int main() {
      while(~scanf("%d%d", &t, &n)) {
        memset(dp, 0, sizeof(dp));
        while(n--) {
          	int a, b;
          	scanf("%d%d", &a, &b);
          	for(int j = t; j >= a; --j)
                dp[j] = max(dp[j], dp[j - a] + b);
        }
        printf("%d\n", dp[t]);
      }
    }
    
    • 1

    信息

    ID
    862
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者