随笔-48  评论-26  文章-0  trackbacks-0
   问题:
            有17个人围成一圈(编号0~16),从第0号的人开始从1报数,凡报到3的倍数的人离开圈子,
            然后再数下去,直到最后只剩下一个人为止,问此人原来的位置是多少号?

  
int [] people = new int[17];
    
int lastPeople = 0;
    
public void getTheLastPeople(){       
        
for(int i = 0; i < people.length; i++){
            people[i] 
= i+1;
        }
        
int count = 0;
        
int countLast = 0;
        
int j = 0;
        
while(true){
            
for(j = 0; j < people.length; j++){               
                
if(people[j] != 0){
                    count
++;
                    people[j] 
= count;
                    System.out.println(
"people[" + j + "] = " + people[j]);
                    
if (people[j] % 3 == 0) {
                        people[j] 
= 0;
                        countLast
++;
                        
if(countLast == 17){
                            lastPeople 
= j;
                            
return;
                        }
                    }
                }
            }           
        }
    }
posted on 2009-09-26 14:35 Worker 阅读(133) 评论(0)  编辑  收藏 所属分类: 算法/数据结构

只有注册用户登录后才能发表评论。


网站导航: