如何找到一个矩形中含有两个圆的空白空间?

huangapple go评论137阅读模式
英文:

How to find empty space in a rectangle which has 2 circles in it?

问题

我在 Toph 上解决了一个问题。在这个问题中,我需要找出一个矩形中两个相等圆的空白空间。

这里是问题链接

#include <stdio.h>
float pi=3.1416;
int main()
{
    int i,t;
    float r,rest;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%f",&r);
        rest=(4*r*2*r)-(2*pi*r*r);
        printf("Case %d: %.2f\n",i,rest);
    }
    return 0;
}

这是我的解决方案。它对第一个测试案例返回了正确的值,但未能解决第二个问题。
问题是什么?

英文:

I'm solving a problem in Toph . In this problem I've to find out the empty space of a rectangle which has 2 equal circles in it.

here is the problem

#include &lt;stdio.h&gt;
float pi=3.1416;
int main()
{
	int i,t;
	float r,rest;
	scanf(&quot;%d&quot;,&amp;t);
	for(i=1;i&lt;=t;i++)
	{
		scanf(&quot;%f&quot;,&amp;r);
		rest=(4*r*2*r)-(2*pi*r*r);
		printf(&quot;Case %d: %.2f\n&quot;,i,rest);
	}
	return 0;

Here is my solve. It returns a correct value for first test case but it fails to solve the second one.
What's the problem???

答案1

得分: 1

&lt;strike&gt;```float pi=3.1416;``` 是问题的原因。在数学头文件 (```#include &lt;math.h&gt;```) 下有一个常数 ```M_PI```,请使用它代替。&lt;/strike&gt;

Edit:
抱歉,没有仔细阅读,显然问题出在浮点精度上。如果你将所有的 float 值改成 double,应该就可以工作。
```c
#include &lt;stdio.h&gt;
double pi=3.1416;
int main()
{
    int i,t;
    double r,rest;
    scanf(&quot;%d&quot;,&amp;t);
    for(i=1;i&lt;=t;i++)
    {
        scanf(&quot;%lf&quot;,&amp;r);
        rest=(4*r*2*r)-(2*pi*r*r);
        printf(&quot;Case %d: %.2lf\n&quot;,i,rest);
    }
    return 0;
}

<details>
<summary>英文:</summary>

&lt;strike&gt;```float pi=3.1416;``` is the cause of the problem. Under the math header file (```#include &lt;math.h&gt;```) there is a constant ```M_PI``` use it instead.&lt;/strike&gt;

Edit:
Sorry, didn&#39;t read thoroughly, apparently the problem is in the floating point precision. If you change all float values into double it should work.
```c
#include &lt;stdio.h&gt;
double pi=3.1416;
int main()
{
    int i,t;
    double r,rest;
    scanf(&quot;%d&quot;,&amp;t);
    for(i=1;i&lt;=t;i++)
    {
        scanf(&quot;%lf&quot;,&amp;r);
        rest=(4*r*2*r)-(2*pi*r*r);
        printf(&quot;Case %d: %.2lf\n&quot;,i,rest);
    }
    return 0;
}

答案2

得分: 0

不同于2和8,double更准确的原因是因为float无法像输入值一样表示3.1416

3.1416 -> 3.1415998935699462890625
40.082 -> 40.082000732421875
85.8   -> 85.8000030517578125

这里简单来说,精度不够(请注意,IEEE-754 float,这是绝大多数情况下用于float的标准,它以2为底存储)。很可能,后面的数字可能是有意生成的,以便无法通过测试用例。如果想了解更多,请参考不要将其存储在浮点数中每位计算机科学家都应该了解的浮点数算术知识

数值常数是1.7168,假设是他们版本的圆周率乘以r*r的结果是准确的。而使用单精度精度的最佳结果是1.7167999744415283203125,误差为2.55584716796875E-8。使用double的结果是1.71680000000000010373923942097,误差为1.0373923942097E-16,再加上输入的值。

英文:

Unlike 2 and 8, the reason double is more accurate is because float cannot represent 3.1416 as well as the input values:

3.1416 -&gt; 3.1415998935699462890625
40.082 -&gt; 40.082000732421875
85.8   -&gt; 85.8000030517578125

There's simply not enough precision, (note that IEEE-754 float, which is overwhelmingly used for float, stores it in base-2.) Most probably, the later numbers were probably specifically generated in order to fail the test cases. If one wants to know more, Don’t Store That in a Float, and What Every Computer Scientist Should Know About Floating-Point Arithmetic.

The numerical constant is 1.7168, which is exact assuming their version of pi, (times r*r.) The best one could with single-point precision is 1.7167999744415283203125, which is off by 2.55584716796875E-8. With a double, it's 1.71680000000000010373923942097, off by 1.0373923942097E-16, plus the values input.

huangapple
  • 本文由 发表于 2020年1月6日 20:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定