zoukankan      html  css  js  c++  java
  • PAT甲级——A1048 Find Coins

    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 1 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 (≤, the total number of coins) and M (≤, 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 #include <iostream>
     2 #include <algorithm>
     3 #include <vector>
     4 #include <cstdlib>
     5 using namespace std;
     6 int N, M;
     7 //放法一,使用窗口函数
     8 int main()
     9 {
    10     cin >> N >> M;
    11     vector<int>nums(N);
    12     vector<pair<int, int>>res;
    13     for (int i = 0; i < N; ++i)
    14         cin >> nums[i];
    15     sort(nums.begin(), nums.end(), [](int a, int b) {return a < b; });
    16     int l = 0, r = N - 1, m;
    17     while (l < r)//找到中间的数刚好与M/2最接近
    18     {
    19         m = (l + r) / 2;
    20         if (nums[m] <= M / 2 && nums[m + 1] > M / 2)
    21             break;
    22         if (nums[m] > M / 2)
    23             r = m - 1;
    24         else
    25             l = m + 1;
    26     }
    27     r = m + 1;
    28     l = m;
    29     while(l>=0 && r<N)//然后使用窗口函数,进行左右移动
    30     { 
    31         if (nums[l] + nums[r] == M)
    32         {
    33             res.push_back(make_pair(nums[l], nums[r]));
    34             r++;
    35         }
    36         else if (nums[l] + nums[r] < M)
    37             r++;
    38         else
    39             l--;
    40     }
    41     sort(res.begin(), res.end(), [](pair<int, int> a, pair<int, int> b) {return a.first < b.first; });
    42     if (res.size() == 0)
    43         cout << "No Solution" << endl;
    44     else
    45         cout << res[0].first << " " << res[0].second << endl;
    46     return 0;
    47 }
    48 
    49 //方法二,使用数找数原理
    50 int main()
    51 {
    52     cin >> N >> M;
    53     int nums[1001], t;//根据题目要新建的数组大小
    54     memset(nums, 0, sizeof(int) * 1001);
    55     for (int i = 0; i < N; ++i)
    56     {
    57         cin >> t;
    58         nums[t]++;//统计个数
    59     }
    60     for (int i = 0; i < 1001; ++i)
    61     {
    62         if (nums[i] > 0)//个数大于0
    63         {
    64             nums[i]--;//使用掉一个数字
    65             if (M > i && nums[M - i] > 0)
    66             {
    67                 cout << i << " " << M - i << endl;//由最小值开始遍历,故为最优答案
    68                 return 0;
    69             }
    70             nums[i]++;//没有使用,还回去
    71         }
    72     }
    73     cout << "No Solution" << endl;
    74     return 0;
    75 }
  • 相关阅读:
    elk6.3 centos集群搭建 head插件安装
    10.2半群,同余关系,半群直积,商半群
    10.1代数结构
    9.4 关系的闭包
    9.5 等价关系
    9.6偏序关系
    9.3 关系的表示
    9.1 关系及关系性质
    差分数组
    拓扑排序
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11275777.html
Copyright © 2011-2022 走看看