如下题:
    在一只热气球上有15个日本人和15个美国人,由于热气球超重,必须要跳下去15个人。按照商讨的规则,从第一个人开始数起,第九个人被选做要跳下的人,以此类推。最终热气球上只剩下15个美国人,试编写一段程序,打印出那15个日本人的位置。

解决方案 »

  1.   

    昏,这是最基础的约瑟夫环阿开一个length=30的数组,初始状态置0,点到置1,
    i=(i+9)%30基本上就是这样
      

  2.   

    package com.pss.salix;import java.util.ArrayList;
    import java.util.List;import com.pss.util.prints.Conica;/*
     * 约瑟夫环。
     * @author George
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class Joshphus {

    public static void main(String[] args) {
    String[] japaneseAndAmerican = new String[30];
    for(int i=0; i < japaneseAndAmerican.length; i++) {
    japaneseAndAmerican[i] = String.valueOf(i+1);
    }
    int startPerson = 1;
    int deadCode = 9;
    List outList = joshphus(japaneseAndAmerican, startPerson, deadCode);
    Conica.pl(outList);
    } public static List joshphus(String[] str, int start, int code) {

    List list = new ArrayList();
    if(start<=0 || start>str.length)
    {
    System.out.println("You can do anything cause you can not find a start position");
    System.out.println("Person number is " +str.length);
    System.out.println("Your start position is " + start);
    return null;
    }
    int persons = str.length;
    int[] p = new int[persons];
    int out = 0;
    int index = start - 1;
    int count;
    int indexDead = 0; while (out < persons) {
    count = 0; for (; count != code;) {
    if (p[index] == 0)
    count++;
    indexDead = index++;
    if (index == persons)
    index = 0;
    }
    p[indexDead] = 1;
    list.add(str[indexDead]);
    out++;
    }

    return list;
    }
    }
      

  3.   

    The result is as follows:
    [9, 18, 27, 6, 16, 26, 7, 19, 30, 12, 24, 8, 22, 5, 23, 11, 29, 17, 10, 2, 28, 25, 1, 4, 15, 13, 14, 3, 20, 21]
      

  4.   

    又是YOSEFU问题嘛
    我来给一段,刚写的,运行成功,楼主可以参考
      import java.io.*;
    public class C{
    public static class Ren{
      private int weizhi;
      Ren next;
      public Ren(){}
      public Ren(int i){
       weizhi=i;
      }
    }
      public static void main(String[] args) throws IOException {
        Ren a1=new Ren(1);
        Ren list=new Ren();
        list=a1;
        for(int i=2;i<=30;i++){
         Ren a=new Ren(i);
         list.next=a;
         list=list.next;
        }
        list.next=a1;
      for(int j=1;j<=15;j++){
        for(int m=1;m<=8;m++){
          list=list.next;
      }
       System.out.println("第"+list.next.weizhi+"个人是日本人");
       list.next=list.next.next;
      }
     }
      
      }
      

  5.   

    //我的实现代码
    package com.pclib.www;public class Josephus {
    public static void main(String args[]) {
    int p[] = new int[30]; // 全部
    int r[] = new int[15]; // 结果
    for (int i = 0; i < p.length; i++) {
    p[i] = i;
    }
    int stat = 0;
    for (int i = 1, j = 0, k=1;; i++) {
    if (stat >= 15) {
    break;
    }
    if (p[i%30]==-1) continue;
    k = k % 9;
    if (k == 0) {
    stat++;
    System.out.print(p[i%30]+" ");
    p[i%30] = -1;
    }
    k++;
    }
    }
    }
    //输出结果:9 18 27 6 16 26 7 19 0 12 24 8 22 5 23
      

  6.   

    #include <stdio.h>
    #include <malloc.h>struct node {
        int key;
        struct node *next;
    };
    int main() {
        int i,j;
        struct node *p,*head,*temp;
        head=(struct node*)malloc(sizeof(struct node));
        head->key=1;
        p=head;
        for(i=2;i<=30;i++) {
            temp=(struct node*)malloc(sizeof(struct node));
            temp->key=i;
            p->next=temp;
            p=p->next;
        }
        p->next=head;
        for(i=0;i<15;i++)
        {
         for(j=1;j<=8;j++)
         {
         p=p->next;
         }
         printf("%d ",p->next->key);
         p->next=p->next->next;
        
        
        }
    }
      

  7.   

    public class ATest {
    public static void main(String[] args){
    int[] list=new int[30];
    int k=0;
    int m=0;
    for(int i=0;i<list.length;i++){
    list[i]=i+1;
    }
    for(int i=0;i<=list.length;){
    if(i==list.length){
    i=0;
    continue;
    }
    if(list[i]!=0){
    k++;
    }
    if(k==9){
    System.out.println(list[i]);
    list[i]=0;
    k=0;
    m++;
    if(m==15){
    break;
    }
    }
    i++;
    }
    }
    }