B. Badge(模拟题,输出第二个被罚的人)

B. Badge
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
In Summer Informatics School, if a student doesn’t behave well, teachers make a hole in his badge. And today one of the teachers caught a group of
n
students doing yet another trick.

Let’s assume that all these students are numbered from
1
to
n
. The teacher came to student
a
and put a hole in his badge. The student, however, claimed that the main culprit is some other student
p
a
.

After that, the teacher came to student
p
a
and made a hole in his badge as well. The student in reply said that the main culprit was student
p
p
a
.

This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.

After that, the teacher put a second hole in the student’s badge and decided that he is done with this process, and went to the sauna.

You don’t know the first student who was caught by the teacher. However, you know all the numbers
p
i
. Your task is to find out for every student
a
, who would be the student with two holes in the badge if the first caught student was
a
.

Input
The first line of the input contains the only integer
n
(
1

n

1000
) — the number of the naughty students.

The second line contains
n
integers
p
1
, …,
p
n
(
1

p
i

n
), where
p
i
indicates the student who was reported to the teacher by student
i
.

Output
For every student
a
from
1
to
n
print which student would receive two holes in the badge, if
a
was the first student caught by the teacher.

Examples
inputCopy
3
2 3 2
outputCopy
2 2 3
inputCopy
3
1 2 3
outputCopy
1 2 3
Note
The picture corresponds to the first example test case.

When

a

1
, the teacher comes to students
1
,
2
,
3
,
2
, in this order, and the student
2
is the one who receives a second hole in his badge.

When

a

2
, the teacher comes to students
2
,
3
,
2
, and the student
2
gets a second hole in his badge. When

a

3
, the teacher will visit students
3
,
2
,
3
with student
3
getting a second hole in his badge.

For the second example test case it’s clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.

题意:一群人都犯错了,老师现在来惩罚他们,每到一个学生面前给他一个“奖章”,这时候这个学生就会说是另一个人干的,然后老师就去惩罚另一个;当一个学生有两枚奖章的时候就输出;

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
int a[1001],b[1001],c[1001];
int main()
{
    int n,p;
    scanf("%d",&n);
    for(int k=1; k<=n; k++)
    {
        scanf("%d",&b[k]);
    }
    int s=0;
    for(int i=1; i<=n; i++)
    {
       memset(a,0,sizeof(a));
       a[i]++;//每到一个学生面前就给他一枚奖章
       p=b[i];//老师走到了这个学生说的另一个学生面前
       while(1)
       {
           a[p]++;
           if(a[p]==2)
           {
             c[++s]=p;
             break;
           }
           p=b[p];
       }
    }
    for(int i=1;i<=s;i++)
    {
        if(i==1)printf("%d",c[i]);
        else printf(" %d",c[i]);
    }
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值