📜  相同数字总和的最小数字

📅  最后修改于: 2021-04-22 07:27:14             🧑  作者: Mango

给定一个以数组形式表示的数字,以便该数组的每个元素存储该数字的一位数字。也就是说,数字1234的数组将为arr [] = {1,2,3,4}。任务是找到大于给定数字但数字总和等于给定数字的最小数字。

为简单起见:考虑数字的最大长度为20。

例子

蛮力方法是:

  1. 从该数字开始,然后将数字递增1。
  2. 检查总和。如果总和相同,则返回数字。
  3. 否则返回第一步。

更好的方法是跳到O(n)时间复杂度的下一个数字,其中n是字符串的长度。

下面是上述方法的实现:

C++
// CPP program to find next greater number with
// same sum of digits.
#include 
using namespace std;
  
#define pb push_back
  
void getnext(int arr[], int n)
{
    // for storing 4 regions
    vector a1, a2, a3, a4;
  
    // trailing zeros region1
    int i = n - 1; // last index
    while (arr[i] == 0) 
    {
        a1.pb(0);
        i--;
    }
  
    // lowest region(region 2) not in region 1
    a2.pb(arr[i--]); 
  
    // Consecutive 9's   (region 3)
    while (arr[i] == 9) 
    {
        a3.pb(9);
        i--;
    }
  
    int j = 0;
    while (arr[j] == 0)
        j++; // Starting zeros
  
    while (j <= i) // 4th region
    {
        a4.pb(arr[j]);
        j++;
    }
  
    // Calculation of result
    j = a4.size() - 1;
  
    a4[j]++; // Region4 + 1
  
    a2[0]--; // Region2 -1
  
    int l = a1.size() + a2.size() + a3.size() + a4.size();
  
    // Calculating the result
    j = n-1;
    i = a3.size() - 1;
  
    // Copying the third region
    while (i >= 0)
    {
        arr[j--] = a3[i--];
    }
  
    // Copying the 2nd region
    i = a2.size() - 1;
    while (i >= 0) 
    {
        arr[j--] = a2[i--];
    }
  
    // Copying the 1st region
    i = a1.size() - 1;
    while (i >= 0) 
    {
        arr[j--] = a1[i--];
    }
  
    // Copying the 4th region
    i = a4.size() - 1;
    while (i >= 0) 
    {
        arr[j--] = a4[i--];
    }
  
    while (j >= 0)
        arr[j--] = 0;
}
  
int main()
{
    int arr[] = { 0, 0, 0, 0, 0, 0, 0, 3, 9, 7, 
                0, 0, 2, 9, 8, 9, 5, 9, 9, 0 };
    int n = sizeof(arr)/sizeof(arr[0]);
  
    getnext(arr, n); // Calling the function
  
    for (int i = 0; i < n; i++)
        cout << arr[i];
  
    return 0;
}


Java
// Java program to find next greater number with
// same sum of digits.
import java.util.*;
  
class GFG
{
  
static void getnext(int []arr, int n)
{
    // for storing 4 regions
    ArrayList a1 = new ArrayList();
    ArrayList a2 = new ArrayList();
    ArrayList a3 = new ArrayList();
    ArrayList a4 = new ArrayList();
  
    // trailing zeros region1
    int i = n - 1; // last index
    while (arr[i] == 0) 
    {
        a1.add(0);
        i--;
    }
  
    // lowest region(region 2) not in region 1
    a2.add(arr[i--]); 
  
    // Consecutive 9's (region 3)
    while (arr[i] == 9) 
    {
        a3.add(9);
        i--;
    }
  
    int j = 0;
    while (arr[j] == 0)
        j++; // Starting zeros
  
    while (j <= i) // 4th region
    {
        a4.add(arr[j]);
        j++;
    }
  
    // Calculation of result
    j = a4.size() - 1;
  
    a4.set(j,a4.get(j) + 1); // Region4 + 1
  
    a2.set(0,a2.get(0) - 1); // Region2 -1
  
    //int l = a1.size() + a2.size() + a3.size() + a4.size();
  
    // Calculating the result
    j = n - 1;
    i = a3.size() - 1;
  
    // Copying the third region
    while (i >= 0)
    {
        arr[j--] = (int)a3.get(i);
        i--;
    }
  
    // Copying the 2nd region
    i = a2.size() - 1;
    while (i >= 0) 
    {
        arr[j--] = (int)a2.get(i);
        i--;
    }
  
    // Copying the 1st region
    i = a1.size() - 1;
    while (i >= 0) 
    {
        arr[j--] = a1.get(i);
        i--;
    }
  
    // Copying the 4th region
    i = a4.size() - 1;
    while (i >= 0) 
    {
        arr[j--] = a4.get(i);
        i--;
    }
  
    while (j >= 0)
        arr[j--] = 0;
}
  
// Driver code
public static void main (String[] args) 
{
    int []arr = { 0, 0, 0, 0, 0, 0, 0, 3, 9, 7, 
                0, 0, 2, 9, 8, 9, 5, 9, 9, 0 };
    int n = arr.length;
  
    getnext(arr, n); // Calling the function
  
    for (int i = 0; i < n; i++)
        System.out.print(arr[i]);
}
}
  
// This code is contributed by mits


Python3
# Python3 program to find next greater number with
# same sum of digits.
  
