Description
题目大意:Eval喜欢收集coin,现在买东西需要支付两个硬币,希望你能帮她找到哪两个硬币
Input
n代表coin数量 m代表购买的总价格 接下来n个数代表每个硬币的价值
Output
输出两个硬币的数值,如果不唯一,输出第一个较小的那个,如果没有答案,输出No Solution
解题思路
算法标签:散列表 简单做个映射numbers[number]代表number的数量,显然题目中可能存在多个价值相同的硬币,所以要考虑价值为i和m-i是否相同,相同的话数量是否大于1,显然这道题关键在于查找的效率
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std
;
const int N
= 100005;
const int MAXX
= 0x3f3f3f3f;
int numbers
[N
];
int main()
{
int n
= 0,m
= 0,minn
= MAXX
,maxx
= 0;
bool isresult
= false;
int number
= 0;
cin
>>n
>>m
;
for(int i
=1;i
<=n
;i
++)
{
scanf("%d",&number
);
minn
= min(minn
,number
);
maxx
= max(maxx
,number
);
numbers
[number
]++;
}
for(int i
=minn
;i
<=maxx
;i
++)
{
if((numbers
[m
-i
]!=0)&&(numbers
[i
]!=0)&&(m
-i
!=i
))
{
isresult
= true;
cout
<<i
<<" "<<m
-i
;
break;
}
else if((numbers
[m
-i
]!=0)&&(numbers
[i
]>=2)&&(m
-i
==i
))
{
isresult
= true;
cout
<<i
<<" "<<m
-i
;
break;
}
}
if(!isresult
)
cout
<<"No Solution";
return 0;
}