码迷,mamicode.com
首页 > 编程语言 > 详细

python 实现N个人,从1数到3,数到3的出局,来回循环,最后剩下一个人

时间:2019-02-18 11:37:40      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:数组   remove   lis   append   string   range   index   main   元素   

person = []
for i in range(1, 4):
person.append(i)
print ‘person:%s‘ % person
index = 0
while len(person) > 1:
removelist = []
for item in person:
index += 1
if index == 3:
removelist.append(item)
index = 0
print ‘removelist:%s‘ % removelist
for item in removelist:
person.remove(item)

print ‘current person list:%s‘ % person
print ‘current removelist:%s‘ % removelist

print ‘left person list:%s‘ % person

java 版:
import java.util.ArrayList;
import java.util.List;

public class TestShushu {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    List<Integer> list = new ArrayList<Integer>();
    for(int i=1;i<4;i++) {
        list.add(i);
    }
    System.out.println(list);

    int index = 0;
    while(list.size() > 1) {
        List<Integer> removelist = new ArrayList<Integer>();
        for(int i: list) {
            index ++;
            if(index == 3) {
                removelist.add(i);
                //System.out.println(removelist);
                index = 0;
            }
        }
        System.out.println("要删除的数组" + removelist);
        list.removeAll(removelist);
        System.out.println("当前数组元素:" + list);

    }
    System.out.println("最后留下的那个编号:" + list);

}

}

python 实现N个人,从1数到3,数到3的出局,来回循环,最后剩下一个人

标签:数组   remove   lis   append   string   range   index   main   元素   

原文地址:http://blog.51cto.com/dreamgirl1314/2351212

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!