zoukankan      html  css  js  c++  java
  • Greatest Number

    Greatest Number

    Problem Description

      Saya likes math, because she think math can make her cleverer.
      One day, Kudo invited a very simple game:
      Given N integers, then the players choose no more than four integers from them (can be repeated) and add them together. Finally, the one whose sum is the largest wins the game. It seems very simple, but there is one more condition: the sum shouldn’t larger than a number M.
      Saya is very interest in this game. She says that since the number of integers is finite, we can enumerate all the selecting and find the largest sum. Saya calls the largest sum Greatest Number (GN). After reflecting for a while, Saya declares that she found the GN and shows her answer.
      Kudo wants to know whether Saya’s answer is the best, so she comes to you for help.
    Can you help her to compute the GN?

    Input

      The input consists of several test cases.
      The first line of input in each test case contains two integers N (0<N≤1000) and M(0<M≤ 1000000000), which represent the number of integers and the upper bound.
      Each of the next N lines contains the integers. (Not larger than 1000000000)
    The last case is followed by a line containing two zeros.

    Output

      For each case, print the case number (1, 2 …) and the GN.
    Your output format should imitate the sample output. Print a blank line after each test case.

    Sample Input

    2 10
    100
    2
    0 0

    Sample Output

    Case 1:8

     

    分析:

    第一届山东省赛题。

    本来想深搜的,剪不过去。定义一个数组,存放每个数和任意两个数之和,从在数组任意抽两个数,可以表示任意的1,2,3,4个数之和。找出最大的。

    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 1000+10;
    
    int a[maxn];
    vector<int> q;
    
    int main(){
        int n, kase = 0;
        int m, ans;
        while(scanf("%d%d", &n, &m) == 2){
            if(n == 0 && m == 0) break;
            ans = 0;
    
            a[0] = 0;
            for(int i=1; i<=n; i++){
                scanf("%d", &a[i]);
            }
    
            q.clear();
    
            for(int i=0; i<=n; i++){
                for(int j=0; j<=n; j++){
                    if(a[i] +a[j]<=m) q.push_back(a[i]+a[j]);
                }
            }
    
            sort(q.begin(), q.end());
    
            int sz = q.size(), ed = sz-1, st, pos = sz-1, mid;
            for(int i=0; i<sz; i++){
                st = i; ed = pos;
                while(st <= ed){
                    mid = (st+ed)/2;
                    if(q[i]+q[mid] == m) { ans = m; break; }
                    else if(q[i] + q[mid] > m) ed = mid-1;
                    else{
                        st = mid+1;
                        if(q[i]+q[mid] > ans){
                            ans = q[i] + q[mid];
                        }
                    }
                }
                if(st <= ed) break;
            }
    
            printf("Case %d: %d\n\n", ++kase, ans);
        }
    
    
        return 0;
    }
  • 相关阅读:
    【笔记】xml文件读写
    创业唯一不需要的是金钱
    关于阻焊层和助焊层的理解
    UNIX net
    一种方便调试的打印语句宏定义
    C语言指针一种容易错误使用的方法
    文件操作
    MPEG文件格式
    指针在函数间传递实质
    如何查看静态库内容 Unix/Linux
  • 原文地址:https://www.cnblogs.com/tanhehe/p/3086076.html
Copyright © 2011-2022 走看看