King mioj move Fuma

Copyright: spark https://blog.csdn.net/qq_35619728/article/details/89453965

mioj

Kings move Fuma


description

King To find the world's most intelligent man to the princess as a consort, she invented a game. In the game, a plurality of consecutive king room provided, starting from the first room, each room will have n (n> = 0) doors to access the next 1 ~ n a house (e.g., the house has 2 doors, then after that, after the first door to enter a room first, second door can enter the second house), and finally through the least house will win, marry the beautiful princess.


Entry

A set of data, respectively (,) separated, each door corresponding to a number indicates the number of total house.


Export

An integer representing the number of the house through the least upon reaching the last room, or -1 if it can not reach.


//athor spark
// 
#include "stdio.h"
int main(void)

{
	static int a,b[100],i,j,k;
	static char p;
	while (~scanf("%d,", &a))
	{
		p = getchar();
		if (p == 'p')
			break;
		b[i++] = a;

	}
	int lastreach = 0, ans = 0, reach = b[0];
	for (j = 1; j < i&&j <= reach; j++)
	{
		if (j > lastreach)
		{
			ans++;
			lastreach = reach;
		}
		if (reach >= j + b[j])
			reach = reach;
		else
			reach = j + b[j];
	}
	if (reach < i - 1)
		ans = -1;
	printf("%d",ans);
	scanf("%d,", &a);
}

Hey cycle to get. . . . . . . Once through each house to go to the farthest position
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_35619728/article/details/89453965