码个code给bangbang9814

来源: 2012-06-19 17:55:24 [旧帖] [给我悄悄话] 本文已被阅读:

天天码code,预防老年痴呆!把25人推广成N,把7推广成M,下面是个JAVA code:

public void func(int n, int m) {
        ArrayList<String> people = new ArrayList<String>();
        for (int i = 1; i <= n; i++)
        {
            people.add(new String(String.valueOf(i)));
        }
       
        int ct = 1;
        int index = 0;
       
        while(people.size() > 1)
        {
            if (ct%m==0)
            {
                index = index%people.size();
                people.remove(index);
                index --;
            }
            ct++;
            index++;
        }   
        System.out.println("Last Person: " + people.get(0).toString());
    }