如果数字的所有数字都不相同,那么数字是幸运的。如何检查给定的数字是否幸运。
例子:
Input: n = 983
Output: true
All digits are different
Input: n = 9838
Output: false
8 appears twice
强烈建议您最小化浏览器,然后自己尝试。
想法是遍历给定数字的每个数字,并将遍历的数字标记为已访问。由于数字的总数为10,因此我们需要一个大小仅为10的布尔数组来标记访问的数字。
以下是上述想法的实现。
C++
// C++ program to check if a given number is lucky
#include
using namespace std;
// This function returns true if n is lucky
bool isLucky(int n)
{
// Create an array of size 10 and initialize all
// elements as false. This array is used to check
// if a digit is already seen or not.
bool arr[10];
for (int i=0; i<10; i++)
arr[i] = false;
// Traverse through all digits of given number
while (n > 0)
{
// Find the last digit
int digit = n%10;
// If digit is already seen, return false
if (arr[digit])
return false;
// Mark this digit as seen
arr[digit] = true;
// REmove the last digit from number
n = n/10;
}
return true;
}
// Driver program to test above function.
int main()
{
int arr[] = {1291, 897, 4566, 1232, 80, 700};
int n = sizeof(arr)/sizeof(arr[0]);
for (int i=0; i
Java
// Java program to check if
// a given number is lucky
class GFG
{
// This function returns true if n is lucky
static boolean isLucky(int n)
{
// Create an array of size 10 and initialize all
// elements as false. This array is used to check
// if a digit is already seen or not.
boolean arr[]=new boolean[10];
for (int i = 0; i < 10; i++)
arr[i] = false;
// Traverse through all digits
// of given number
while (n > 0)
{
// Find the last digit
int digit = n % 10;
// If digit is already seen,
// return false
if (arr[digit])
return false;
// Mark this digit as seen
arr[digit] = true;
// Remove the last digit from number
n = n / 10;
}
return true;
}
// Driver code
public static void main (String[] args)
{
int arr[] = {1291, 897, 4566, 1232, 80, 700};
int n = arr.length;
for (int i = 0; i < n; i++)
if(isLucky(arr[i]))
System.out.print(arr[i] + " is Lucky \n");
else
System.out.print(arr[i] + " is not Lucky \n");
}
}
// This code is contributed by Anant Agarwal.
Python3
# python program to check if a
# given number is lucky
import math
# This function returns true
# if n is lucky
def isLucky(n):
# Create an array of size 10
# and initialize all elements
# as false. This array is
# used to check if a digit
# is already seen or not.
ar = [0] * 10
# Traverse through all digits
# of given number
while (n > 0):
#Find the last digit
digit = math.floor(n % 10)
# If digit is already seen,
# return false
if (ar[digit]):
return 0
# Mark this digit as seen
ar[digit] = 1
# REmove the last digit
# from number
n = n / 10
return 1
# Driver program to test above function.
arr = [1291, 897, 4566, 1232, 80, 700]
n = len(arr)
for i in range(0, n):
k = arr[i]
if(isLucky(k)):
print(k, " is Lucky ")
else:
print(k, " is not Lucky ")
# This code is contributed by Sam007.
C#
// C# program to check if
// a given number is lucky
using System;
class GFG {
// This function returns true if
// n is lucky
static bool isLucky(int n)
{
// Create an array of size 10
// and initialize all elements
// as false. This array is used
// to check if a digit is
// already seen or not.
bool []arr = new bool[10];
for (int i = 0; i < 10; i++)
arr[i] = false;
// Traverse through all digits
// of given number
while (n > 0)
{
// Find the last digit
int digit = n % 10;
// If digit is already seen,
// return false
if (arr[digit])
return false;
// Mark this digit as seen
arr[digit] = true;
// Remove the last digit
// from number
n = n / 10;
}
return true;
}
// Driver code
public static void Main ()
{
int []arr = {1291, 897, 4566, 1232,
80, 700};
int n = arr.Length;
for (int i = 0; i < n; i++)
if(isLucky(arr[i]))
Console.Write(arr[i] +
" is Lucky \n");
else
Console.Write(arr[i] +
" is not Lucky \n");
}
}
// This code is contributed by sam007.
PHP
0)
{
// Find the last digit
$digit = $n % 10;
// If digit is already seen,
// return false
if ($arr[$digit])
return false;
// Mark this digit as seen
$arr[$digit] = true;
// Remove the last digit
// from number
$n = (int)($n / 10);
}
return true;
}
// Driver Code
$arr = array(1291, 897, 4566,
1232, 80, 700);
$n = sizeof($arr);
for ($i = 0; $i < $n; $i++)
if(isLucky($arr[$i]))
echo $arr[$i] , " is Lucky \n";
else
echo $arr[$i] , " is not Lucky \n";
// This code is contributed by jit_t
?>
Javascript
输出:
1291 is not Lucky
897 is Lucky
4566 is not Lucky
1232 is not Lucky
80 is Lucky
700 is not Lucky