CF1703B. Cow and Friend

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值