【问题标题】:How to proceed to find the maximum sum of 2 integers with replacing only one digit?如何通过仅替换一位数字来找到 2 个整数的最大和?
【发布时间】:2021-03-24 12:56:27
【问题描述】:

示例 1

a=1
b=90
answer will be 1+99 = 100

示例 2

23
45
answer will be 93+45 =138

注意:也可以有负数。 不允许添加数字,只需替换单个数字即可获得最大和

【问题讨论】:

  • 欢迎来到 SO。首先尝试提出自己的程序,然后在遇到错误或意外行为时提出问题。解决有趣的编程问题需要海报至少展示一个有趣的程序(顺便说一下其中的问题)。

标签: java c++ algorithm math


【解决方案1】:

使用这个

 if(a>b || a==b){
   a=10*((a/10)+1)
   return a;
}
else{
b=10*((b/10)+1)
return b;
}

【讨论】:

    【解决方案2】:

    让我们假设第一个数字有 d1 位,第二个有 d2 位,为了简单起见,让我们进一步假设

    d1 >= d2
    

    k = d1 - d2
    

    所以,k >= 0。如果较大数字的前k位是可修改的(可修改:如果数字是正数,但数字不是9,或者数字是负数),则优化修改该数字,如前所述。

    否则,在后续数字中检查是否有任何数字是可修改的,如果是,请计算您对两位数字所做的更改之间的差异,然后选择更改较大的那个。

    当第一次修改完成后,工作应该停止。

    【讨论】:

      【解决方案3】:

      这是一个简单的 JavaScript 递归(很容易翻译成 Java 或 C++)。这个想法是为每个数字选择可能的最佳加法或减法(对于负数)。复杂度为 O(log10 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('');
      }

      【讨论】:

        【解决方案4】:

        我有一个相当简单的想法。首先将两个整数 `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)` 以确定要选择的集合。

        需要特别注意的是对于 >0 且以数字开头的整数`999...` 考虑到这种情况,我们使用 for 循环来更改不等于 `9` 的第一个数字。如果所有数字都是“9”,我们对整数不做任何事情。

        #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
        

        【讨论】:

          猜你喜欢
          • 2011-03-22
          • 1970-01-01
          • 2015-12-14
          • 2023-04-06
          • 2018-08-27
          • 1970-01-01
          • 1970-01-01
          • 2023-04-03
          • 1970-01-01
          相关资源
          最近更新 更多