1 /* 2 * 第二题,输入字符串长度,字符串,计数m。从前往后计数,当数到m个元素时,m个元素出列,同时将该元素赋值给m, 3 * 然后从下一个数计数循环,直到所有数字都出列,给定的数全部为大于0的数字。输出出队队列。 4 * 例如: 输入:len=4 str="3,1,2,4" m=7 5 * 输出:2,3,1,4 6 */ 7 public static String getOutString(int len, String str, int m) { 8 String[] ss = str.split(","); 9 ArrayList<Integer> arrayInt = new ArrayList<Integer>(); 10 Stack<Integer> stackInt = new Stack<Integer>(); 11 String s = ""; 12 13 for(int i = 0; i < ss.length; i++){ 14 arrayInt.add(Integer.parseInt(ss[i])); 15 } 16 17 while(arrayInt.size() != 0){ 18 19 20 int n = m % arrayInt.size();//第n个数,在arrayInt 中是第 n - 1 个数 21 if(n == 0) { 22 n = arrayInt.size(); 23 } 24 m = arrayInt.get(n - 1); 25 //arrayInt.remove(n - 1); 26 //arrayInt 中 第n个数开始 27 for(int i = n - 2; i >= 0; i--){ 28 stackInt.push(arrayInt.get(i)); 29 } 30 for(int i = arrayInt.size() - 1; i > n -1; i--){ 31 stackInt.push(arrayInt.get(i)); 32 } 33 System.out.println(arrayInt.remove(n - 1) +""); 34 for(int i = 0; i < arrayInt.size(); i++){ 35 arrayInt.set(i, stackInt.pop()); 36 } 37 } 38 return s; 39 }
1 import java.util.ArrayList; 2 import java.util.List; 3 4 5 public class HuaWeiTest { 6 7 public static void main(String[] args) { 8 int len=4; 9 String str="3,1,2,4"; 10 int m=7; 11 HuaWeiTest hwt = new HuaWeiTest(); 12 System.out.println(hwt.getOutString(len, str, m)); 13 } 14 public String getOutString(int len, String str, int m) { 15 String ret =""; 16 String[] arr = str.split(","); 17 List<String> ls = new ArrayList<String>(); 18 for(int i=0;i<len;i++) { 19 ls.add(arr[i]); 20 } 21 for(int i=0;i<len;i++) { 22 int temp = (m-1)%ls.size(); 23 ret += ls.get(temp); 24 m = Integer.parseInt(ls.get(temp))+temp; 25 ls.remove(temp); 26 } 27 return ret; 28 } 29 }
原文:http://www.cnblogs.com/wanghui390/p/3585372.html