Alaska

Problem describe:
The Alaska Highway runs 1422 miles from Dawson Creek, British Columbia to Delta Junction, Alaska. Brenda would like to be the first person to drive her new electric car the length of the highway. Her car can travel up to 200 miles once charged at a special charging station. There is a charging station in Dawson Creek, where she begins her journey, and also several charging stations along the way. Can Brenda drive her car from Dawson City to Delta Juntion and back?
Input
The input contains several scenario. Each scenario begins with a line containing n, a positive number indicating the number of charging stations. n lines follow, each giving the location of a filling station on the highway, including the one in Dawson City. The location is an integer between 0 and 1422, inclusive, indicating the distance in miles from Dawson Creek. No two filling stations are at the same location. A line containing 0 follows the last scenario.
Output
For each scenario, output a line containing POSSIBLE if Brenda can make the trip. Otherwise, output a line containing the word IMPOSSIBLE.
Sample Input
2
0
900
8
1400
1200
1000
800
600
400
200
0
0
Sample Output
IMPOSSIBLE
POSSIBLE
题意描述:一个人开着一辆充电的车,每次在一个充电站充满电后可以跑200英里,现在他要在两个城市间跑一个来回(两个城市间的距离为1422英里),问在给定充电站个数以及充电站位置后他能否跑一个来回。
解题思路:首先起点有一个充电站,因此给定的所由充电站中从小到大排序之后,第一个充电站的位置不能大于200,这是从起点到终点第一段距离的要求,再看最后一个充电站的要求,如果最后一个充电站的位置小于1322,那他是不是也回不来了,等于1322时刚好能满足从此处到终点,再从终点到此处的距离为200。
AC代码如下:

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
	int n,a[3000],i,flag;
	while(scanf("%d",&n),n!=0)
	{
		flag=0;
		for(i=0;i<n;i++)
			scanf("%d",&a[i]);
		sort(a,a+n);
		if(a[0]>200||a[n-1]<1322)
			flag=1; 
		else
		{
			for(i=0;i<n-1;i++)
			{
				if(a[i+1]-a[i]>200)
				{
					flag=1;
					break;
				}
			} 
		}
		if(flag==1)
			printf("IMPOSSIBLE\n");
		else
			printf("POSSIBLE\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44313771/article/details/106198446
今日推荐