Time Limit: 1 second
Memory Limit: 512 mebibytes
As we know, frog is one of the world’s oldest amphibious animal, so the frogs have a special counting method of their own, each of them has a deck of n mogic stones labeled with 0,1,2,....(n−1).
Initially, the stones are placed in the order p1,p2,p3...pnfrom top to bottom. In each round, if the top stones labeled with x, our young frog will place it x stones downward, so it becomes the stones number (x+1) in the deck, counting from 1, the relative order of other stones will not be changed, and then our yound frog will become one second older.
How many seconds will the frog get until the mogic stone labeled with 0 comes to the top?
Input
The first line contains an integer n(1≤n≤20)
The second line contains n distinct integer p1,p2,…pn(0≤pi≤n).
Output
Output an integer which denotes the number of seconds.If the stone labeled with 0 never comes to the top, output -1s instead.
Examples
Sample Input 1
5
1 3 2 4 0
Sample Output 1
13
Sample Input 2
2
0 1
Sample Output 2
0
Note
Why not play it by yourself to find something interesting?
——————,——————。
#include <stdio.h>
#define MAX 20
int main()
{
int arr1[MAX], arr2[MAX];
int i, j;
int n;
int age = 0;
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%d", &arr1[i]);
while(arr1[0] != 0)
{
for(j=0; j<arr1[0]; j++)
arr2[j] = arr1[j+1];
arr2[j++] = arr1[0];
for(; j<n; j++)
arr2[j] = arr1[j];
age++;
for(i=0; i<n; i++)
arr1[i] = arr2[i];
}
printf("%d\n", age);
return 0;
}
deathpure
发布了2 篇原创文章 · 获赞 0 · 访问量 8
私信
关注