【PAT甲级】1048 Find Coins (25分):哈希

include

using namespace std;

const int maxM=1010;
int Time[maxM];//数组:硬币出现次数
int main() {
fill(Time,Time+maxM,0);
int N,M;
int coin;

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

for(int i=0; i<M/2+1; i++) {
    if(Time[i]) {//不等于0
        Time[i]--;
        if(Time[M-i]) {
            printf("%d %d",i,M-i);
            return 0;
        }
        Time[i]++;
    }
}
printf("No Solution");

return 0;

}

猜你喜欢

转载自www.cnblogs.com/musecho/p/12290649.html