猿问

质数检查java

编写一个程序来读取 n 个数字。指定为输入的第一个数字将是 n。接下来,程序应该读取 n 个整数。


程序应该检查每个数字是否是素数以及它的反向是否是素数。


按升序显示所有这些数字。


考虑下面的输入和输出示例:


输入:7 11 12 23 19 7 113 101


输出:


7 11 101 113


我的代码


public class Prime {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int x = sc.nextInt();

        int temp;


        int[] a = new int [x];

        int[] r = new int [x];

        int[]c = new int[a.length+r.length];

        int[] rev = new int [x];


        for(int i=0;i<x;i++){

            a[i] = sc.nextInt();

            rev[i]=a[i];

        }


        for(int i = 0; i < a.length; i++) {

            while(rev[i] != 0) {

                r[i] = r[i] * 10;

                r[i] = r[i] + rev[i]%10;

                rev[i] = rev[i]/10;

            }

        }


        for(int i = 0; i < a.length; i++) {

            boolean isPrime = true;

            for (int j = 2; j < i; j++) {

                if((a[i]%j==0) || (r[i]%j==0)) {

                    isPrime = false;

                    break;

                }

            }

            if(isPrime) 

                System.out.println(a[i]);

            System.out.println(r[i]);

        }

    }

}

在某个地方我被卡住了我不知道如何消除重复的否,最后如何合并数组,并且当我给出输入和 2 时它打印 1 和 2 作为素数


PIPIONE
浏览 89回答 4
4回答

叮当猫咪

您需要使用 TreeSet - 它只包含不同的元素并以排序形式给出结果。您可以参考以下代码-&nbsp; Set<Integer> set = new TreeSet<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < a.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean isPrime = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isPrime(a[i]) && isPrime(r[i]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set.add(a[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }Iterator it = set.iterator();&nbsp; &nbsp; &nbsp; &nbsp; while(it.hasNext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(it.next() + " ");还创建一个检查素数的函数 -private static boolean isPrime(int num) {&nbsp; &nbsp; &nbsp; &nbsp; if(num==1) return false;&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 2; i <= num/2; ++i)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(num % i == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }

阿晨1998

你可以试试下面的代码。希望对你有帮助import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Set;import java.util.TreeSet;public class PrimeNumberTest {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List<Integer> list = new ArrayList<>(Arrays.asList(7, 11, 12, 23, 19, 7 ,113, 101));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//To remove duplicates&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Set<Integer> set = new TreeSet<>(list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(getPrimeNumbers(set).toString().replaceAll(",", "").replace("]", "").replace("[", ""));&nbsp; &nbsp; }&nbsp; &nbsp; //Method to get unique ordered set of prime numbers&nbsp; &nbsp; private static Set<Integer> getPrimeNumbers(Set<Integer> set) {&nbsp; &nbsp; &nbsp; &nbsp; Set<Integer> resultList=new TreeSet<>();&nbsp; &nbsp; &nbsp; &nbsp; set.forEach(ele->{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //check for prime&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isPrime(ele)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if prime number check for reverse and if true, add to result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(isPrime(reverserNumb(ele)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resultList.add(ele);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; return resultList;&nbsp; &nbsp; }&nbsp; &nbsp; private static boolean isPrime(int num){&nbsp; &nbsp; &nbsp; &nbsp; if(num<2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; // Check for even numbers&nbsp; &nbsp; &nbsp; &nbsp; if (num % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return num == 2;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; // Check for odd numbers&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 3; i*i <= num; i += 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (num % i == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; private static int reverserNumb(int num) {&nbsp; &nbsp; &nbsp; &nbsp; return Integer.valueOf(new StringBuilder(String.valueOf(num)).reverse().toString());&nbsp; &nbsp; }}

呼啦一阵风

试试这个代码是否有效,希望它有帮助代码:&nbsp; &nbsp; &nbsp; &nbsp; import java.util.ArrayList;&nbsp; &nbsp; &nbsp; &nbsp; import java.util.Collections;&nbsp; &nbsp; &nbsp; &nbsp; import java.util.Scanner;&nbsp; &nbsp; &nbsp; &nbsp; public class PrimeNumbers {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; static ArrayList<Integer> prime = new ArrayList<>();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner user_input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter Number of Integers: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int number_count = user_input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] numbers = new int[number_count];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < numbers.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter Integer: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numbers[i] = user_input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Values Entered: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < numbers.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(numbers[i] + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkPrime(numbers[i],false); //false don't reverse&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkPrime(numbers[i],true); //true reverse value to check if it is also prime&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("\nList of prime numbers: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(prime);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int n : prime){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(n + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(prime.isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("no prime numbers on list\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //check for duplicates&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; static void insertValueToPrime(int n){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int p : prime){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(n == p){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prime.add(n);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; static void checkPrime(int n,boolean isReverse) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i, m = 0, flag = 0,realn = n;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isReverse){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n = reverseNumber(n);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m = n / 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n == 0 || n == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //no a prime number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (i = 2; i <= m; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n % i == 0) {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; not a prime number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (flag == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insertValueToPrime(realn);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; static int reverseNumber(int n){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String reverse = "",str=""+n;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = str.length() - 1; i >= 0; i--)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reverse = reverse + str.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Integer.parseInt(reverse);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }

繁星点点滴滴

这是使用 √n 方法进行质数测试的代码static boolean isPrime(int n){//corner caseif (n <= 1) return false;&nbsp;if (n <= 3) return true;&nbsp;//middle 5 numberif (n % 2 == 0 || n % 3 == 0) return false;for (int i = 5; i * i <= n; i = i + 6)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n % i == 0 || n % (i + 2) == 0)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp;&nbsp;return true;}使用可set用于删除重复元素
随时随地看视频慕课网APP

相关分类

Java
我要回答