Question 5 of the 5th Java Group Provincial Competition of the Blue Bridge Cup: Right-angled Triangle

topic:

  The Pythagorean theorem, known in the West as the Pythagorean theorem, and the triangle it corresponds to is now called a right-angled triangle.
  It is known that the hypotenuse of a right triangle is an integer, and it is required that the other two sides must also be integers.
  Find the number of distinct right triangles that satisfy this condition.
【Data format】
Enter an integer n (0<n<10000000) to represent the length of the hypotenuse of a right triangle.
It is required to output an integer representing the number of right-angled triangles that satisfy the condition.
For example, input:
5 The
program should output:
1
For another example, input:
100 The
program should output:
2
For another example, input:
3 The
program should output:
0
Resource convention:
Peak memory consumption (including virtual machines) < 256M
CPU consumption < 1000ms
Please Strictly output according to the requirements, and do not superficially print superfluous content like: "Please enter...".
All code is placed in the same source file, after debugging, copy and submit the source code.
Note: Do not use the package statement. Do not use features of jdk1.7 and above.
Note: The name of the main class must be: Main, otherwise it will be processed as invalid code.


Answer:

public class Main {
	public static void main(String[] args){
		Scanner in=new Scanner(System.in);
		long n=in.nextInt();
// long star=System.currentTimeMillis();//Get system time
		long nn=n*n;
		long ii;
		int sum=0;
		int term=(int)(n/Math.sqrt(2))+1;
		for(int i=1;i<term;i++){
			ii = i * i;
			if(Math.sqrt(nn-ii)%1==0)
				sum++;
		}
		System.out.println(sum);
//		long end=System.currentTimeMillis();
//		System.out.println((end-star)+"ms");
	}
}

Analysis: The idea of ​​this question is unique, using the Pythagorean theorem, but the difficulty lies in reducing the time complexity, starting from 1 exhaustive, to the hypotenuse/root number 2 is the minimum number of exhaustive times, with two for corresponding to two The side length must be timed out, so consider another way: use one side length to find the other, and judge whether it has a decimal or not.

Finally, the surrentTimeMills() method is used to determine that the largest data (10000000) has no timeout (200ms).

Summarize:

1. In the time complexity problem, the first question is how to reduce the number of loop nesting, the second is the exhaustive number, and finally how to reduce the number of for internal statements.

2. Ensure that the value types of the variables are consistent as much as possible during operation, otherwise overflow problems may occur without knowing it. (For example: n*n, if n reaches 50000, overflow will occur and result will be wrong)

3.Math.sqrt(); The method is easy to use, but this method is for the operation of double type values. If you write a square root of int type by yourself, the time complexity will be greatly reduced. For example, if you open this question by yourself method, then the running time will be reduced by another level.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772390&siteId=291194637