A. Garden

欺负人的水题:http://codeforces.com/problemset/problem/915/A

Luba thinks about watering her garden. The garden can be represented as a segment of length k. Luba has got n buckets, the i-th bucket allows her to water some continuous subsegment of garden of length exactly ai each hour. Luba can’t water any parts of the garden that were already watered, also she can’t water the ground outside the garden.

Luba has to choose one of the buckets in order to water the garden as fast as possible (as mentioned above, each hour she will water some continuous subsegment of length ai if she chooses the i-th bucket). Help her to determine the minimum number of hours she has to spend watering the garden. It is guaranteed that Luba can always choose a bucket so it is possible water the garden.

See the examples for better understanding.

Input
The first line of input contains two integer numbers n and k (1 ≤ n, k ≤ 100) — the number of buckets and the length of the garden, respectively.

The second line of input contains n integer numbers ai (1 ≤ ai ≤ 100) — the length of the segment that can be watered by the i-th bucket in one hour.

It is guaranteed that there is at least one bucket such that it is possible to water the garden in integer number of hours using only this bucket.

Output
Print one integer number — the minimum number of hours required to water the garden.
在这里插入图片描述
Note
In the first test the best option is to choose the bucket that allows to water the segment of length 3. We can’t choose the bucket that allows to water the segment of length 5 because then we can’t water the whole garden.

In the second test we can choose only the bucket that allows us to water the segment of length 1.

题意:在a[n]中找出一个可以被k整除的数,当然越大越好。

错误代码:

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
	int n, k, i;
	int a[101];
	cin >> n >> k;
	for (i = 0; i < n; i++)
		cin >> a[i];
	sort(a, a + n);
	for (; n >= 0; n--)
	{
		i = a[n];
		if (k%i == 0)
		{

			cout << k / i << endl;
			break;
		}
	}
	//system("pause");
	return 0;
}

在这里插入图片描述
原因是超时,这个竟然超时!!!无fu*k理解!!!

正确代码:

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
	int n, k, i,ans=110;
	int a[101];
	cin >> n >> k;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];
		if (k%a[i] == 0)
		{
			a[i] = k / a[i];
			ans = min(a[i], ans);
		}
	}
	cout << ans << endl;
	//system("pause");
	return 0;
}

明明就改动不大!!!之前怎么就超时了???
难道是两个for的原因??

我的40米大刀已经收不住了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值