给定一个正数n,我们需要找到x,使得1*n,2*n,3*n…。。x*n至少给出10位数字一次。如果没有这样的x是可能的打印-1。 例如:
null
Input : n = 1692Output : 3Explanation:n = 1692, we got the digits- 1, 2, 6, 92*n = 3384, we got the digits- 1, 2, 3, 4, 6, 8, 9.3*n = 5076, we got the digits- 1, 2, 3, 4, 5, 6, 7, 8, 9.At this step we got all the digits at leastonce. Therefore our answer is 3.Input : 1Output : 10Input : 0Output :-1
这里使用的想法很简单。我们从1开始,一直与n相乘,直到我们至少一次没有得到所有的10位数。为了跟踪每次迭代中出现的所有数字,我们使用了一个临时数组,大小为10,初始值为0。每当我们第一次得到一个数字时,我们将用1初始化它在数组中的索引。当所有数字访问一次时,我们就完成了。 下面是它的实现。
C++
// CPP program to find x such that 1*n, 2*n, 3*n // ...x * n have all digits from 1 to 9 at least // once #include <bits/stdc++.h> using namespace std; // Returns smallest value x such that 1*n, 2*n, // 3*n ...x * n have all digits from 1 to 9 at // least once int smallestX( int n) { // taking temporary array and variable. int temp[10] = { 0 }; if (n == 0) return -1; // iterate till we get all the 10 digits // at least once int count = 0, x = 0; for (x = 1; count < 10; x++) { int y = x * n; // checking all the digits while (y) { if (temp[y % 10] == false ) { count++; temp[y % 10] = true ; } y /= 10; } } return x - 1; } // driver function int main() { int n = 5; cout <<smallestX(n); return 0; } |
JAVA
// Java program to find x such // that 1*n, 2*n, 3*n...x * n // have all digits from 1 to 9 // at least once import java.io.*; import java.util.*; class GFG { // Returns smallest value x // such that 1*n, 2*n, 3*n // ...x * n have all digits // from 1 to 9 at least once public static int smallestX( int n) { // taking temporary // array and variable. int [] temp = new int [ 10 ]; for ( int i = 0 ; i < 10 ; i++) temp[i] = 0 ; if (n == 0 ) return - 1 ; // iterate till we get // all the 10 digits // at least once int count = 0 , x = 0 ; for (x = 1 ; count < 10 ; x++) { int y = x * n; // checking all // the digits while (y > 0 ) { if (temp[y % 10 ] == 0 ) { count++; temp[y % 10 ] = 1 ; } y /= 10 ; } } return x - 1 ; } // Driver Code public static void main(String args[]) { int n = 5 ; System.out.print(smallestX(n)); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
Python3
# Python3 program to find x such # that 1*n, 2*n, 3*n ...x * n # have all digits from 1 to 9 # at least once # Returns smallest value x such # that 1*n, 2*n, 3*n ...x * n # have all digits from 1 to 9 # at least once def smallestX(n): # taking temporary # array and variable. temp = [ 0 ] * 10 if (n = = 0 ): return - 1 # iterate till we get # all the 10 digits # at least once count = 0 x = 1 while (count < 10 ): y = x * n # checking all # the digits while (y> 0 ): if (temp[y % 10 ] = = 0 ): count + = 1 temp[y % 10 ] = 1 y = int (y / 10 ) x + = 1 return x - 1 # Driver code if __name__ = = '__main__' : n = 5 print (smallestX(n)) # This code is contributed # by mits |
C#
// C# program to find x such // that 1*n, 2*n, 3*n...x * n // have all digits from 1 to 9 // at least once using System; class GFG { // Returns smallest value x // such that 1*n, 2*n, 3*n // ...x * n have all digits // from 1 to 9 at least once public static int smallestX( int n) { // taking temporary // array and variable. int [] temp = new int [10]; for ( int i = 0; i < 10; i++) temp[i] = 0; if (n == 0) return -1; // iterate till we get // all the 10 digits // at least once int count = 0, x = 0; for (x = 1; count < 10; x++) { int y = x * n; // checking all the digits while (y > 0) { if (temp[y % 10] == 0) { count++; temp[y % 10] = 1; } y /= 10; } } return x - 1; } // dDriver Code static void Main() { int n = 5; Console.Write(smallestX(n)); } } // This code is contributed by mits |
PHP
<?php // PHP program to find x such // that 1*n, 2*n, 3*n ...x * n // have all digits from 1 to 9 // at least once // Returns smallest value x such // that 1*n, 2*n, 3*n ...x * n // have all digits from 1 to 9 // at least once function smallestX( $n ) { // taking temporary // array and variable. $temp = array_fill (0, 10, false); if ( $n == 0) return -1; // iterate till we get // all the 10 digits // at least once $count = 0; $x = 0; for ( $x = 1; $count < 10; $x ++) { $y = $x * $n ; // checking all // the digits while ( $y ) { if ( $temp [ $y % 10] == false) { $count ++; $temp [ $y % 10] = true; } $y = (int)( $y / 10); } } return $x - 1; } // Driver code $n = 5; echo smallestX( $n ); // This code is contributed // by mits ?> |
Javascript
<script> // Javascript program to find x such // that 1*n, 2*n, 3*n...x * n // have all digits from 1 to 9 // at least once // Returns smallest value x // such that 1*n, 2*n, 3*n // ...x * n have all digits // from 1 to 9 at least once function smallestX(n) { // taking temporary // array and variable. let temp = Array.from({length: 10}, (_, i) => 0); for (let i = 0; i < 10; i++) temp[i] = 0; if (n == 0) return -1; // iterate till we get // all the 10 digits // at least once let count = 0, x = 0; for (x = 1; count < 10; x++) { let y = x * n; // checking all // the digits while (y > 0) { if (temp[y % 10] == 0) { count++; temp[y % 10] = 1; } y /= 10; } } return x - 1; } // driver program let n = 5; document.write(smallestX(n)); </script> |
输出:
18
本文由 萨洛尼·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END