• 【PAT(甲级)】1048 Find Coins(测试点2,3,4)


    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

    解题思路:

    碰到这种卡时间的题目,输入输出用scanf,printf,能省一点时间就是一点时间。然后将所给的数字用sort从小到大进行快排,因为要V1最小嘛,而匹配V1的时候,相加要从后面大的数字往前相加,当小于M的时候就说明以经不可能了,直接下一个数字。

    易错点:

    1. 测试点2其实就是当有两个相同的数字且他们刚好是M/2,所以不能用set(而且set还慢);

    2.测试点3就是当相加已经小于M的时候就可以推出循环了(代码里有标注);

    3.测试点4就是当有许多相同的数字给出且他们相加不能等于M的时候。

    代码:

    1. #include
    2. using namespace std;
    3. int N,M;
    4. int coins[100001];
    5. int Tpay(int a){
    6. for(int i=N-1;i>a;i--){
    7. if(coins[i]+coins[a]==M){
    8. return coins[i];
    9. }
    10. if(coins[i]+coins[a]break;//测试点3
    11. }
    12. return -1;
    13. }
    14. int main(){
    15. scanf("%d %d",&N,&M);
    16. for(int i=0;i
    17. scanf("%d",&coins[i]);
    18. }
    19. sort(coins,coins+N);
    20. for(int i=0;i
    21. if(coins[i]>M){
    22. printf("No Solution");
    23. return 0;
    24. }
    25. if(coins[i]==coins[i-1]) continue;//测试点4
    26. if(Tpay(i)>0){
    27. printf("%d %d",coins[i],Tpay(i));
    28. return 0;
    29. }
    30. }
    31. printf("No Solution");
    32. return 0;
    33. }

  • 相关阅读:
    unity shader全局雾效
    CiscoCUCM电话注册
    开源社区赋能,Walrus 用户体验再升级
    算法部署经验实操:手把手教你掌握Atlas移植和算法推理
    Python基础语法入门
    毅速科普课堂丨3D打印随形水路模具制造的一般流程
    SpringMVC+统一表现层返回值+异常处理器
    [Vulnhub] basic_pentesting_1
    L13.linux命令每日一练 -- 第二章 文件和目录操作命令 -- lsattr和file命令
    java基础-集合-ConcurrentHashMap源码学习
  • 原文地址:https://blog.csdn.net/weixin_55202895/article/details/126549587