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;
    				}
    			}
    		}
    	}
    }
    
    
  • 相关阅读:
    ubuntu 16.0.5 修改网卡为固定IP
    Ubuntu PostgreSQL安装和配置
    NPOI 1.2.1版本替换为2.4.0版本实体类变更
    C# 之 Math取整
    解决github 下载过慢的问题
    优伦自动语言话务员设置
    python3学习笔记 列表
    【postgresql】role "root" does not exist 解决办法
    Eclipse使用的小技巧
    Servlet
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852186.html
Copyright © 2011-2022 走看看