第一题没思路,第二题差十分钟写出下面代码,后面的没看,我哭了
美团4.23第二题
import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); LinkedList<StringBuilder> list = new LinkedList<>(); while (in.hasNextLine()) { StringBuilder sb = new StringBuilder(in.next()); // list.add(sb); f(sb); } } public static void f(StringBuilder sb){ StringBuilder res = new StringBuilder(); int len = sb.length(); // System.out.println(sb.toString()); Stack<Character> stack = new Stack<>(); int j = 0; if (sb.charAt(0)=='-') { stack.push('('); stack.push('$'); j=1; }else { stack.push('$'); } for (int i = j; i < len; i++) { if (sb.charAt(i)=='.'){ len =len>i+3? i+3:len; } stack.push(sb.charAt(i)); } int count = 0; if (sb.charAt(0)=='-'){ res.append(")"); } while (!stack.empty()){ res.append(stack.pop()); if (!stack.empty()&&stack.peek()=='.'){ res.append(stack.pop()); while (stack.peek()!='$'){ if (stack.peek()!='$')res.append(stack.pop()); else break; if (stack.peek()!='$')res.append(stack.pop()); else break; if (stack.peek()!='$')res.append(stack.pop()); else break; if (stack.peek()!='$') res.append(","); else break; } } } res = res.reverse(); System.out.println(res); } }