CCF CSP Brush Question Record 21-201712-2 Game (Java)

Question number: 201712-2
Question name: game
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  There are n children playing games in a circle, the children are numbered from 1 to n , the 2nd child sits in the clockwise direction of the 1st child, the 3rd child sits in the clockwise direction of the 2nd child,..., the 1st child Sitting in the clockwise direction of child n .
  At the beginning of the game, start with the number 1 child and report the number clockwise. Next, each child’s number is the number reported by the previous child plus one. If the number reported by a child is a multiple of k or the last digit (that is, the ones digit of the number) is k , then the child will be eliminated from the game and will not participate in future reports. When there is only one child left in the game, that child wins.
  For example, when n=5, k=2:
  Child No. 1 counts 1;
  Child 2 counts 2 to be eliminated;
  Child 3 counts 3;
  Child 4 reports 4 to be eliminated;
  Child 5 counts 5;
  1 Child number 6 is eliminated;
  child 3 reports 7;
  child 5 reports 8 to eliminate;
  child 3 wins.

  Given n and k , what is the number of the last kid who won?

Input format

  Enter a line, including two integers n and k , the meaning is as described in the title.

Output format

  Output a line containing an integer that represents the number of the winning child.

Sample input

5 2

Sample output

3

Sample input

7 3

Sample output

4

Data size and convention

  For all evaluation use cases, 1 ≤ n  ≤ 1000 and 1  ≤  k  ≤ 9.

 

import java.util.Scanner;
public class 游戏 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int k=sc.nextInt();
		int[] a =new int[n+1];
		int count=n;
		int i=1;
		int num=1;
		
		while(count!=1){
			if(a[i]!=-1){
			a[i]=num;
			num++;
			}
			if(a[i]%k==0||a[i]%10==k){
				a[i]=-1;
				count--;
			}
			if(i<n){
				i++;
			}else{
				i=1;
			}
			
		}
		
		for(int j=1;j<=n;j++){
			if(a[j]!=-1){
				System.out.println(j);
			}
		}
		
	}

}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108362108