在数字的世界里,存在一类非常特殊的数,它们被称为“间谍数”。如果一个数的所有数位之和等于所有数位之积,那么我们就称这个数为间谍数。让我们一起来探索这个有趣的数学概念,并看看如何在代码中识别它们。
示例解析:
> 输入: 1412
> 输出: 是间谍数
> 解释:
> 位数之和 = (1 + 4 + 1 + 2) = 8
> 位数之积 = (1 4 1 * 2) = 8
> 因为,和 == 积 == 8
> 输入: 132
> 输出: 是间谍数
> 解释:
> 位数之和 = (1 + 3 + 2) = 6
> 位数之积 = (1 3 2) = 6
> 因为,和 == 积 == 6
下面,我们将通过多种编程语言来实现这一逻辑,展示如何通过代码来“侦测”这些间谍数。
C++ 实现
// CPP program to check
// a spy number
#include
using namespace std;
// Function to
// check spy number
bool checkSpy(int num)
{
int digit, sum = 0,
product = 1;
while (num > 0)
{
digit = num % 10;
// getting sum of digits
sum += digit;
// getting product of digits
product *= digit;
num = num / 10;
}
if (sum == product)
return true;
else
return false;
}
// Driver code
int main()
{
int num = 1412;
if (checkSpy(num))
cout << "The number is "
<< "a Spy number"
<< endl;
else
cout << "The number is "
<< "NOT a spy number"
<< endl;
return 0;
}
Java 实现
// Java program to
// check Spy number
import java.util.*;
class GFG
{
// boolean function to
// check Spy number
static boolean checkSpy(int input)
{
int digit, sum = 0,
product = 1;
while (input > 0)
{
digit = input % 10;
// getting the
// sum of digits
sum += digit;
// getting the product
// of digits
product *= digit;
input = input / 10;
}
// Comparing the
// sum and product
if (sum == product)
return true;
else
return false;
}
// Driver code
public static void main(String args[])
{
int input = 1412;
if (checkSpy(input))
System.out.println("The number is "+
"a Spy number");
else
System.out.println("The number is "+
"NOT a Spy number");
}
}
Python3 实现
# Python program to check
# Spy number
# Function to check
# Spy number
def checkSpy(num):
sums = 0
product = 1
while num>0:
digit = num % 10
# getting the
# sum of digits
sums = sums + digit
# getting the product
# of digits
product = product * digit
num = num // 10
if sums == product:
return True
else:
return False
# Driver Code
num = 1412
if (checkSpy(num)):
print("The number is a Spy Number")
else:
print("The number is NOT a spy number")
C# 实现
// C# program to check
// Spy number
using System;
class GFG
{
// boolean function to
// check Spy number
static bool checkSpy(int input)
{
int digit, sum = 0,
product = 1;
while (input > 0)
{
digit = input % 10;
// getting the sum
// of digits
sum += digit;
// getting the product
// of digits
product *= digit;
input = input / 10;
}
// Comparing the sum
// and product
if (sum == product)
return true;
else
return false;
}
// Driver code
public static void Main()
{
int input = 1412;
if (checkSpy(input))
Console.WriteLine("The number is " +
"a Spy number");
else
Console.WriteLine("The number is " +
"NOT a Spy number");
}
}
// This code is Contributed by vt_m.
PHP 实现
0)
{
$digit = $num % 10;
// getting sum
// of digits
$sum += $digit;
// getting product
// of digits
$product *= $digit;
$num = $num / 10;
}
if ($sum == $product)
return 1;
else
return -1;
}
// Driver code
$num = 1412;
if (checkSpy($num))
echo "The number is a ".
"Spy number","
";
else
echo "The number is NOT ".
"a spy number","
";
// This code is contributed by ajit.
?>
JavaScript 实现
“`javascript
// Javascript program to check
// a spy number
// Function to
// check spy number
function checkSpy(num)
{
let digit, sum = 0,
product = 1;
while (num > 0)
{
digit = num % 10;