PAT甲级真题 1048 Find Coins (25分) C++实现 (找数组中两数和为某值,多种方法)

news/2024/5/19 23:23:52 标签: 数据结构, 算法, 二分法, 快速排序

题目

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

思路

这道题很容易通过,有多种方法。

排序+二分查找

一开始想到的是排序后二分查找(顺序遍历,在其后面找等于M - a[i]的数),复杂度为O(nlogn)。具体是排序用时nlogn,二分查找用时log(n-1) + log(n-2) + log(n-3)……+ 1 = log(n-1)! = O(nlogn)。

用到了STL库函数,algorithm.h中定义的binary_search

bool binary_search (ForwardIterator first, ForwardIterator last, const T& val);

返回值表示是否找到了元素。

排序+双指针

也可以排序后用双指针i、j分别指向头尾,若和大于M则左移最j,若和小于M则右移i;若二者相遇还没找到和等于M的,则无解。复杂度为nlogn + n,量级一样但具体复杂度低了。

参考:PAT-A 1048 Find Coins (25 分)

Hash

柳神用了复杂度O(n)的方法,用空间换时间。考虑到数组中每个元素值都不超过500,可以以数值为索引,记录元素个数。这样查找就能直接完成。

参考:1048. Find Coins (25)-PAT甲级真题(Hash散列)

代码

二分查找版:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    int n, m;
    cin >> n >> m;
    vector<int> coin(n);
    for (int i=0; i<n; i++){
        cin >> coin[i];
    }
    sort(coin.begin(), coin.end());
    for (int i=0; i<n-1; i++){
        if (binary_search(coin.begin()+i+1, coin.end(), m-coin[i])){
            cout << coin[i] << " " << m - coin[i] << endl;
            return 0;
        }
    }
    cout << "No Solution" << endl;
    return 0;
}



http://www.niftyadmin.cn/n/905851.html

相关文章

PHP如何隐藏URL,如何隐藏url中的index.php

隐藏url中的index.php的方法&#xff1a;首先设置【URL_MODEL>2】&#xff1b;然后根路径下建立一个【.htaccess】&#xff1b;接着将相关代码放到【.htaccess】中保存&#xff1b;最后将配置文件中【#】去除。隐藏url中的index.php的方法&#xff1a;第一步&#xff1a;URL…

MikroTik RouterOS 6.x版本开始支持使用img镜像安装(U盘安装的终极解决方法)

从6的rc6版本开始&#xff0c;官方已经提供了img镜像的安装方式&#xff0c;这让很多以前使用U盘安装的弊端一下子解放了&#xff0c;只需要把下载回来的img进入到PE&#xff0c;然后还原即可。 还原方法有很多&#xff1a;比如physdiskwrite、dd等等。 官方提供的第一个地址&a…

es php配置返回的字段,Elasticsearch中使用script_fields的同时还返回_source字段的方法...

如何保留_source字段的同时返回新增自定义字段背景Elasticsearch说它能满足一切意料之中和意料之外的需求&#xff0c;通常&#xff0c;增加一个返回字段就是意料之外又是意料之中的需求。。。通过官方文档&#xff0c;可以看到&#xff0c;通过script_fields我们可以自定义一些…

原创 PAT甲级真题 1049 Counting Ones (30分) C++实现(数学问题,找计算公式)

题目 The task is simple: given any positive integer N, you are supposed to count the total number of 1’s in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1’s in 1, 10, 11, and 12. Input Specification: Each…

暑假第二十八测

题解&#xff1a; 第一题&#xff1a;蓝书上原题&#xff0c;每个蚂蚁相对位置不变&#xff0c;排一遍序&#xff1b; #include<bits/stdc.h> using namespace std; const int M 100005, E 1000000000; double ans[M], t[M] ; struct Ant{int p, d;}a[M]; int main(){f…

PAT甲级真题 1050 String Subtraction (20分) C++实现(散列记录字符是否出现)

题目 Given two strings S​1​​ and S​2​​, SS​1​​−S​2​​ is defined to be the remaining string after taking all the characters in S​2​​ from S​1​​. Your task is simply to calculate S​1​​−S​2​​ for any given strings. However, it might…

springMVC的流程

springMVC的流程&#xff1f; 答&#xff1a;1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理器映射器。 3.处理器映射器根据请求url找到具体的处理器&#xff0c;生成处理器对象及处理器拦截器(如果有则生成)一并返回给Dispa…

PAT甲级真题 1051 Pop Sequence (25分) C++实现(检查递减序列是否合法)

题目 Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can…