题目
Score : 100 points
Problem Statement
There is a device with a screen that shows a single-digit number, and a button.
When the screen is showing a number kk, pressing the button once changes the number on the screen to akak.
The device currently shows 00. After pressing the button 33 times, what will be shown on the screen?
Constraints
- 0≤ai≤9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
a0 a1 …… a9
Output
Print the answer.
Sample Input 1
9 0 1 2 3 4 5 6 7 8
Sample Output 1
7
The number on the screen transitions as 0→9→8→7.
Sample Input 2
4 8 8 8 0 8 8 8 8 8
Sample Output 2
4
The number on the screen transitions as 0→4→0→4.
Sample Input 3
0 0 0 0 0 0 0 0 0 0
Sample Output 3
0
题目翻译及分析
给你9个数,a0,a1...a9。刚开始取0为初始位置,然后转移到a0位置,再转移到a[a[0]]位置...,这样转3次就好了。
先把a数组读入。然后可以直接printf("%d",a[a[a[0]]]),也可以用循环,我写的是用循环。
代码
#include<bits/stdc++.h>
using namespace std;
int n,i,a[10];
int main(){
for(i=0;i<10;++i) scanf("%d",&a);
n=3,i=a[0]; //第一次
while(--n) i=a[i]; //再来两次
printf("%d",i);
return 0;
}