ACM Exercises-Pythagoras Triangle

Source: Internet
Author: User

Topic Description:

The famous mathematician Pythagoras may never have thought anyone would ever ask him such a question: Give an integer, how many triangles there are, the length of one side of it equals this integer, and the length of the other edges is also an integer. Since Pythagoras could not have foreseen the appearance of a computer, if he could not answer it, then who could blame him?  But now that you have a computer, you can't answer it. Input Format:

The first line has an integer n that represents how many data (1≤n≤20). Next n rows, each row represents one data. A data is an integer AI (1≤i≤n,1≤ai≤100). output Format:

Each data must have the corresponding output. There is a blank line between the output of two data. Do not add blank lines after the last test data output.
For each data, if no solution is found, a blank row is output. If we find the solution, we output all the right triangles that match the criteria. Each triangle occupies one row, outputting the other side of the triangle, you must first output the long edge, then a comma, and then output the short edge. Two triangles cannot have empty rows, and must be sorted in descending order of long edges. Input Sample:

2

20

Output Sample:

101,99

52,48

29,21

25,15

16,12


37,35

20,16

15,9

13,5

parsing:

When a is the hypotenuse, we lift the minimum edge, at which point the minimum edge is taken (1,SQRT (A2/2));

When a is for right-angled side, a2 = C2-b2, at this time the C-b,c+b = a2/(c-b), b= (a2/(c-b)-(C-b))/2, C = (a2/(c-b) +c–b)/2 Sample code:

#include <stdio.h> #include <stdlib.h> #include <math.h> int fun1 (int n, int *a);//When the hypotenuse is an int fun2 (int
	n, int *a);//is a right-angled edge when int main (void) {int n;
	int *b;
	scanf ("%d", &n);
	b = (int *) malloc (sizeof (int) * n);
	for (int i = 0; i < n; i++) {scanf ("%d", B + i);  for (int i = 0; I < n;
		i++) {fun2 (i, b);
		Fun1 (i, b);
	printf ("\ n");
	} int fun1 (int n, int *a) {double x = 0;
	int temp;
	Double foo = 1e-10;
		When the bevel is beveled, the minimum side for (Double i = 1.0 i < sqrt (double) (a[n] * a[n])/2.0), i++) {x = a[n] * A[n]-I * i;
		temp = (int) sqrt (x);
		if (sqrt (x)-temp < foo) {printf ("%.0f,%.0f\n", sqrt (x), i);
	}//printf ("Fun1 down.\n");
return 0;
	int fun2 (int n, int *a) {//Poor lift c-b double Temp1,temp2,temp3;
	Double foo = 1e-10;
		for (Double i = 1.0 i < a[n]; i++) {Temp3 = a[n] * A[n]/I;
		Temp1 = (temp3-i)/2.0;		
		Temp2 = (Temp3 + i)/2.0; if (Temp3-(int) Temp3 < foo && temp1-(int) Temp1 < foo && teMP2-(int) Temp2 < foo) {printf ("%.0f,%.0f\n", TEMP2,TEMP1);	
}//printf ("Fun2 down.\n");
 }

Copyright Notice: Original works, allow reprint, reprint, please be sure to hyperlink form to indicate the original publication of the article, the author's information and this statement. Otherwise, legal liability will be held. Article Address http://blog.csdn.net/itkaikai/article/details/22733387, blog address: Http://blog.csdn.net/itkaikai.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.