
迷人数字可以定义为一个数字,与2和3相乘,然后与该数字本身连接后的结果包含从1到9的所有数字。
要使一个数字成为一个令人着迷的数字,它应该是三位数或三位数以上。
输入数字为327
让我们用迷人数字的逻辑来检查一下 -
立即学习“Java免费学习笔记(深入)”;
327 * 2 = 654 327 * 3 = 981
连接“654”+“981”+“327”= 654981327
因此,327 是一个令人着迷的数字。
输入数字为192
让我们用迷人数字的逻辑来检查一下 -
立即学习“Java免费学习笔记(深入)”;
192 * 2 = 384 192 * 3 = 576
连接“384”+“576”+“192”= 384576192
因此,327 是一个令人着迷的数字。
输入数字为241
让我们用迷人数字的逻辑来检查一下 -
立即学习“Java免费学习笔记(深入)”;
241 * 2 = 482 241 * 3 = 723
连接“482”+“723”+“241”= 482723241
因此,241并不是一个令人着迷的数字
其他一些令人着迷的数字示例包括 192、1920、2019、327 等。
第 1 步 - 通过初始化或用户输入获取整数。
第 2 步 - 检查这是否是三位数。
第 3 步 - 将数字乘以 2 和 3。
第 4 步 - 将两个乘积与数字本身连接起来。
第 5 步 - 现在查找数字中是否存在从 1 到 9 的所有数字。如果确实如此,那么这将是一个令人着迷的数字
我们通过不同的方式提供了解决方案。
通过使用静态输入值
通过使用用户定义的方法
让我们一一看看该程序及其输出。
在这种方法中,在程序中初始化一个整数值,然后通过使用算法我们可以检查一个数字是否是一个迷人的数字。
public class Main {
public static void main(String args[]){
// Initialized an integer value
int num = 327;
System.out.println("Given number: "+num);
// Store the product of the numbers in the variables
int prod1 = num*2;
int prod2 = num*3;
// Concatenate the numbers
String concatNum = prod1+""+prod2+num;
// Boolean value to store the result
boolean flag = true;
// Loops from 1 to 9
for(char c = '1'; c <= '9'; c++) {
// COunt holds the number of times a digit occurs
int count = 0;
//loop counts the frequency of each digit
for(int i = 0; i < concatNum.length(); i++) {
char ch = concatNum.charAt(i);
//compares the character of concatNum with i
if(ch == c)
//increments the count by 1 if the specified condition returns true
count++;
}
// Checks if all the digits are present in the number
if(count > 1 || count == 0) {
flag = false;
break;
}
}
// Prints the result
if(flag)
System.out.println("Fascinating number");
else
System.out.println("Not a fascinating number");
}
}
Given number: 327 Fascinating number
在这种方法中,在程序中初始化一个整数值,并将该数字作为参数传递到用户定义的方法中,然后在该方法中使用算法我们可以检查一个数字是否是一个迷人的数字。
public class Mai {
static boolean fascinatingNum(int num){
// Store the product of the numbers in the variables
int prod1 = num*2;
int prod2 = num*3;
// Concatenate the numbers
String concatNum = prod1+""+prod2+num;
// Boolean value to store the result
boolean flag = true;
// Loops from 1 to 9
for(char c = '1'; c <= '9'; c++) {
// COunt holds the number of times a digit occurs
int count = 0;
//loop counts the frequency of each digit
for(int i = 0; i < concatNum.length(); i++){
char ch = concatNum.charAt(i);
//compares the character of concatNum with i
if(ch == c)
//increments the count by 1 if the specified condition returns true
count++;
}
// Checks if all the digits are present in the number
if(count > 1 || count == 0) {
flag = false;
break;
}
}
return flag;
}
public static void main(String args[]){
// Initialized an integer value
int num = 327;
System.out.println("Given number: "+num);
// Calls the user defined method and stores the result in res variable
boolean res = fascinatingNum(num);
// Prints the result
if(res)
System.out.println("Fascinating number");
else
System.out.println("Not a fascinating number");
}
}
Given number: 327 Fascinating number
在本文中,我们探讨了如何使用不同的方法在 Java 中检查一个数字是否是一个有趣的数字。
以上就是如何在Java中检查一个数字是否是迷人的数字?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号