编程语言
首页 > 编程语言> > python 实现N个人,从1数到3,数到3的出局,来回循环,最后剩下一个人

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

作者:互联网

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);

}

}

标签:index,数到,python,list,出局,System,person,removelist,out
来源: http://blog.51cto.com/dreamgirl1314/2351212