题目大意:给一个正方形,然后取各边的中点,连接成一个新的正方形,每次在新的正方形中重复此过程,共1000次。最后给出一条平行与y轴的线,问经过多少条边。
分析:规律蛮好找的,思路蛮好想的。然而,精度的问题,还是用Java好了。这是第一次用Java写,所以,参考了下别人的代码,传送门:http://www.cnblogs.com/qscqesze/p/4824212.html
这个是UESTC卿神的博客。Orz...
代码:
import java.math.BigDecimal;
import java.util.Scanner;
public class Main
{
static BigDecimal a[] = new BigDecimal[1005];
public static void main(String argv[]) throws Exception
{
Scanner scanner = new Scanner(System.in);
BigDecimal x = BigDecimal.valueOf(0.5);
BigDecimal k = BigDecimal.valueOf(0.5);
BigDecimal y;
a[0] = BigDecimal.valueOf(0);
for(int i = 1; i <= 500; i++)
{
x = x.multiply(k);
a[i] = a[i-1].add(x);
}
int T = scanner.nextInt();
while(T != 0)
{
int L = 0, R = 500;
y = scanner.nextBigDecimal();
while(L <= R)
{
int mid = (L+R)/2;
int result = a[mid].compareTo(y);
if(result == -1) L = mid+1;
else if(result == 1) R = mid-1;
else
{
L = mid;
break;
}
}
if(a[L].compareTo(y) == 0) System.out.println(-1);
else System.out.println(L*4);
T--;
}
}
}