[ZCMU OJ]5198: Mona Lisa‘s smile(思路记录)

5198: Mona Lisa's smile

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 101  Solved: 30
[Submit][Status][Web Board]

Description

When Leonardo da Vinci's famous painting "Mona Lisa's smile" was born, Leonardo da Vinci was ready to hang it on the wall. First, take a closer look. In order to hang the picture, you need to nail a nail in the wall. After the nail is nailed, the connecting line between the nail and the four corners of the wall divides the wall into several disjoint areas with an area greater than zero. Leonardo da Vinci is not only a master of art, but also a mathematical genius. He wanted to know: which of these disjoint areas has the smallest area? Can you help him figure it out? Suppose the wall is a square with side length A, and the distance between the nail and the upper and left margins of the wall are B and C respectively.

Input

The input has multiple cases (no more than 100) of test data.

Each test case occupies one line, which are three integers A(1 ≤ A ≤ 100)、B(0 ≤ B ≤ A)and C(0 ≤ C ≤ A), representing the side length of the square wall and the distance between the nail and the upper and left margins of the wall.

The end of input will be represented by a line of test case where A、B and C are equal to 0. This test case should not be processed.

Output

Each test case outputs a line, which is a real number, that is, the area of the smallest area, which is rounded to one decimal place.

Sample Input

4 1 1

6 2 1

0 0 0

Sample Output

2.0

3.0

_____________________________________________________________________________

这道题的意思是:一个正方形内任意找一个点,连线到四个顶点;连线之后正方形被分成四个区域,求这四个区域中最小的那一块区域。

这四块区域无非都是三角形,所以我的思路(有漏洞的)一开始是:找到四个三角形的高,再找到最小的那个高,就可以求到最小的那块面积。但是这题得考虑到一种边界情况,例如:“4 0 0”。这个时候输出应该是8.0(4*4/2)。但是很不幸的是wa了。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	double bian,shang,zuo;
	while(cin>>bian>>shang>>zuo)
	{
		double heng,shu,min_one;
		heng=min(shang,bian-shang);
		shu=min(zuo,bian-zuo);
		
		if(heng==0||shu==0)
		{
			heng=bian;
			shu=bian;
		}
		
		min_one=min(heng,shu);
		
		double min_area=bian*min_one/2;
		
		if(min_area)
		printf("%.1lf\n",min_area);
	}
}

后来换了一种思路:既然找最小的面积,那就直接把四块面积算出来找到最小的面积就好了,(最小的面积也得大于0)这样也避免了上一种思路得逻辑漏洞。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	double bian,zuo,shang;
	while(cin>>bian>>zuo>>shang)
	{
		double s[4];
		s[0]=bian*zuo/2;
		s[1]=bian*shang/2;
		s[2]=bian*(bian-zuo)/2;
		s[3]=bian*(bian-shang)/2;
		
		sort(s,s+4);
		for(int i=0;i<4;i++)
		{
			if(s[i])
			{
				printf("%.1lf\n",s[i]);
				break;
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/Solar_Zheng0817/article/details/124504193