posts - 2, comments - 1, trackbacks - 0, articles - 9
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Joseph Circle

Posted on 2006-11-24 14:56 ljf2999 阅读(100) 评论(0)  编辑  收藏 所属分类: Arithmetic

 

 1 public   class  Josephus  {
 2
 3      public   static   void  main(String[] args)  {
 4          if (args.length < 2 ) {
 5             System.out.println( " Input N and M. " );
 6              return ;
 7         }

 8          int  n  =  Integer.parseInt(args[ 0 ]);
 9          int  m  =  Integer.parseInt(args[ 1 ]);
10          int  point = 0 ,number = 1 ;
11         List < Integer >  list  =   new  ArrayList < Integer > ();
12          for ( int  i = 1 ;i <= n; i ++ ) {
13              // 初始化链表
14             list.add(i);
15         }

16
17          while (list.size() > 1 ) {
18              if (number % m == 0 ) {
19                 list.remove(point);
20                  -- point;
21             }

22              ++ point; // 指针后移
23              ++ number;
24              if (point > list.size() - 1 ) {
25                  // 指针越界重新开始
26                 point = 0 ;
27             }

28         }

29         
30
31         System.out.println(list.get( 0 ));
32         
33     }

34 }

35

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


网站导航: