HDU 1344(Nonst)

题目不难,注意几个点:

  1. 绿灯、黄灯、红灯时间都是整型;
  2. 速度为整型,范围为 [30, 60];
  3. 绿灯和黄灯都可以通过;
  4. 注意输出格式。
#include <iostream>
using namespace std;

struct signal //信号灯
{
	double dis; //距停车场的距离(英里)
	int cycle; //信号灯周期(秒)
	int go; //每个信号灯周期中能通过的时间(秒)
}s[10];

int main()
{
	int Case = 1; //测试用例数
	int N;
	while (cin >> N)
	{
		if (N == -1)
			break;

		int g, y, r; //绿灯时间,黄灯时间,红灯时间
		for (int i = 0; i < N; i++)
		{
			cin >> s[i].dis >> g >> y >> r;
			s[i].cycle = g + y + r;
			s[i].go = g + y;
		}

		int speed[35]; //满足要求的速度
		int index = 0;
		double time; //到达当前信号灯的时间(秒)
		bool flag; //到达每个信号灯时,是否都是绿灯或黄灯
		for (int v = 30; v <= 60; v++) //判断每个速度是否满足要求
		{
			flag = true;
			for (int i = 0; i < N; i++) //依次判断每个信号灯
			{
				time = s[i].dis * 3600 / v;
				time -= (int)time / s[i].cycle * s[i].cycle;
				if (time > s[i].go)
				{
					flag = false;
					break;
				}
			}
			if (flag) //当前速度满足要求
				speed[index++] = v;
		}

		cout << "Case " << Case++ << ": ";
		if (index > 0) //输出
		{
			int i, j;
			for (i = 0; i < index - 1; i++)
			{
				cout << speed[i];
				int total = 0; //能够和speed[i]组合的速度的数量
				for (j = i + 1; j < index; j++)
				{
					if (speed[j] == speed[j - 1] + 1)
						++total;
					else
						break;
				}
				if (total > 0)
				{
					cout << "-";
					if (j < index)
						cout << speed[j - 1] << ", ";
					i = j - 1;
				}
				else
					cout << ", ";
			}
			cout << speed[index - 1] << endl;
		}
		else
			cout << "No acceptable speeds." << endl;
	}
	return 0;
}

继续加油。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值