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 }
1 package com.huawei.test;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Stack;
6 /**
7 * 第三题,输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。
8 * 例如: 输入:3+8×2/9-2
9 * 输出:2
10 *
11 * @author Administrator
12 *
13 */
14
15 public class HuaWeiTest {
16
17 public static void main(String[] args) {
18
19 System.out.println(getMyRet("3+8/2*2"));
20 }
21
22 public static int getMyRet(String str) {
23 String expression = str.trim();
24 Stack<Integer> operandStack = new Stack<Integer>();
25 Stack<Character> operatorStack = new Stack<Character>();
26
27 for(int i = 0; i < expression.length(); i++){
28 char ch = expression.charAt(i);
29 if(ch == '+' || ch == '-' ){//
30 while(!operatorStack.isEmpty() && (operatorStack.peek() == '+'
31 || operatorStack.peek() == '-'
32 || operatorStack.peek() == '*'
33 || operatorStack.peek() == '/')){
34 processAnOperator(operandStack, operatorStack);
35 }
36 operatorStack.push(ch);
37 }
38 else if (ch == '*' || ch == '/'){
39 while(!operatorStack.isEmpty() && (
40 operatorStack.peek() == '*' || operatorStack.peek() == '/')){
41 processAnOperator(operandStack, operatorStack);
42 }
43 operatorStack.push(ch);
44 }
45 else{
46 operandStack.push(ch - '0');
47 }
48 }
49 while(!operatorStack.isEmpty()){
50 processAnOperator(operandStack, operatorStack);
51 }
52 return operandStack.pop();
53 }
54 public static void processAnOperator(Stack<Integer> operandStack, Stack<Character> operatorStack){
55 char op = operatorStack.pop();
56 int op1 = operandStack.pop();
57 int op2 = operandStack.pop();
58 switch (op) {
59 case '+':
60 operandStack.push(op1 + op2);
61 break;
62 case '-':
63 operandStack.push(op2 - op1);
64 break;
65 case '*':
66 operandStack.push(op1 * op2);
67 break;
68 case '/':
69 operandStack.push(op2 / op1);
70 break;
71 default:
72 break;
73 }
74 }
75 }