JAVA Blue Bridge Cup training screening algorithm number

JAVA Blue Bridge Cup training screening algorithm number

Resource constraints
Time limit: 1.0s Memory Limit: 512.0MB

Problem description
  has n individual circle, Arranging the sequence (numbered 1 to n). From the first personal countin (report number from 1-3), who report in three people exit the circle. I began to continue to report the number from the next person, until the last person remaining, the game ends.
  I ask who is the last remaining original No. few.
  For example, eight people in a circle:
  13,456,782
  after the first time the number of reported 3 Exit remaining:
  1,245,678 (from now start countin 4)
  2nd after a few reported 6 exit, remaining:
  (from now gettin number 7) 124 578
  after the third number reported, exit 1, and the rest:
  24578 (from now gettin number 2)
  after the 4th number reported, exit 5, and the rest:
  (from now 7 Countin) 2478
  5th number reported after 2 quit, leaving:
  478 (from now start countin 4)
  of the after six times the number reported, exit 8, and the rest:
  47 (from 4 now gettin number)
  after the last number off, exit 4, and the rest:
  7.
  so in the end who stay number is 7.

Input format
  a positive integer n, (1 <n <10000 )

Output formats
  a positive integer, and finally left behind that person's number.

Sample input
8

Sample output
7

And the data size agreed
  to 100% of the data, 1 <n <10000.

import java.util.Scanner;

public class Main {
		public static void main(String[] args) {
			Scanner sca=new Scanner(System.in);
			int n=sca.nextInt();
			int[] a=new int[n];
			int index=0;
			int temp=0;
			int i=-1;
			while(index!=n-1) {
				temp++;
				i++;
				if(i==n) {
					i=0;
				}
				while(a[i]!=0) {
					i++;
					if(i==n) {
						i=0;
					}
				}
				if(temp==3) {
					temp=0;
					a[i]=1;
					index++;
					//System.out.println(i+1);
				}
			}
			for(int j=0;j<n;j++) {
				if(a[j]==0) {
					System.out.println(j+1);
					break;
				}
			}
		}
	}
	

Published 17 original articles · won praise 0 · Views 366

Guess you like

Origin blog.csdn.net/qq_36551453/article/details/104489096