自己写的 java 报数 程序

一圈人报数,数到3出来,继续报,求最后剩下的人,原来的编号。

static int p[] = new int[9];
	static int [] np = null;
	public static void main(String[] args) {
		
		for(int i=0;i<9;i++){
			p[i]=i;
		}
		  circle(p);
		for(int i=0;i<np.length;i++){
			System.out.println(np[i]);
		}
	}
	
	
	public static void circle(int[] p){
		int m = 0;
		for(int i=0;i<p.length;i++){
			
		 
			m++;
			if(m==3){
				m=0;
				  np = rebulid(p,i);
				if(np.length>2){
					circle(np);
				} 
			}
			
		}		
	}
	public static int[] rebulid(int[] p,int i){
		int [] np = new int [p.length-1];
		int c = i+1;
		if(c>=p.length){
			 c=c-p.length;
		 }
		for(int j=0;j<np.length;j++){
			np[j] = p[c];
			c++;
			i++;
			 if(c>=p.length){
				 c=c-p.length;
			 }
			
		}
		return np;
	}
	


猜你喜欢

转载自blog.csdn.net/os2046/article/details/52596449