Java 如何检查一个数字是否是迷人的数字
迷人的数字可以定义为一个数字,当它与2和3相乘,然后将乘积与数字本身连接起来,其中包含从1到9的所有数字。
要成为一个迷人的数字,它应该是三位数或三位数以上。
举几个示例
示例1
输入数字是327
让我们使用迷人数字的逻辑来检查它-
327 * 2 = 654
327 * 3 = 981
连接“654”+“981”+“327”= 654981327
因此,327是一个有趣的数。
示例2
输入的数字是192
让我们通过使用有趣数的逻辑来检查它−
192 * 2 = 384
192 * 3 = 576
连接 “384” + “576” + “192” = 384576192
因此,327是一个迷人的数字。
示例3
输入数字为241
让我们使用迷人数字的逻辑来检查它
241 * 2 = 482
241 * 3 = 723
连接字符串”482″ + “723” + “241” = 482723241
因此,241不是一个有趣的数字
其他有趣数字的示例包括192, 1920, 2019, 327等等
步骤
- 第一步 - 通过初始化或用户输入获得一个整数。
- 
第二步 - 检查该数是否为三位数。 
- 
第三步 - 将该数乘以2和3。 
- 
第四步 - 将两个乘积与数本身连接起来。 
- 
第五步 - 现在查找从1到9的所有数字是否都出现在该数中。如果是,则该数是一个有趣的数字。 
多种方法
我们提供了不同的解决方法。
- 通过使用静态输入值
- 
通过使用用户定义的方法 
让我们逐个查看程序及其输出。
方法1:通过使用静态输入值
在这个方法中,程序中初始化一个整数值,然后使用算法检查一个数字是否是一个有趣的数字。
示例
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
方法2:使用用户定义的方法
在这种方法中,程序中初始化一个整数值,并将该数字作为参数传递给用户定义的方法,然后在方法内部通过算法来检查一个数字是否是迷人数。
示例
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
在本文中,我们探讨了如何通过使用不同的方法来检查一个数字是否是令人着迷的数字。
 极客笔记
极客笔记