[kata](5kyu) 约瑟夫战死排序(排列)

之前一直不懂,今天百度了下,发下kyu是级别的意思,dan是段的意思,级别数值越小越强,段数数值越大越强.

原题  https://www.codewars.com/kata/josephus-permutation/train/java

 

import java.util.*;

public class Josephus {
  public static <T> List<T> josephusPermutation(final List<T> items, final int k) {

     //if you kill youself, you will go to hell.
     if(k==0){
       return null;
     }
     int killIndex = k - 1;
     List<T> itemsB = new ArrayList<T>();
     itemsB = items;
     ArrayList<T> hellList = new ArrayList<T>();
     while(itemsB.size()!=0){
       int maxIndex = itemsB.size()-1;
       
       if(killIndex>maxIndex){
          //if k is odd, the killIndex maybe at even!
          if(maxIndex==0){
            killIndex = 0;
          }else{
            killIndex = killIndex % itemsB.size();
          }
          
        }
        
          hellList.add(itemsB.get(killIndex));
          itemsB.remove(killIndex);
          killIndex+=(k-1);
        
       }
       return hellList;
  }
}

 

另一道题:https://www.codewars.com/kata/josephus-survivor 未解

 

 

第一道5kyu题,做得有点艰难

今天又点了一道5kyu的题,测试通过了,但是解题只是求出了多个数值(传入list)的最小公倍数,而不是传入的表示形式上的分子/分母的公共分母,没有考虑到即使分母/分子不是正整数,但乘以分子可能得到正整数的可能.

 

posted @ 2018-08-18 18:05  ukyo--君君小时候  阅读(498)  评论(0编辑  收藏  举报