📌  相关文章
📜  使用从数组中选择的总和小于S的数字的索引来形成最小数

📅  最后修改于: 2021-05-04 13:35:12             🧑  作者: Mango

给定一个数组arr []和一个整数S ,任务是从数组中选择最大的数字数,以使数字的总和小于S并使用它们的索引形成最小的数字
注意:可以多次选择任何元素。

例子:

方法:想法是找到可以选择的元素的最大数量,该数量可以使用

\frac{Sum}{X}

最后,可以通过选择每个位数中数字中的最小位数来计算可以选择多次的最小索引。

下面是上述方法的实现:

C++
// C++ implementation to find
// minimum number which
// have a maximum length
 
#include 
using namespace std;
 
// Function to find the
// minimum number which
// have maximum length
string max_number(int arr[], int sum)
{
    int frac[9];
    int maxi = INT_MIN;
    string ans;
    int pos;
 
    // Find Maximum length
    // of number
    for (int i = 0; i < 9; i++) {
        frac[i] = sum / arr[i];
        if (frac[i] > maxi) {
            pos = i;
            maxi = frac[i];
        }
    }
 
    ans.insert(0,
               string(maxi,
                      (pos + 1) + '0'));
    sum -= maxi * arr[pos];
 
    // Find minimum number WHich
    // have maximum length
    for (int i = 0; i < maxi; i++) {
        for (int j = 1; j <= 9; j++) {
 
            if (sum
                    + arr[pos]
                    - arr[j - 1]
                >= 0) {
 
                ans[i] = (j + '0');
                sum += arr[pos]
                       - arr[j - 1];
                break;
            }
        }
    }
 
    if (maxi == 0) {
        return 0;
    }
    else {
        return ans;
    }
}
 
// Driver Code
int main()
{
    int arr[9] = { 3, 4, 2, 4, 6,
                   5, 4, 2, 3 };
    int s = 13;
    cout << max_number(arr, s);
    return 0;
}


Java
// Java implementation to find
// minimum number which
// have a maximum length
class GFG{
 
// Function to find the
// minimum number which
// have maximum length
static String max_number(int arr[], int sum)
{
    int frac[] = new int[9];
    int maxi = Integer.MIN_VALUE;
     
    StringBuilder ans = new StringBuilder();
    int pos = 0;
 
    // Find Maximum length
    // of number
    for(int i = 0; i < 9; i++)
    {
        frac[i] = sum / arr[i];
        if (frac[i] > maxi)
        {
            pos = i;
            maxi = frac[i];
        }
    }
 
    for(int i = 0; i < maxi; i++)
    {
        ans.append((char)((pos + 1) + '0'));
    }
 
    sum -= maxi * arr[pos];
 
    // Find minimum number WHich
    // have maximum length
    for(int i = 0; i < maxi; i++)
    {
        for(int j = 1; j <= 9; j++)
        {
            if (sum + arr[pos] - arr[j - 1] >= 0)
            {
                ans.setCharAt(i, (char)(j + '0'));
         
                sum += arr[pos] - arr[j - 1];
                break;
            }
        }
    }
     
    if (maxi == 0)
    {
        return "0";
    }
    else
    {
        return ans.toString();
    }
}
 
// Driver Code
public static void main(String str[])
{
    int arr[] = { 3, 4, 2, 4, 6,
                  5, 4, 2, 3 };
    int s = 13;
     
    System.out.println(max_number(arr, s));
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 implementation to find
# minimum number which
# have a maximum length
 
# Function to find the
# minimum number which
# have maximum length
def max_number(arr, sum):
     
    frac = [0]*9
    maxi = -10**9
 
    pos = 0
     
    # Find Maximum length
    # of number
    for i in range(9):
        frac[i] = sum // arr[i]
         
        if (frac[i] > maxi):
            pos = i
            maxi = frac[i]
 
    an = str((pos + 1)) * maxi
     
    #print(an)
    sum -= maxi * arr[pos]
 
    ans = [i for i in an]
 
    # Find minimum number WHich
    # have maximum length
    for i in range(maxi):
        for j in range(1, 10):
            if (sum + arr[pos] - arr[j - 1] >= 0):
                ans[i] = str(j)
                sum += arr[pos] - arr[j - 1]
                break
 
    if (maxi == 0):
        return 0
    else:
        return "".join(ans)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 3, 4, 2, 4, 6,
            5, 4, 2, 3 ]
    s = 13
     
    print(max_number(arr, s))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation to find
// minimum number which
// have a maximum length
using System;
using System.Text;
class GFG{
 
// Function to find the
// minimum number which
// have maximum length
static String max_number(int []arr,
                         int sum)
{
  int []frac = new int[9];
  int maxi = int.MinValue;
  StringBuilder ans =
                new StringBuilder();
  int pos = 0;
 
  // Find Maximum length
  // of number
  for(int i = 0; i < 9; i++)
  {
    frac[i] = sum / arr[i];
    if (frac[i] > maxi)
    {
      pos = i;
      maxi = frac[i];
    }
  }
 
  for(int i = 0; i < maxi; i++)
  {
    ans.Append((char)((pos + 1) + '0'));
  }
  sum -= maxi * arr[pos];
 
  // Find minimum number WHich
  // have maximum length
  for(int i = 0; i < maxi; i++)
  {
    for(int j = 1; j <= 9; j++)
    {
      if (sum + arr[pos] -
          arr[j - 1] >= 0)
      {
        ans[i] = (char)(j + '0');
 
        sum += arr[pos] - arr[j - 1];
        break;
      }
    }
  }
 
  if (maxi == 0)
  {
    return "0";
  }
  else
  {
    return ans.ToString();
  }
}
 
// Driver Code
public static void Main(String []str)
{
  int []arr = {3, 4, 2, 4, 6,
               5, 4, 2, 3};
  int s = 13;
  Console.WriteLine(max_number(arr, s));
}
}
 
// This code is contributed by 29AjayKumar


输出:
133333