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. Python编程中 re正则表达式模块 介绍与使用教程

    Python编程中 re正则表达式模块 介绍与使用教程 一.前言: 这篇文章是因为昨天写了一篇 shell script 的文章,在文章中俺大量调用多媒体素材与网址引用.这样就会有一个问题就是:随着俺 ...

  2. sed--行编辑器命令

    2017-11-02 sed:行编辑器,按行处理:默认不做原文件修改,会在sed自身的模式空间中按行读相关的数据并完成编辑,然后显示到屏幕上     一.     基本用法: 1. 语法格式: sed ...

  3. Tomcat入门级小白教程

    Tomcat 类似与一个apache的扩展型,属于apache软件基金会的核心项目,属于开源的轻量级Web应用服务器,是开发和调试JSP程序的首选,主要针对Jave语言开发的网页代码进行解析,Tomc ...

  4. java里面的标识符、关键字和类型

    1. 注释  Java中有三种注释:   (1) // -单行注释,注释从“//”开始,终止于行尾:   (2)  -多行注释,注释从““结束:   (3)  -是Java特有的doc注释,这种注释主 ...

  5. Python脚本模板

    1.Python脚本规范基础模板 #coding:utf8 import time, re, os, sys, time,urllib2,shutil,string import json,datet ...

  6. 新公司,环境搭建,windows上的坑

    1 在windows上安装nodejs环境,node -v 后可以看到版本号 2 在windows上安装nvm管理node的版本,可以对node版本自由切换,使用5.3.0版本的node 3 在win ...

  7. ElasticSearch 常用设置

    2.2.0的启动和6.几 启动路径.端口一样,但是进入Head的路径不一样 http://localhost:9200/ 进入Head的方式2.2 的在 http://localhost:9200/_ ...

  8. jsoup 抓取省市区

    package com.xazhxc.htjcom.back.controller.base; import cn.hutool.core.util.StrUtil; import com.aliba ...

  9. 做point data的切面的时候的注意事项

    正确的顺序应该是: 先导入cell data,再转换为point data,再做切面.结果如下: 如果这里导入cell data以后先做了切面再转换为point data,结果就是这样的: 很明显中间 ...

  10. 基础篇:4.2)规范化:3d软件工程图纸用模板

    本章目的:分享一些国标标准模板,方便作图时使用 这篇博文的内容作者会定时的更新 1.工程图模板 drawing template 定义 三维机械设计软件中的一种文件类型.通过标准化定制和使用该文件,可 ...