B. Cow and Friend
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Bessie has way too many friends because she is everyone’s favorite cow! Her new friend Rabbit is trying to hop over so they can play!
More specifically, he wants to get from (0,0) to (x,0) by making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Euclidean distance between the endpoints of a hop is one of its n favorite numbers: a1,a2,…,an. What is the minimum number of hops Rabbit needs to get from (0,0) to (x,0)? Rabbit may land on points with non-integer coordinates. It can be proved that Rabbit can always reach his destination.
……
……
……
思路:
1.如果数组中有与x相等的数直接1步。
2.如果不符合1,求出数组的最大数max。如果max大于x,则2步,否则,上取整x/max
#include<iostream>
using namespace std;
const int N = 100010;
int a[N];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,x;
cin>>n>>x;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int maxi=0;
bool flag=false;
for(int i=0;i<n;i++)
{
if(a[i]==x){
flag=true;
}
if(a[i]>maxi) maxi=a[i];
}
if(flag) cout<<1<<endl;
else{
if(maxi>x) cout<<2<<endl;
else if(x%maxi==0) cout<<x/maxi<<endl;
else cout<<x/maxi+1<<endl;
}
}
return 0;
}
PS:上取整优美写法:(x+maxi-1)/maxi