//寻找某两个数相除,其结果 离黄金分割点 0.618最近
//分母和分子不能同时为偶数
//分母和分子 取值范围在[1-20]
public class GoldenSectionRatio
{
public static void main(String[] args)
{
double breakPoint = 0.618 ;
int fenZhi = 1;
int fenMu = 1;
double value = 10;
double miniDiff = 100;
double diff = 0;
for (int i =1;i<=20 ;i++ )
{
for (int j=1;j <=20 ;j++ )
{
if (i % 2 == 0&&j %2 ==0)
{
continue;
}
value = (double)i/j;
diff = value - breakPoint;
diff = diff > 0 ? diff : 0-diff;
if (miniDiff > diff)
{
miniDiff = diff;
fenZhi = i;
fenMu = j;
}
}
}
System.out.println("分子为"+fenZhi+"分母为"+fenMu+",值为"+fenZhi+"/"+fenMu+"="+((double)fenZhi/fenMu));
}
}
黄金分割点(java)
最新推荐文章于 2022-04-09 17:48:35 发布