two sum题目,算是贪婪吧

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm> using namespace std; int main() {
int N, M;
scanf("%d%d", &N, &M); vector<int> coins(N);
for (int i=; i<N; i++) {
scanf("%d", &coins[i]);
} sort(coins.begin(), coins.end()); int p = , q = N - ;
while (p<q) {
int sum = coins[p] + coins[q];
if (sum > M) {
// bigger, so decrease it, move q to the smaller coins
q--;
} else if (sum < M) {
// smaller, so increase it, move p to the bigger coins
p++;
} else {
// we found the coins
printf("%d %d\n", coins[p], coins[q]);
break;
}
}
if (p >= q) {
printf("No Solution\n");
}
return ;
}

PAT 1048. Find Coins的更多相关文章

  1. PAT 1048 Find Coins[比较]

    1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...

  2. PAT 解题报告 1048. Find Coins (25)

    1048. Find Coins (25) Eva loves to collect coins from all over the universe, including some other pl ...

  3. PAT甲 1048. Find Coins (25) 2016-09-09 23:15 29人阅读 评论(0) 收藏

    1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...

  4. PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)

    1048 Find Coins (25 分)   Eva loves to collect coins from all over the universe, including some other ...

  5. 浙大pat 1048 题解

    1048. Find Coins (25) 时间限制 50 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...

  6. 1048 Find Coins (25 分)

    1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...

  7. PAT 甲级 1048 Find Coins

    https://pintia.cn/problem-sets/994805342720868352/problems/994805432256675840 Eva loves to collect c ...

  8. PAT Advanced 1048 Find Coins (25 分)

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  9. PAT Advanced 1048 Find Coins (25) [Hash散列]

    题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. On ...

随机推荐

  1. suid, sgid,粘滞位,chattr

    一. 基本含义和作用 1. SUID:当设置了SUID 位的文件被执行时,该文件将以所有者的身份运行,也就是说无论谁来执行这个文件,他都有文件所有者的特权. 2. SGID:与上面的内容类似.用于设置 ...

  2. P5030 长脖子鹿放置 最小割

    $ \color{#0066ff}{ 题目描述 }$ 如图所示,西洋棋的"长脖子鹿",类似于中国象棋的马,但按照"目"字攻击,且没有中国象棋"别马腿& ...

  3. 通过Maven简单搭建SSM框架

    创建Maven就不用多说了,下面直接看Pom.xml里面的依赖吧 <properties> <!-- spring版本号 --> <spring.version>5 ...

  4. ubuntu 使用ifupdown 进行高级网络设置

    ifupdown ubuntu 本身支持linux的网络底层设置命令:ifconfig,route,ip 等命令,但为了让网络设置更加简单,Debian 提供了一个标准的高级网络设置工具,包含 ifu ...

  5. Touchmove获取当前触摸的Dom节点

    原文链接:https://www.jianshu.com/p/a2a41fe20790 Touchmove获取当前触摸的Dom节点 近期为自己的app增加一个通讯录首字母导航的功能,如下图蓝色框部分, ...

  6. PHP单表操作mysqli数据库类的封装

    class DB{ private $options=array( 'database_type' => 'mysql', 'database_name' => 'test', 'serv ...

  7. 大数据-zookeeper集群安装

    一.安装前发现的问题: 1.安装前期发现jps权限不够 [root@master1 ~]# jps -bash: /opt/workspace/jdk1./bin/jps: Permission de ...

  8. docker images 按名称过滤

    docker images nihao_* 列出所有 nihao_* 正则匹配的镜像

  9. MyEclipse配置,每次打开server中都没有weblogic

    最近在myeclipse新配了个weblogic,结果每次打开myeclipse在server中都看不到weblogic,得重新去配置页面走一遭才能出现.很麻烦. 后来在网上找了找,找到一个办法: 在 ...

  10. ZOJ - 3946-Highway Project(最短路变形+优先队列优化)

    Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can ...