• UVA10487(二分)


    Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct numbers from the set, which is closest to the query number.
    Input
    Input contains multiple cases. Each case starts with an integer n (1 < n ≤ 1000), which indicates, how many numbers are in the set of integer. Next n lines contain n numbers. Of course there is only one number in a single line. The next line contains a positive integer m giving the number of queries, 0 < m < 25. The next m lines contain an integer of the query, one per line. Input is terminated by a case whose n = 0. Surely, this case needs no processing.
    Output
    Output should be organized as in the sample below. For each query output one line giving the query value and the closest sum in the format as in the sample. Inputs will be such that no ties will occur.
    Sample Input
    5 3 12 17 33 34 3 1 51 30 3 1 2 3 3 1 2 3 3 1 2 3 3 4 5 6 0
    Sample Output
    Case 1: Closest sum to 1 is 15. Closest sum to 51 is 51. Closest sum to 30 is 29. Case 2: Closest sum to 1 is 3. Closest sum to 2 is 3. Closest sum to 3 is 3. Case 3: Closest sum to 4 is 4. Closest sum to 5 is 5. Closest sum to 6 is 5.

    题意:给一个数和一组数列,在数列选两个数组合出距离这个数最近的值。

    收获:了解了unique函数。

    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    #include <ctime>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <list>
    #include <vector>
    #include <map>
    #include <set>
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    const double eps=1e-10;
    const double PI=acos(-1.0);
    #define maxn 500
    vector<int> a;
    vector<int> a1;
    vector<int> a2;
    int n;
    int main()
    {
        int cas = 1;
        while(~scanf("%d", &n)&&n)
        {
            a.clear();
            a1.clear();
            int b;
            for(int i = 0; i < n; i++)
            {
                scanf("%d", &b);
                a.push_back(b);
            }
            for(int i = 0; i < a.size(); i++)
                for(int j = i + 1; j < a.size(); j++)
                a1.push_back(a[i]+a[j]);
            sort(a1.begin(), a1.end());
    //        a2.clear();
    //        a2.push_back(a1[0]);
    //        for(int i = 1; i < a1.size(); i++)  if(a1[i] != a1[i-1]) a2.push_back(a1[i]);
            vector<int>::iterator iter = unique(a1.begin(), a1.end());
            a1.erase(iter, a1.end());
            int m;
            printf("Case %d:
    ", cas++);
            scanf("%d", &m);
            int c;
            for(int i = 0; i < m; i++)
            {
                scanf("%d", &c);
                vector<int>::iterator it;
                it = lower_bound(a1.begin(), a1.end(), c);
                if(*it == c) printf("Closest sum to %d is %d.
    ", c, c);
                else
                {
                    int ans = *it;
                    if(it != a1.begin() && abs(*(it-1) - c) < abs(*it - c))
                    ans = *(it - 1);
                    printf("Closest sum to %d is %d.
    ", c, ans);
    
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    微软发布TFS 2018!
    Git小技巧:VIM中如何填写注释信息
    为某金融企业的IT技术部人员提供基于TFS的软件研发流程介绍
    【TFS 2017】使用浏览器上传文件(TFVC)或者编辑代码,错误提示TF14098,需要对文件有PendChange 权限
    TFS 2017 培训
    签入代码(新建分支,新建推拉请求)关联工作项,却找不到自己需要的工作项
    TFS支持移动设备,微软已经走出了第一步(手机上更新、查询工作项)
    在TFS持续集成(持续发布)中执行Telnet任务
    Nginx
    单点登录
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/4734282.html
Copyright © 2020-2023  润新知