Skip to content

Instantly share code, notes, and snippets.

@chyiiiiiiiiiiii
Last active January 18, 2016 15:11
Show Gist options
  • Save chyiiiiiiiiiiii/90c0a814b5032d0e1cca to your computer and use it in GitHub Desktop.
Save chyiiiiiiiiiiii/90c0a814b5032d0e1cca to your computer and use it in GitHub Desktop.
1號解法:
public class Main {
public static void main(String argv[]) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int j = 0; j < n; j++) {
int people = input.nextInt();
int die = input.nextInt();
ArrayList list = new ArrayList(people);
for (int i = 0; i < people; i++) {
list.add(i + 1);
}
int index = 0;
for (int k = 0; k < people; k++) {
if (list.size() == 1) {
System.out.println(list.get(0));
break;
}
index = (index + die - 1) % list.size();
list.remove(index);
}
}
}
}
2號解法:
public class Main {
public static void main(String argv[]) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if (1 < n && n < 10) {
for (int j = 0; j < n; j++) {
int people = input.nextInt();
int die = input.nextInt();
ArrayList list = new ArrayList(people);
for (int i = 0; i < people; i++) {
list.add(i + 1);
}
int index = 0;
int result = r(index, die, list);
System.out.println(result);
}
}
}
private static int r(int index, int die, ArrayList list) {
if (list.size() == 1) {
return (int) list.get(0) ;
}
index = (index + die - 1) % list.size();
list.remove(index);
return r(index, die, list);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment