华为0506笔试
菜鸡只会做第二题,还没做全对,被输出格式卡住了,过了90,事后想起来了可能不对的地方,发出来记录下
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String exp = sc.next(); String[] nums = exp.split("\\+"); StringBuilder ss = new StringBuilder(); if (nums[0].contains(".") && nums[1].contains(".")) { String[] n1 = nums[0].split("\\."); String[] n2 = nums[1].split("\\."); int r = add1(n1[1], n2[1]); add2(n1[0], n2[0], r); ss.append(sb2).append(".").append(sb); } else if (nums[0].contains(".") && !nums[1].contains(".")) { String[] n1 = nums[0].split("\\."); add2(n1[0], nums[1], 0); ss.append(sb2).append(".").append(n1[1]); } else if (!nums[0].contains(".") && nums[1].contains(".")) { String[] n2 = nums[1].split("\\."); add2(nums[0], n2[0], 0); ss.append(sb2).append(".").append(n2[1]); } else { add2(nums[0], nums[1], 0); ss.append(sb2); } // 记录! + ! = 0 这种特殊情况 boolean flag1 = false; for (int i = 0; i < ss.length(); i++) { char c = ss.charAt(i); if (c != '0' && c != '.') { flag1 = true; } } if (flag1) { int l = 0; int r = ss.length() - 1; char[] rc = ss.toString().toCharArray(); while (l < rc.length) { if (rc[l] != '0') { break; } l++; } // 记录 25.000这种特殊情况 boolean flag = false; if (ss.toString().contains(".")) { for (int i = ss.indexOf(".") + 1; i < ss.length(); i++) { if (ss.charAt(i) != '0') { flag = true; break; } } if (flag) { while (r >= 0) { if (rc[r] != '0' || rc[r + 1] == '.') { break; } r--; } } } if (ss.toString().contains(".") && !flag) { System.out.println(String.valueOf(rc).substring(l, ss.indexOf("."))); } else { System.out.println(String.valueOf(rc).substring(l, r + 1)); } } else { System.out.println(0); } } // 小数答案 static StringBuilder sb = new StringBuilder(); // 整数答案 static StringBuilder sb2 = new StringBuilder(); public static int add1(String s1, String s2) { //StringBuilder sb = new StringBuilder(); char[] s1c = s1.toCharArray(); char[] s2c = s2.toCharArray(); if (s1c.length < s2c.length) { char[] temp = s1c; s1c = s2c; s2c = temp; } int index = s1c.length - 1; int r = 0; while (index >= 0) { if (index > s2c.length - 1) { sb.insert(0, s1c[index]); } else { if ('0' <= s1c[index] && s1c[index] <= '9' && '0' <= s2c[index] && s2c[index] <= '9') { int t = Integer.parseInt(s1c[index] + "") + Integer.parseInt(s2c[index] + "") + r; sb.insert(0, t % 10); r = t / 10; } else { int sum = sadd(s1c[index], s2c[index]) + r; sb.insert(0, sum % 10); r = sum / 10; } } index--; } return r; } public static void add2(String s1, String s2, int m) { char[] s1c = s1.toCharArray(); char[] s2c = s2.toCharArray(); int i1 = s1c.length - 1; int i2 = s2c.length - 1; int r = 0; while (i1 >= 0 || i2 >= 0) { char t1 = i1 >= 0 ? s1c[i1] : '0'; char t2 = i2 >= 0 ? s2c[i2] : '0'; if ('0' <= t1 && t1 <= '9' && '0' <= t2 && t2 <= '9') { int sum = Integer.parseInt(t1+"") + Integer.parseInt(t2 + "") + r; if (i1 == s1c.length - 1 && i2 == s2c.length - 1) { sum += m; } sb2.insert(0, sum % 10); r = sum / 10; } else { int sum = sadd(t1, t2) + r; if (i1 == s1c.length - 1 && i2 == s2c.length - 1) { sum += m; } sb2.insert(0, sum % 10); r = sum / 10; } i1--; i2--; } if (r != 0) { sb2.insert(0, r); } } public static int sadd(char s1, char s2) { int res; if (s1 == '!' && s2 == '!') { res = 0; } else if (s1 == '!' && s2 == '@' || s1 == '@' && s2 == '!') { res = 13; } else if (s1 == '!' && s2 == '#' || s1 == '#' && s2 == '!') { res = 4; } else if (s1 == '@' && s2 == '@') { res = 7; } else if (s1 == '@' && s2 == '#' || s2 == '@' && s1 == '#') { res = 20; } else { res = 5; } return res; } }#我的实习求职记录#