俊介三

一天更新一点,一天积累一点

导航

1048. Find Coins (25)

Posted on 2013-03-04 23:29  俊介三在前进  阅读(160)  评论(0)    收藏  举报

1048. Find Coins (25)

http://pat.zju.edu.cn/contests/pat-a-practise/1048

题目大意:给一系列硬币的面值,再给一个物品的总价值,找出两个硬币刚好能支付这个物品。

最初思路错了,先给它排序,然后逐个找,以为排除一下最大的两个硬币都比物品价值小,或者最小两个都比物品大就行~

其实,看清它问题各个变量的范围,可以发现面值[1,500],用哈希表的方法才不会超时~~哎,我勒个擦。。

(PS. CYLL很稀饭火星,一些无厘头或不合理的规定、游戏规则,都会说在Mars上。。。)

#include <stdio.h>
#define MAX 1505

int coin[MAX]={0};

int main(){
    int N,M,i;
    scanf("%d %d",&N,&M);

    int temp;
    for(i=0;i<N;i++){
        scanf("%d",&temp);
        coin[temp]++;
    }

    for(i=1;i<=500;i++){
        if(coin[i]>0 && coin[M-i]>0 && i!=M-i){
            printf("%d %d\n",i,M-i);
            return 0;
        }
        if(i==M-i && coin[i]>=2){
            printf("%d %d\n",i,M-i);
            return 0;
        }
    }
    printf("No Solution\n");
    return 0;
}

以后要记得哈希表!!

PS. 集合判断元素唯一性之类的,也用哈希~~