1196: 最后的胜利者
题目
Description
n 个小孩围成一圈做游戏,游戏将决出若干个胜利者。假定一个数 m,从第
1 个小孩起,顺时针数数,每数到第 m 个小孩时,该小孩离开。接着又从
下一个小孩开始数数,数到第 m 个小孩时,该小孩也离开,如此不断反复
进行,最后剩下的 k 个小孩便是胜利者。对于一定的 n、m、k,究竟胜利
者是哪些呢?
Input
输入数据有一些数据组,每组数据含有整数 n、m、k(1≤ n, m, k≤
50)),分别表示小孩数,游戏中每次数数的个数和最后剩下的 k 个胜利
者。
Output
对于每组数据,按从小到大的顺序输出一列获胜小孩的位置。每组获胜序
列之间应回车。
Sample Input
10 3 3
10 4 3
5 2 2
2 1 1
Sample Output
4 5 10
1 5 6
3 5
2
代码块
//该题的原理和我之前提交的一个出圈的原理相同,里面大致计算内容,都写了注释,大家可以查看那一段代码,有问题,可以直接评论
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cn = new Scanner(System.in);while (cn.hasNext()) {int n = cn.nextInt();int m = cn.nextInt();int k = cn.nextInt();int[] a = new int[n];for (int j = 0; j < n; j++) {a[j] = j + 1;}int i = 0, len = n, count = 1;while (len > k) {if (a[i % n] > 0) {if (count % m == 0) {a[i % n] = -1;len--;count = 1;i++;} else {i++;count++;}} else {i++;}}int t = 0;for (int j = 0; j < n; j++) {if (a[j] > 0) {t++;}}int z =0;for(int j =0;j<n;j++){if(a[j]>0&&z<t-1){z++;System.out.print(a[j]+" ");}else if(a[j]>0){System.out.println(a[j]);}}}}
}
1196: 最后的胜利者
题目
Description
n 个小孩围成一圈做游戏,游戏将决出若干个胜利者。假定一个数 m,从第
1 个小孩起,顺时针数数,每数到第 m 个小孩时,该小孩离开。接着又从
下一个小孩开始数数,数到第 m 个小孩时,该小孩也离开,如此不断反复
进行,最后剩下的 k 个小孩便是胜利者。对于一定的 n、m、k,究竟胜利
者是哪些呢?
Input
输入数据有一些数据组,每组数据含有整数 n、m、k(1≤ n, m, k≤
50)),分别表示小孩数,游戏中每次数数的个数和最后剩下的 k 个胜利
者。
Output
对于每组数据,按从小到大的顺序输出一列获胜小孩的位置。每组获胜序
列之间应回车。
Sample Input
10 3 3
10 4 3
5 2 2
2 1 1
Sample Output
4 5 10
1 5 6
3 5
2
代码块
//该题的原理和我之前提交的一个出圈的原理相同,里面大致计算内容,都写了注释,大家可以查看那一段代码,有问题,可以直接评论
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cn = new Scanner(System.in);while (cn.hasNext()) {int n = cn.nextInt();int m = cn.nextInt();int k = cn.nextInt();int[] a = new int[n];for (int j = 0; j < n; j++) {a[j] = j + 1;}int i = 0, len = n, count = 1;while (len > k) {if (a[i % n] > 0) {if (count % m == 0) {a[i % n] = -1;len--;count = 1;i++;} else {i++;count++;}} else {i++;}}int t = 0;for (int j = 0; j < n; j++) {if (a[j] > 0) {t++;}}int z =0;for(int j =0;j<n;j++){if(a[j]>0&&z<t-1){z++;System.out.print(a[j]+" ");}else if(a[j]>0){System.out.println(a[j]);}}}}
}