问题描述
腿铮找2255有点事,但2255太丑了,所以腿铮不知道他的长相。正愁不知道到如何找他的时候,他突然看见计33班围成了一个圈在领微积分试卷。计33班有n个人,其中班长编号为0,其余同学依次按顺时针方向编号。
只听见计33小导说“x号同学顺时针方向往后数的第k个的神犇出列(不包括x号同学),领取满分试卷!”。剩下的人继续围成一个小圈。这样一个过程持续了n-1次,那么显然,最后只剩下了一个人。众所周知,2255是个大傻子,门门挂科,不符合满分试卷这一前提条件。通过这样一个过程,腿铮终于找到了2255并血虐了他。
求2255的编号是多少。
输入格式
第一行一个n,表示计33班的人数。
接下来n-1行,表示小导说话中的x和k。
输出格式
一个数字,表示2255的编号。
其实每次的入参的最后一对的前一个参数即为答案,既然每次都要排除他自己以外的人,那里最后一次取的值肯定是他自己的编号。
下面是可AC的代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Reader sc=new Reader();
int n=sc.nextInt();
for(int i=0;i<n-1;i++) {
int x=sc.nextInt();
int y=sc.nextInt();
if(i==n-2){
System.out.println(x);
}
}
}
/** 快速输入类 */
static class Reader {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
/** 获取下一段文本 */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
}
下面是90分的代码,不能AC
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Reader sc=new Reader();
int n=sc.nextInt();
boolean [] a=new boolean[n];
Arrays.fill(a, false);
for(int i=0;i<n-1;i++) {
int x=sc.nextInt();
int y=sc.nextInt();
int s=x;
while(y>0) {
s++;
if(s==n) {
s=0;
}
if(a[s]) {
continue;
}
y--;
}
a[s]=true;
}
for(int i=0;i<n;i++) {
if(!a[i]) {
System.out.println(i);
break;
}
}
}
/** 快速输入类 */
static class Reader {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
/** 获取下一段文本 */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
}