趋势科技第一题

图片说明
借用下群里老哥发的题图

import java.util.Scanner;

public class Adder {
    // please don't modify any code below.
    public static void main(String[] args) {
//        Scanner sc = new Scanner(System.in);

//        String num1 = sc.nextLine();
//        String num2 = sc.nextLine();


        String num1 = "199.999";
        String num2 = "0.001";
        String sum = add(num1, num2);
        System.out.println(sum);
//        System.out.println("2234567890223456789022345678902234567890.223456789022345678902234567890223456789");
    }

    private static String[] split(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '.') {
                return new String[]{str.substring(0, i), str.substring(i + 1)};
            }
        }
        return new String[]{str, ""};
    }

    private static String add(String num1, String num2) {
         // 坑1 :如何拿到正确的数
        String[] number1 = split(num1);
        String[] number2 = split(num2);
        String Integer1 = number1[0];
        String Integer2 = number2[0];
        String dot1 = number1[1];
        String dot2 = number2[1];
        int jw = 0;

        StringBuilder dotsb1 = new StringBuilder(dot1);
        StringBuilder dotsb2 = new StringBuilder(dot2);
        // 坑2:小数点尾数对不齐低的尾要补齐
        while (dotsb1.length() != dotsb2.length()) {
            if (dotsb1.length() < dotsb2.length()) {
                dotsb1.append('0');
            } else {
                dotsb2.append('0');
            }
        }
        dotsb1.reverse();
        dotsb2.reverse();

        StringBuilder integerRes = new StringBuilder();
        int len2 = dotsb1.length();
        boolean flag = true;
        for (int i = 0; i < len2; i++) {
            int tmp = jw;
            if (i < dotsb1.length()) {
                tmp = dotsb1.charAt(i) - '0' + tmp;
            }
            if (i < dotsb2.length()) {
                tmp = dotsb2.charAt(i) - '0' + tmp;
            }
            if (tmp >= 10) {
                jw = tmp / 10;
                tmp %= 10;
            } else {
                jw = 0;
            }
            char c = (char) (tmp + '0');
            // 坑3:刚开始加可能都是 0
            if (flag && c == '0') {
                continue;
            } else if (c != '0') {
                flag = false;
            }

            integerRes.append(c);
        }
        // 坑4:小数位可能全是 0
        if (!flag) {
            integerRes.append(".");
        }
        StringBuilder sb1 = new StringBuilder(Integer1);
        sb1.reverse();
        StringBuilder sb2 = new StringBuilder(Integer2);
        sb2.reverse();
        int len1 = sb1.length() > sb2.length() ? sb1.length() : sb2.length();
        for (int i = 0; i < len1; i++) {
            int tmp = jw;
            if (i < sb1.length()) {
                tmp = sb1.charAt(i) - '0' + tmp;
            }
            if (i < sb2.length()) {
                tmp = sb2.charAt(i) - '0' + tmp;
            }
            if (tmp >= 10) {
                jw = tmp / 10;
                tmp %= 10;
            } else {
                jw = 0;
            }
            integerRes.append((char) (tmp + '0'));
        }
        // 坑5:最后一个进位
        if (jw == 1) {
            integerRes.append('1');
        }
        // 最后倒回去
        return integerRes.reverse().toString();
    }
}
#趋势科技##笔试题目#
全部评论
我写了150行
点赞 回复 分享
发布于 2019-08-08 20:26
这类名错了跑出来了吗😂
点赞 回复 分享
发布于 2019-08-08 20:26
。。我也是又臭又长,还特么40%就没心情改了
点赞 回复 分享
发布于 2019-08-08 20:28
这tm是给人做的题吗?
点赞 回复 分享
发布于 2019-08-08 20:29
为啥我提交说我编译不过 改类名变成Main就变成百分之0  我自测用例也没问题啊……
点赞 回复 分享
发布于 2019-08-08 21:18
import java.util.Scanner; public class Main {     public static void main(String[] args) {         Scanner input = new Scanner(System.in);         String num1 = input.nextLine();         String num2 = input.nextLine();         String sum = add(num1, num2);         System.out.println(sum);     }     /*      * 1234567890123456789012345678901234567890.      * 1234567890123456789012345678901234567890      * 1000000000100000000010000000001000000000.      * 1000000000100000000010000000001000000000      */     /*      * 2234567890223456789022345678902234567890.      * 223456789022345678902234567890223456789      */     private static String[] split(String str) {         for (int i = 0; i < str.length(); i++) {             if (str.charAt(i) == '.') {                 return new String[] { str.substring(0, i), str.substring(i + 1) };             }         }         return new String[] { str, "" };     }     private static String add(String num1, String num2) {         /*String[] number1 = num1.split("\\.");         String[] number2 = num2.split("\\.");*/         String[] number1 = split(num1);         String[] number2 = split(num2);         String nums1 = number1[0];         String nums2 = number2[0];         String dot1 = number1[1];         String dot2 = number2[1];         int dlength1 = dot1.length();         int dlength2 = dot2.length();         int min;         int[] dnums = new int[dlength1 > dlength2 ? dlength1 : dlength2];         if (dlength1 > dlength2) {             min = dlength2;             for (int i = min; i < dlength1; i++) {                 dnums[i] = dot1.charAt(i) - '0';             }         } else {             min = dlength1;             for (int i = min; i < dlength2; i++) {                 dnums[i] = dot2.charAt(i) - '0';             }         }         min--;         int top = 0;         while (min >= 0) {             int sum = (dot1.charAt(min) + dot2.charAt(min) + dnums[min] - 2 * '0');             if (sum >= 10) {                 dnums[min] = sum % 10;                 if (min != 0) {                     dnums[--min] = sum / 10;                 } else {                     top = sum / 10;                 }             } else {                 dnums[min--] = sum;             }         }         int[] num = new int[(nums1.length() > nums2.length() ? nums1.length() : nums2.length()) + 1];         int i = nums1.length() - 1;         int j = nums2.length() - 1;         int k = num.length - 1;         num[k] = top;         while (i >= 0 && j >= 0) {             int sum = (nums1.charAt(i) + nums2.charAt(j) + num[k] - 2 * '0');             if (sum >= 10) {                 num[k] = sum % 10;                 num[--k] = sum / 10;             } else {                 num[k--] = sum;             }             i--;             j--;         }         if (i < 0 || j >= 0) {             while (j >= 0) {                 int sum = (nums2.charAt(j) + num[k]) - '0';                 if (sum >= 10) {                     num[k] = sum % 10;                     num[--k] = sum / 10;                 } else {                     num[k--] = sum;                 }                 j--;             }         } else if (i >= 0 || j < 0) {             while (i >= 0) {                 int sum = (nums1.charAt(i) + num[k]) - '0';                 if (sum >= 10) {                     num[k] = sum % 10;                     num[--k] = sum / 10;                 } else {                     num[k--] = sum;                 }                 i--;             }         }         StringBuilder sb = new StringBuilder();         StringBuilder sb1 = new StringBuilder();         boolean sign = false, sign1 = false;         for (int s : num) {             if (s != 0)                 sign = true;             if (sign) {                 sb.append(s);             }         }         for (int h = dnums.length - 1; h >= 0; h--) {             if (dnums[h] != 0)                 sign1 = true;             if (sign1) {                 sb1.append(dnums[h]);                 if (h == 0)                     sb1.append(".");             }         }         sb.append(sb1.reverse());         return sb.toString();     } } 思路是不难,代码量太大了吧!!!
点赞 回复 分享
发布于 2019-08-08 22:18
60% 又臭又长。。
点赞 回复 分享
发布于 2019-08-09 07:31
有短且精辟的代码吗?
点赞 回复 分享
发布于 2019-08-09 10:08
public static String add(String num1, String num2){         String result = null;         if (num1.contains(".")){             int des1 = getPoint(num1);             String subNum1 = num1.substring(0,des1);             String leftNum1 = num1.substring(des1+1);             if (num2.contains(".")){                 int des2  =getPoint(num2);                 String subNum2 = num2.substring(0,des2);                 String leftNum2 = num2.substring(des2+1);                 int max = leftNum1.length() > leftNum2.length()?leftNum1.length() : leftNum2.length();                 if (subNum1.length() > subNum2.length())                      subNum2 = addZeroAtLeft(subNum2,subNum1.length()-subNum2.length());                 else                     subNum1 = addZeroAtLeft(subNum1, subNum2.length()-subNum1.length());                 if (leftNum1.length() > leftNum2.length())                     leftNum2 = addZeroAtRight(leftNum2,leftNum1.length()-leftNum2.length());                 else                     leftNum1 = addZeroAtRight(leftNum1,leftNum2.length()-leftNum1.length());                 String newNum1 = subNum1+leftNum1;                 String newNum2 = subNum2+leftNum2;                  result = getAddResult(newNum1,newNum2,newNum1.length());                  StringBuffer sb = new StringBuffer(result);                  int pos = result.length();                  for (int i=0;i<max;i++){                      pos --;                  }                  sb.insert(pos,'.');                  result = new String(sb);             }             else {                 if (subNum1.length() > num2.length())                     num2 = addZeroAtLeft(num2,subNum1.length()-num2.length());                 else                     subNum1 = addZeroAtLeft(subNum1,num2.length()-subNum1.length());                 result = getAddResult(subNum1,num2,subNum1.length()) + "." + leftNum1;             }         }         else {             if (num2.contains(".")){                 int des2  =getPoint(num2);                 String subNum2 = num2.substring(0,des2);                 String leftNum2 = num2.substring(des2+1);                 if (num1.length() > subNum2.length())                     subNum2 = addZeroAtLeft(subNum2,num1.length()-subNum2.length());                 else                     num1 = addZeroAtLeft(num1,subNum2.length()-num1.length());                 result = getAddResult(num1,subNum2,num1.length()) + "." + leftNum2;             }             else{                 if (num1.length() > num2.length())                     num2 = addZeroAtLeft(num2,num1.length()-num2.length());                 else                     num1 = addZeroAtLeft(num1,num2.length()-num1.length());                 result = getAddResult(num1,num2,num1.length());             }         }         return result;     }     public static String addZeroAtLeft(String str, int len){         StringBuffer stringBuffer = new StringBuffer(str);         for (int i=0;i<len;i++)             stringBuffer.insert(0,'0');         return new String(stringBuffer);     }     public static String addZeroAtRight(String str, int len){         StringBuffer stringBuffer = new StringBuffer(str);         for (int i=0;i<len;i++)             stringBuffer.append('0');         return new String(stringBuffer);     }     public static String getAddResult(String num1, String num2, int len){         char[] result = new char[len+1];         for (int i=0;i<=len;i++)             result[i] = '0';         for (int i=len-1;i>=0;i--){             int a = Integer.parseInt(num1.charAt(i) + "");             int b = Integer.parseInt(num2.charAt(i) + "");             int c = Character.digit(result[i+1],10);             int sum = a + b + c;             result[i+1] = (char) ('0' + sum % 10);             result[i] = (char) ('0' + sum / 10);         }         StringBuffer sb = new StringBuffer();         for (int i = 0;i<=len;i++){             if (result[i]=='0'&&i==0)                 continue;             sb.append(result[i]);         }         return new String(sb);     }     public static int getPoint(String str){         int len = str.length();         int i;         for (i=len-1;i>0;i--){             if (str.charAt(i) == '.')                 break;         }         return i;     }
点赞 回复 分享
发布于 2019-08-09 10:39

相关推荐

在评审的大师兄很完美:像这种一般就是部门不匹配 转移至其他部门然后挂掉 我就是这样被挂了
点赞 评论 收藏
分享
牛客771574427号:恭喜你,华杰
点赞 评论 收藏
分享
3 10 评论
分享
牛客网
牛客企业服务