📜  权重为W的N位整数的数量

📅  最后修改于: 2021-04-29 12:33:27             🧑  作者: Mango

在给定N的情况下,大于或等于2且权重为W的整数的位数。任务是找到具有N位数和权重W的整数的计数。

注意:权重定义为整数的连续数字之间的差。

例子

Input : N = 2, W = 3
Output : 6

Input : N = 2, W = 4
Output : 5

在上面的示例中,权重等于3的可能的2位整数总数将为6。例如数字14的权重为3(4-1),数字25、36、47、58、69的权重为3。我们将找到逻辑,如果将权重增加为2位数字的5,则此类数字的总数为5。如果权重为2位数字的6,则总数为4,然后为3,依此类推。上。另外,如果我们增加位数。假设n等于3且权重3,那么对于n等于4且权重3,n的总数可能为60和600。

位数|重量->可能的总数

2|2 —> 7 2|3 —> 6 2|4 —> 5 2|5 —> 4 2|6 —> 3 2|7 —> 2 2|8 —> 1
3|2 —> 70 3|3 —> 60 3|4 —> 50 3|5 —> 40 3|6 —> 30 3|7 —> 20 3|8 —> 10
4|2 —>700 4|3 —>600 4|4 —>500 4|5 —>400 4|6 —>300 4|7 —>200 4|8 —>100

如上表所示,随着位数的增加,权重为“ w”的数字数量遵循一种模式,它以10 ^(n-2)的倍数变化,其中“ n” ‘是位数。

以下是解决此问题的分步算法:

  1. 检查给定的权重(W)是正数还是负数。
  2. 如果为正,则从9减去权重(W)。
  3. 如果为负,则将权重添加到10,然后更新新的权重。
  4. 对于n位整数,将10 ^(n-2)乘以此更新的权重。
  5. 这将使我们获得满足此权重的整数的数量。

下面是上述方法的实现:

C++
// CPP program to find total possible numbers
// with n digits and weight w
  
#include 
#include
  
using namespace std;
  
// Function to find total possible numbers
// with n digits and weight w
int findNumbers(int n, int w)
{
    int x = 0, sum = 0;
  
    // When Weight of an integer is Positive
    if (w >= 0 && w <= 8) {
        // Subtract the weight from 9
        x = 9 - w;
    }
    // When weight of an integer is negative
    else if (w >= -9 && w <= -1) {
        // add the weight to 10 to make it positive
        x = 10 + w;
    }
      
    sum = pow(10, n - 2);
    sum = (x * sum);
      
    return sum;
}
  
// Driver code
int main()
{
    int n, w;
  
    // number of digits in an
    // integer and w as weight
    n = 3, w = 4;
      
    // print the total possible numbers
    // with n digits and weight w
    cout << findNumbers(n, w);;
  
    return 0;
}


Java
// Java program to find total 
// possible numbers with n
// digits and weight w
  
class GFG
{
      
// Function to find total 
// possible numbers with n
// digits and weight w
static int findNumbers(int n, int w)
{
    int x = 0, sum = 0;
  
    // When Weight of an 
    // integer is Positive
    if (w >= 0 && w <= 8) 
    {
        // Subtract the weight from 9
        x = 9 - w;
    }
      
    // When weight of an
    // integer is negative
    else if (w >= -9 && w <= -1) 
    {
        // add the weight to 10 
        // to make it positive
        x = 10 + w;
    }
      
    sum = (int)Math.pow(10, n - 2);
    sum = (x * sum);
      
    return sum;
}
  
// Driver code
public static void main(String args[])
{
    int n, w;
  
    // number of digits in an
    // integer and w as weight
    n = 3;
    w = 4;
      
    // print the total possible numbers
    // with n digits and weight w
    System.out.println(findNumbers(n, w));
} 
}
  
// This code is contributed 
// by ankita_saini


Python3
# Python3 program to find total possible 
# numbers with n digits and weight w
  
# Function to find total possible 
# numbers with n digits and weight w
def findNumbers(n, w):
  
    x = 0; 
    sum = 0;
  
    # When Weight of an integer
    # is Positive
    if (w >= 0 and w <= 8):
        # Subtract the weight from 9
        x = 9 - w;
      
    # When weight of an integer
    # is negative
    elif (w >= -9 and w <= -1):
          
        # add the weight to 10 to
        # make it positive
        x = 10 + w;
      
    sum = pow(10, n - 2);
    sum = (x * sum);
      
    return sum;
  
# Driver code
  
# number of digits in an
# integer and w as weight
n = 3; 
w = 4;
  
# print the total possible numbers
# with n digits and weight w
print(findNumbers(n, w));
  
# This code is contributed 
# by mits


C#
// C# program to find total possible 
// numbers with n digits and weight w
using System;
  
class GFG
{
      
// Function to find total possible 
// numbers with n digits and weight w 
static int findNumbers(int n, int w) 
{ 
    int x = 0, sum = 0; 
  
    // When Weight of an integer
    // is Positive 
    if (w >= 0 && w <= 8) 
    { 
        // Subtract the weight from 9 
        x = 9 - w; 
    } 
      
    // When weight of an 
    // integer is negative 
    else if (w >= -9 && w <= -1) 
    { 
        // add the weight to 10 
        // to make it positive 
        x = 10 + w; 
    } 
      
    sum = (int)Math.Pow(10, n - 2); 
    sum = (x * sum); 
      
    return sum; 
} 
  
// Driver code 
static public void Main ()
{
    int n, w; 
      
    // number of digits in an 
    // integer and w as weight 
    n = 3; 
    w = 4; 
      
    // print the total possible numbers 
    // with n digits and weight w 
    Console.WriteLine(findNumbers(n, w)); 
} 
} 
  
// This code is contributed by jit_t


PHP
= 0 && $w <= 8)
    {
        // Subtract the weight from 9
        $x = 9 - $w;
    }
      
    // When weight of an integer
    // is negative
    else if ($w >= -9 && $w <= -1)
    {
        // add the weight to 10 to
        // make it positive
        $x = 10 + $w;
    }
      
    $sum = pow(10, $n - 2);
    $sum = ($x * $sum);
      
    return $sum;
}
  
// Driver code
  
// number of digits in an
// integer and w as weight
$n = 3; $w = 4;
  
// print the total possible numbers
// with n digits and weight w
echo findNumbers($n, $w);
  
// This code is contributed 
// by Akanksha Rai


输出:
50