您经营着一个政治网站,并且确定与以百分比(“ 71%” )表示的选举时相比,赢得选举或失败选举的机会与比例(“ 7 分之 5”)对人们有更好的直觉理解。)。
但是,您也不想显示诸如“ 82中的58”之类的令人迷惑的比率,即使它们不太精确,也希望它们更容易理解。
因此,给定一个介于0.1%和99.9%之间的百分比,请使用以下规则返回最接近的“易于理解”比率“ x in y ”:
- 大多数值(请参见下面的异常)应返回最接近的比率,即10或更低。55%的用户应返回“ 5 in 9”,而不是“ 11 in 20”。
- 比率应降至最低水平。65%的用户应返回“ 2 in 3”,而不是“ 4 in 6”。
- 小于10%的值应返回“ n中的1 ”的最接近比率,其中n是(10,12,15,20,30,40,50,60,70,80,90,100)中的一个。例如,6%应返回“ 15分之一”。
- 超过90%的值应返回最接近的比率,形式为“ n-1 in n ”,其中n是(10,12,15,20,30,40,50,60,70,80,90,100)中的一个。例如,98.7%应该返回“ 79 in 80”。
- 小于1%的值应返回“ <100分之一 ”
- 超过99%的值应返回“ > 99 in 100 ”
或者,换一种方式考虑,您的程序应该从以下可能的输出中返回最接近的比率(为方便起见,我将它们的近似值包括在内):
<1 in 100
1 in 100 = 1.00%
1 in 90 = 1.11%
1 in 80 = 1.25%
1 in 70 = 1.43%
1 in 60 = 1.67%
1 in 50 = 2.00%
1 in 40 = 2.50%
1 in 30 = 3.33%
1 in 20 = 5.00%
1 in 15 = 6.67%
1 in 12 = 8.33%
1 in 10 = 10.00%
1 in 9 = 11.11%
1 in 8 = 12.50%
1 in 7 = 14.29%
1 in 6 = 16.67%
1 in 5 = 20.00%
2 in 9 = 22.22%
1 in 4 = 25.00%
2 in 7 = 28.57%
3 in 10 = 30.00%
1 in 3 = 33.33%
3 in 8 = 37.50%
2 in 5 = 40.00%
3 in 7 = 42.86%
4 in 9 = 44.44%
1 in 2 = 50.00%
5 in 9 = 55.56%
4 in 7 = 57.14%
3 in 5 = 60.00%
5 in 8 = 62.50%
2 in 3 = 66.67%
7 in 10 = 70.00%
5 in 7 = 71.43%
3 in 4 = 75.00%
7 in 9 = 77.78%
4 in 5 = 80.00%
5 in 6 = 83.33%
6 in 7 = 85.71%
7 in 8 = 87.50%
8 in 9 = 88.89%
9 in 10 = 90.00%
11 in 12 = 91.67%
14 in 15 = 93.33%
19 in 20 = 95.00%
29 in 30 = 96.67%
39 in 40 = 97.50%
49 in 50 = 98.00%
59 in 60 = 98.33%
69 in 70 = 98.57%
79 in 80 = 98.75%
89 in 90 = 98.89%
99 in 100 = 99.00%
>99 in 100
其他规定:
- 数值输入可以在0.1到99.9的范围内或在0.001到0.999的范围内,以较方便的为准。您必须处理至少3个有效数字。
- 您必须输出比率(“ 3 in 4”),而不是等效分数(“ 3/4”)。
- 如果有两个比率接近输入,则您的程序可以返回一个比率。7.5%可能会返回“ 12分之一”或“ 15分之一”。
- 前导/尾随空白和/或换行都可以
例子:
Input : Output
0.5 : <1 in 100
1.0 : 1 in 100
1.5 : 1 in 70
7.5 : 1 in 15 or 1 in 12 (either is acceptable)
9.2 : 1 in 10
13.1 : 1 in 8
29.2 : 2 in 7
29.3 : 3 in 10
52.7 : 1 in 2
52.8 : 5 in 9
72.0 : 5 in 7
73.9 : 3 in 4
88.8 : 8 in 9
90.8 : 9 in 10
94.2 : 19 in 20
98.7 : 79 in 80
98.9 : 89 in 90
99.0 : 99 in 100
99.1 : >99 in 100
这是一个代码挑战,每种语言中最短的代码将获胜。
(类似于但不重复:将小数转换为小数,最近的小数,n位精度的近似浮点数)
If there are two ratios equally close to the input, your program can return either one. 7.5% could return "1 in 12" or "1 in 15"
这是否意味着我们也可以返回7 in 100
?Btw,1 in 14
在这种情况下更接近输入。