关注
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;
}
查看原帖
点赞 评论
相关推荐
牛客热帖
正在热议
# 25届秋招总结 #
375928次浏览 3731人参与
# 如果再来一次,你还会选择这个工作吗? #
100637次浏览 1012人参与
# 北方华创开奖 #
64516次浏览 503人参与
# 地方国企笔面经互助 #
6002次浏览 14人参与
# 简历被挂麻了,求建议 #
2524136次浏览 33455人参与
# 发工资后,你做的第一件事是什么 #
4581次浏览 19人参与
# 阿里云管培生offer #
46115次浏览 1376人参与
# ai智能作图 #
10839次浏览 174人参与
# 我的实习求职记录 #
6103833次浏览 83825人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
8742次浏览 177人参与
# 上班到公司第一件事做什么? #
14565次浏览 164人参与
# 阿里求职进展汇总 #
71785次浏览 780人参与
# 如何写一份好简历 #
615761次浏览 8700人参与
# 听到哪句话就代表面试稳了or挂了? #
96695次浏览 810人参与
# 还记得你第一次面试吗? #
29402次浏览 413人参与
# 华为工作体验 #
109341次浏览 852人参与
# 面试体验感最好的是哪家? #
91526次浏览 921人参与
# 网易求职进展汇总 #
38876次浏览 334人参与
# 如果有时光机,你最想去到哪个年纪? #
27022次浏览 559人参与
# 腾讯求职进展汇总 #
205474次浏览 1687人参与
# 硬件兄弟们 甩出你的华为奖状 #
75688次浏览 612人参与
# 上班苦还是上学苦呢? #
106677次浏览 865人参与