1048. 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 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.

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> coin;
int main ()
{
    int i,n,m,temp;
    scanf("%d %d",&n,&m);
    for( i=0;i<n;i++)
    {
         scanf("%d",&temp);
         coin.push_back(temp);
         }
    sort(coin.begin(),coin.end());
    int low=0,high=coin.size()-1,sum=0;
    if( low==high)
    {
            printf("No Solution\n");
            system("pause");
            return 0;
        }
    for( ;low<high;)
    {
         sum=coin[low]+coin[high];
         if( sum==m)
         {
             printf("%d %d\n",coin[low],coin[high]);
             system("pause");
             return 0;
             }
         else if( sum>m) high--;
         else low++;
         }
    printf("No Solution\n");
    system("pause");
    return 0;
    }
时间: 2024-11-29 09:12:26

1048. Find Coins的相关文章

PAT 1048. Find Coins

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=0; i<N; i++)

1048. Find Coins (25)

题目例如以下: 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 pa

PAT:1048. Find Coins (25)(哈希表法) AC

#include<stdio.h> #include<string.h> int HARSH[1066]; int main() { memset(HARSH,0,sizeof(HARSH)); int n,m; scanf("%d%d",&n,&m); for(int i=0 ; i<n ; ++i) { int tmp; scanf("%d",&tmp); ++HARSH[tmp]; } for(int i=

PAT:1048. Find Coins (25)(双指针法) AC

#include<stdio.h> #include<algorithm> using namespace std; int arr[100066]; int main() { int n,m; scanf("%d%d",&n,&m); for(int i=0 ; i<n ; ++i) scanf("%d",&arr[i]); sort(arr,arr+n); int l=0,r=n-1; //左右指针 whil

PAT:1048. Find Coins (25)(二分查找) AC

#include<stdio.h> #include<algorithm> using namespace std; int arr[100066]; int FIND(int l,int r,int aim) //二分查找,从l到r,查找aim { int mid; while(l<=r) { mid=(l+r)/2; if(arr[mid]==aim) return mid; //找到:返回坐标 else if(arr[mid]<aim) l=mid+1; else

【PAT甲级】1048 Find Coins (25 分)(二分)

题意: 输入两个正整数N和M(N<=10000,M<=1000),然后输入N个正整数(<=500),输出两个数字和恰好等于M的两个数(小的数字尽可能小且输出在前),如果没有输出"No Solution". 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[100007];int main(){ ios::sync_with_stdio(false

pat1048. Find Coins (25)

1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 a

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d