Sohu swim strokes written in autumn 2019 ------------ mathematics Zhenti

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_44313091/article/details/102554577

Mathematics

Title Description

Math teacher is teaching the concept of small smooth and small tour two primes. In order to help consolidate the knowledge of the two men, a number of teachers to say, require a small tour and small smooth cooperation, each person speak a prime number, making the two prime numbers and say exactly equal to the number of teachers to say. Please write a program to calculate the number of prime numbers of the two people say. For example, the teacher said 10 small and small smooth travel can say two pairs of prime numbers, respectively (5,5) and (3,7)

Enter a description

Includes an input integer n (3 <= n <= 1000)

Output Description

The number of qualifying output pair primes

SAMPLE INPUT

10

SAMPLE INPUT

2

 

package Test;
import java.util.Collection;
import java.util.HashMap;
import java.util.Scanner;

public class TestString{
	
	public static void main(String[] args) {
		Scanner s = new Scanner (System.in);
		HashMap Map =new HashMap ();
		int n =s.nextInt();
		for(int j =1;j<=(n/2)+1; j++) {
			if(isPrime(j)&& isPrime(n-j)) {    //如果j和(n-j)都为真 ,则进入下一个判断
				if(!Map.containsKey(j)&& !Map.containsKey(n-j)) {   
				Map.put(j, n-j);
//				System.out.println(j + " " + (n-j) );
				}
			}
		}
		System.out.println(Map.size());
	}
	
	
	private static boolean isPrime (int target) {    //isPrime 判断传入是否为素数
		if(target==1) {
			return false;
		}
		if(target<=3) {
			return true;
		}
		for(int i = 2; i<= (target/2); i++) {
			if(target%i==0) {
				return false;
			}
		}
		return true;
		
	}
	
}

Problem-solving ideas:

First to enter the for loop, isPrime main function is to determine whether the incoming number is a prime number, and if it returns true, otherwise false. Second if statement in order to prevent duplicate numbers appear, such as (2,3) and (3,2) considered a kind. Map Key utilizes digital judgment can not be repeated, the number of final output.

Guess you like

Origin blog.csdn.net/qq_44313091/article/details/102554577