自己机试竟然没做出来, 鄙视下自己呀~~~
import java.util.ArrayList;
import java.util.List;public class YueSeFu {
/**
* 约瑟夫问题 "3 a b c d e f g" 把第一个数当做m,后面字符数到m出来,直到都出来。。输出字符串 形如:“c f b g e a d”
*/
public static void main(String[] args) {
String s = "3 a b c d e f g";
doString(s);
}
public static void doString(String s){
int m = Integer.parseInt(s.charAt(0)+"");
//System.out.println(countNum);
String[] ss = s.split(" ");
List<String> l = new ArrayList<String>();
List<String> l2 = new ArrayList<String>();
for(int i=1;i<ss.length; i++){
l.add(ss[i]);
}
//System.out.println(l);
int left=ss.length-1,count=0,index=0;
while(left>0){
count++;
if(count == m){
left--;
count = 0;
l2.add(l.get(index));
l.remove(index);
index--;
}
index++;
if(index == l.size()) index = 0;
}
StringBuffer sb = new StringBuffer();
for(int i=0; i<l2.size(); i++){
sb.append(l2.get(i));
}
System.out.println(sb);
for(int i=1; i<sb.length(); i+=2){
sb.insert(i, " ");
}
System.out.println(sb);
}
}