def getnext(arr, n):
  
    # for storing 4 regions
    a1=[];
    a2=[];
    a3=[];
    a4=[];
  
    # trailing zeros region1
    i = n - 1; # last index
    while (arr[i] == 0):
        a1.append(0);
        i-=1;
  
    # lowest region(region 2) not in region 1
    a2.append(arr[i]);
    i-=1;
  
    # Consecutive 9's (region 3)
    while (arr[i] == 9):
        a3.append(9);
        i-=1;
  
    j = 0;
    while (arr[j] == 0):
        j+=1; # Starting zeros
  
    while (j <= i): # 4th region
        a4.append(arr[j]);
        j+=1;
  
    # Calculation of result
    j = len(a4) - 1;
  
    a4[j]+=1; # Region4 + 1
  
    a2[0]-=1; # Region2 -1
  
    l = len(a1) + len(a2) + len(a3) + len(a4);
  
    # Calculating the result
    j = n-1;
    i = len(a3) - 1;
  
    # Copying the third region
    while (i >= 0):
        arr[j] = a3[i];
        j-=1;
        i-=1;
  
    # Copying the 2nd region
    i = len(a2) - 1;
    while (i >= 0):
        arr[j] = a2[i];
        j-=1;
        i-=1;
  
    # Copying the 1st region
    i = len(a1) - 1;
    while (i >= 0):
        arr[j] = a1[i];
        j-=1;
        i-=1;
  
    # Copying the 4th region
    i = len(a4) - 1;
    while (i >= 0):
        arr[j] = a4[i];
        j-=1;
        i-=1;
  
    while (j >= 0):
        arr[j] = 0;
        j-=1;
  
# Driver code
arr = [ 0, 0, 0, 0, 0, 0, 0, 3, 9, 7, 0,
            0, 2, 9, 8, 9, 5, 9, 9, 0 ];
n = len(arr);
  
getnext(arr, n); # Calling the function
  
for i in range(0,n):
    print(arr[i],end="");
  
# This code is contributed by mits


C#
// C# program to find next greater number with
// same sum of digits.
using System;
using System.Collections;
  
class GFG
{
  
static void getnext(int []arr, int n)
{
    // for storing 4 regions
    ArrayList a1 = new ArrayList();
    ArrayList a2 = new ArrayList();
    ArrayList a3 = new ArrayList();
    ArrayList a4 = new ArrayList();
  
    // trailing zeros region1
    int i = n - 1; // last index
    while (arr[i] == 0) 
    {
        a1.Add(0);
        i--;
    }
  
    // lowest region(region 2) not in region 1
    a2.Add(arr[i--]); 
  
    // Consecutive 9's (region 3)
    while (arr[i] == 9) 
    {
        a3.Add(9);
        i--;
    }
  
    int j = 0;
    while (arr[j] == 0)
        j++; // Starting zeros
  
    while (j <= i) // 4th region
    {
        a4.Add(arr[j]);
        j++;
    }
  
    // Calculation of result
    j = a4.Count - 1;
  
    a4[j] = (int)a4[j] + 1; // Region4 + 1
  
    a2[0] = (int)a2[0] - 1; // Region2 -1
  
    //int l = a1.Count + a2.Count + a3.Count + a4.Count;
  
    // Calculating the result
    j = n - 1;
    i = a3.Count - 1;
  
    // Copying the third region
    while (i >= 0)
    {
        arr[j--] = (int)a3[i];
        i--;
    }
  
    // Copying the 2nd region
    i = a2.Count - 1;
    while (i >= 0) 
    {
        arr[j--] = (int)a2[i];
        i--;
    }
  
    // Copying the 1st region
    i = a1.Count - 1;
    while (i >= 0) 
    {
        arr[j--] = (int)a1[i];
        i--;
    }
  
    // Copying the 4th region
    i = a4.Count - 1;
    while (i >= 0) 
    {
        arr[j--] = (int)a4[i];
        i--;
    }
  
    while (j >= 0)
        arr[j--] = 0;
}
  
// Driver code
static void Main()
{
    int []arr = { 0, 0, 0, 0, 0, 0, 0, 3, 9, 7, 
                0, 0, 2, 9, 8, 9, 5, 9, 9, 0 };
    int n = arr.Length;
  
    getnext(arr, n); // Calling the function
  
    for (int i = 0; i < n; i++)
        Console.Write(arr[i]);
}
}
  
// This code is contributed by mits


PHP
= 0)
    {
        $arr[$j--] = $a3[$i--];
    }
  
    // Copying the 2nd region
    $i = count($a2) - 1;
    while ($i >= 0) 
    {
        $arr[$j--] = $a2[$i--];
    }
  
    // Copying the 1st region
    $i = count($a1) - 1;
    while ($i >= 0) 
    {
        $arr[$j--] = $a1[$i--];
    }
  
    // Copying the 4th region
    $i = count($a4) - 1;
    while ($i >= 0) 
    {
        $arr[$j--] = $a4[$i--];
    }
  
    while ($j >= 0)
        $arr[$j--] = 0;
}
  
        // Driver code
    $arr = array( 0, 0, 0, 0, 0, 0, 0, 3, 9, 7, 
                0, 0, 2, 9, 8, 9, 5, 9, 9, 0 );
    $n = count($arr);
  
    getnext($arr, $n); // Calling the function
  
    for ($i = 0; $i < $n; $i++)
        echo $arr[$i];
  
// This code is contributed by mits
?>


输出:
00000003970029896089