[英]How to proceed to find the maximum sum of 2 integers with replacing only one digit?
示例 1
a=1
b=90
answer will be 1+99 = 100
示例 2
23
45
answer will be 93+45 =138
注意:也可以有負數。 您不允許添加數字,只需替換單個數字即可獲得最大總和
用這個
if(a>b || a==b){
a=10*((a/10)+1)
return a;
}
else{
b=10*((b/10)+1)
return b;
}
讓我們假設第一個數字有 d1 位,第二個有 d2 位,為了簡單起見,讓我們進一步假設
d1 >= d2
和
k = d1 - d2
因此,k >= 0。如果較大數字的前 k 位是可修改的(可修改:如果該數字是正數,但該數字不是 9,或者該數字是負數),則優化該數字,如前所述.
否則,在隨后的數字中檢查是否有任何數字是可修改的,如果是,請計算您對兩位數字所做的更改之間的差異,然后選擇更改較大的那個。
當第一次修改完成后,工作應該停止。
當數字是個位數或相同時,解決方案非常簡單,即將第一位數字替換為9
。
當它們不同時,您必須將第一個或第二個數字更改為9
,如下所示:
public class Main {
public static void main(String[] args) {
// Tests
System.out.println(getSum(90, 1));
System.out.println(getSum(1, 90));
System.out.println(getSum(8, 80));
System.out.println(getSum(80, 8));
System.out.println(getSum(45, 23));
System.out.println(getSum(23, 45));
System.out.println(getSum(45, 45));
System.out.println(getSum(4, 4));
System.out.println(getSum(9, 9));
System.out.println(getSum(0, 0));
}
static int getSum(int a, int b) {
int result = 0;
String strA = String.valueOf(a);
String strB = String.valueOf(b);
if (a >= b) {
if (strA.length() == 1) {
result = 9 + a;
} else {
if (strA.length() > strB.length()) {
result = getResult(a, b);
} else {
result = getResult(b, a);
}
}
} else {
if (strB.length() == 1) {
result = 9 + b;
} else {
if (strB.length() > strA.length()) {
result = getResult(b, a);
} else {
result = getResult(a, b);
}
}
}
return result;
}
static int getResult(int a, int b) {
String strA = String.valueOf(a);
int firstDigA = Character.getNumericValue(strA.charAt(0));
return Integer.parseInt(firstDigA == 9 ? "99" + strA.substring(2) : "9" + strA.substring(1)) + b;
}
}
Output:
100
100
98
98
138
138
140
13
18
9
為了讓你更容易理解邏輯,我在下面寫了 function 的擴展形式getSum
:
static int getSum(int a, int b) {
int result = 0;
String strA = String.valueOf(a);
String strB = String.valueOf(b);
if (a >= b) {
if (strA.length() == 1) {
result = 9 + a;
} else {
if (strA.length() > strB.length()) {
int firstDigA = Character.getNumericValue(strA.charAt(0));
result = Integer.parseInt(firstDigA == 9 ? "99" + strA.substring(2) : "9" + strA.substring(1)) + b;
} else {
int firstDigB = Character.getNumericValue(strB.charAt(0));
result = Integer.parseInt(firstDigB == 9 ? "99" + strB.substring(2) : "9" + strB.substring(1)) + a;
}
}
} else {
if (strB.length() == 1) {
result = 9 + b;
} else {
if (strB.length() > strA.length()) {
int firstDigB = Character.getNumericValue(strB.charAt(0));
result = Integer.parseInt(firstDigB == 9 ? "99" + strB.substring(2) : "9" + strB.substring(1)) + a;
} else {
int firstDigA = Character.getNumericValue(strA.charAt(0));
result = Integer.parseInt(firstDigA == 9 ? "99" + strA.substring(2) : "9" + strA.substring(1)) + b;
}
}
}
return result;
}
這是 JavaScript 中的簡單遞歸(可輕松轉換為 Java 或 C++)。 這個想法是為每個數字選擇可能的最佳加法或減法(對於負數)。 復雜度為 O(log 10 n),其中 n 是較大的數字。
function improve(n){ if (n > -10 && n < 10) return n < 0? -n: 9 - n; return Math.max( 10 * improve(~~(n / 10)), n < 10? -(n % 10): 9 - (n % 10) ); } function f(a, b){ return a + b + Math.max(improve(a), improve(b)); } var abs = [ [1, 90], [23, 45], [-94, 5] ]; for (let [a, b] of abs){ console.log(a, b); console.log(f(a, b)); console.log(''); }
我有一個相當簡單的想法。 首先將兩個整數 `n1`、`n2` 轉換為 c-string `s1`、`s2`。 然后如果 s1[0] = '-' (n1 為負數) 改變`s1[1] = 0`,否則 (n1>0) 改變`s1[0] = 9`。 對於 c-string `s2` 也是如此。 最后比較哪個和更大:`n1 + stoi(s2)` 或 `n2 + stoi(s1)` 以確定要選擇的集合。
需要特別注意的是,對於 integer >0,並且以數字`999...` 開始,考慮到這種情況,我們使用 for 循環來更改不等於 `9` 的第一個數字。 如果所有數字都是“9”,我們對 integer 不做任何事情。
#include <iostream>
#include <fstream>
#include <cstring>
int main()
{
int n1, n2, a, b;
char s1[32], s2[32];
while (1)
{
std::cout << "\n input n1 & n2 = ";
std::cin >> n1 >> n2;
itoa(n1, s1, 10);
itoa(n2, s2, 10);
if (s1[0] == '-') s1[1] = '0';
else for (int i=0; i<strlen(s1); i++) {
if (s1[i] = '9') continue;
else {s1[i] = '9'; break;}
if (s2[0] == '-') s2[1] = '0';
else for (int i=0; i<strlen(s2); i++) {
if (s2[i] = '9') continue;
else {s2[i] = '9'; break;}
a = n1 + std::stoi(s2);
b = n2 + std::stoi(s1);
if (a > b) std::cout << "two integers: " << n1 << ", " << s2 <<std::endl;
else std::cout << "two integers: " << s1 << ", " << n2 <<std::endl;
}
return 0;
}
一些測試集:
$ ./a.exe
input n1 & n2 = 12 78
two integers: 92, 78
input n1 & n2 = -45 90
two integers: -05, 90
input n1 & n2 = -34 -78
two integers: -34, -08
input n1 & n2 = 23 9999
two integers: 93, 9999
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.