zoukankan      html  css  js  c++  java
  • PAT 解题报告 1048. Find Coins (25)

    1048. Find Coins (25)

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she could only use exactly two coins to pay the exact amount. Since she has as many as 105 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find two coins to pay for it.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (<=105, the total number of coins) and M(<=103, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers no more than 500. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print in one line the two face values V1 and V2 (separated by a space) such that V1 + V2 = M and V1 <= V2. If such a solution is not unique, output the one with the smallest V1. If there is no solution, output "No Solution" instead.

    Sample Input 1:

    8 15
    1 2 8 7 2 4 11 15
    

    Sample Output 1:

    4 11
    

    Sample Input 2:

    7 14
    1 8 7 2 4 11 15
    

    Sample Output 2:

    No Solution


    题意

    给定一系列硬币,以及一个商品的价格。要求从硬币中找到两个硬币让他们的组合等于商品的价格。如果有多个,输出有用最小单个值的硬币组合。

    分析

    思路1:hash

    首先,硬币中币值不小于商品价格的可以过滤掉。遍历过程中,使用 hash 标记的方法,设定一个coins[MAXVALUE](其中 MAXVALUE 为商品价格的最大值)记录已有的币值种类, 在遍历的过程中,一边将读入的硬币增加到coins[]中,一边计算满足条件的最小币值。

    这个方法速度很快,数据读完,就差不多得到结果了。

    思路2:先排序,然后两头扫

    经典的2sum求和问题, 先给数组排序, 然后头尾指针分别从前后开始扫, 扫到的两个数的和若比给定的值要小, 那么头指针向后移动一步, 如果比给定的值大, 那么尾指针向前移动一步. 如果正好找到了, 那么就是他了, 返回.

    hash

    #include <cstdio>
    
    using namespace std;
    
    const int MAXVALUE = 1001;
    int a[MAXVALUE];
    
    int main()
    {
        int min = MAXVALUE;
        int n, m;
        int tmp;
        scanf("%d%d", &n, &m);
        for (int i = 0; i != n; ++i) {
            scanf("%d",&tmp);
            if (tmp < m) {
                int tmpMin = tmp < m - tmp ? tmp : m - tmp;
                if (a[m - tmp] == 1 && tmpMin < min) {
                    min = tmpMin;
                }
                a[tmp] = 1;
            }
        }
    
        if (min != MAXVALUE) printf("%d %d
    ", min, m - min);
        else printf("No Solution");
    
        return 0;
    }

    两头扫

    #include <cstdio>
    #include <algorithm>
    #include <vector>
    
    #define REP(i,n) for(int i=0;i<(n);++i)
    using namespace std;
    
    vector<int> vals;
    
    int main(void) {
        int N, M;
        scanf("%d %d", &N, &M);
        vals.resize(N);
        REP(i, N) scanf("%d", &vals[i]);
        int i=0;
        int j=N-1;
        sort(vals.begin(), vals.end());
        bool found = false;
        while(i < j) {
            int sum = vals[i] + vals[j];
            if(sum == M) {
                found = true;
                break;
            }
            else if(sum < M) {
                i++;
            }
            else
                j--;
        }
        if(found) {
            printf("%d %d
    ", vals[i], vals[j]);
        }
        else {
            printf("No Solution
    ");
        }
        return 0;
    }
  • 相关阅读:
    Elasticsearch崩溃解决办法
    阿里云服务器ubuntu14.04安装Redis
    error: command ‘x86_64-linux-gnu-gcc’ failed with exit status 1
    本机访问阿里云服务器上的Elasticsearch
    阿里云服务器配置ElasticSearch
    ElasticSearch-RTF的安装
    【已解决】neo4j-import使用过程中遇到的问题(there's a field starting with a quote and whereas it ends that quote there seems to be characters in that field /Executor has been shut down in panic)
    OutOfMemoryError和StackOverflowError
    线程的终止
    scala基础
  • 原文地址:https://www.cnblogs.com/549294286/p/3574220.html
Copyright © 2011-2022 走看看