给定N,大于或等于2且权重为W的整数的位数。任务是找到具有N位数且权重为W的整数的计数。 笔记 :权重定义为整数的连续数字之间的差值。
null
例子 :
Input : N = 2, W = 3Output : 6Input : N = 2, W = 4Output : 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,可能的总数是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”是位数。 下面是解决这个问题的逐步算法:
- 检查给定的重量(W)是正的还是负的。
- 如果为正,则从9中减去重量(W)。
- 如果为负,则将权重添加到10,然后更新新的权重。
- 对于n位整数,将10^(n-2)乘以此更新的权重。
- 这将给出满足此权重的整数数。
以下是上述方法的实施情况:
C++
// CPP program to find total possible numbers // with n digits and weight w #include <iostream> #include<cmath> 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
<?php // PHP program to find total possible // numbers with n digits and weight w // Function to find total possible // numbers with n digits and weight w function findNumbers( $n , $w ) { $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 // 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 |
Javascript
<script> // Javascript program to find total possible // numbers with n digits and weight w // Function to find total possible // numbers with n digits and weight w function findNumbers(n, w) { let 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 = Math.pow(10, n - 2); sum = (x * sum); return sum; } let 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 document.write(findNumbers(n, w)); </script> |
输出:
50
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END