zoukankan      html  css  js  c++  java
  • Heron and His Triangle HDU

    A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integers t−1, t, t+ 1 and thatits area is an integer. Now, for given n you need to find a Heron’s triangle associated with the smallest t bigger
    than or equal to n.

    Input

    The input contains multiple test cases. The first line of a multiple input is an integer T (1 ≤ T ≤ 30000) followedby T lines. Each line contains an integer N (1 ≤ N ≤ 10^30).

    Output

    For each test case, output the smallest t in a line. If the Heron’s triangle required does not exist, output -1.

    Sample Input

    4
    1
    2
    3
    4

    Sample Output

    4
    4
    4
    4

    打表发现数列规律 f[n]=4*f[n-1]-f[n-2]

    大数写JAVA!

    package a;
    import java.util.Scanner;
    import java.math.BigInteger;
    public class A {
    	public static void main(String[] args)
    	{
    		BigInteger[] num=new BigInteger[105];
    		num[0]=BigInteger.valueOf(4);
    		num[1]=BigInteger.valueOf(14);
    		for(int i=2;i<100;i++)
    		{
    			num[i]=num[i-1].multiply(num[0]);
    			num[i]=num[i].subtract(num[i-2]);
    		}
    		Scanner cin=new Scanner(System.in);
    		int t=cin.nextInt();
    		while(t--!=0)
    		{
    			BigInteger n=cin.nextBigInteger();
    			for(int i=0;i<99;i++)
    			{
    				if(num[i].compareTo(n)!=-1)
    				{
    					System.out.println(num[i]);
    					break;
    				}
    			}
    		}
    	}
    }
    
    
  • 相关阅读:
    Java 反射
    类中静态/普通/构造初始化顺序
    计算机世界中的0和1
    Java并发练习
    HashMap底层
    HashMap 与 Hashtable 的区别
    为什么重写了equals() 就要重写hashcode()
    干货型up主
    JSP页面元素
    重定向与请求转发的区别
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852186.html
Copyright © 2011-2022 走看看