zoukankan      html  css  js  c++  java
  • Heron and His Triangle 2017 沈阳区域赛

    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.

    InputThe 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). 
    OutputFor 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

    题目是求大于等于的最小t使t,t-1,t+1构成的三角形的面积是一个整数
    然后就是打表找规律。。
    做题的时候一直在想用推出来的公式打表,结果最好看题解竟然是一个大数找规律。唉。
    因为是大数所以用java做的。
    import java.math.*;  
    import java.util.*;  
    import java.io.*;  
      
    public class Main  
    {     
        public static void main(String[] args)  
        {  
            Scanner cin=new Scanner(new BufferedInputStream(System.in));  
            BigInteger res[] = new BigInteger[100];  
            res[0] = BigInteger.valueOf(4L);  
            res[1] = BigInteger.valueOf(14L);  
            for (int i = 2;i < 100;i++) {  
                res[i] = res[i-1].multiply(new BigInteger("4")).subtract(res[i-2]);  
            }  
            while (cin.hasNext()) {  
                int t = cin.nextInt();  
                for (int ca = 1;ca <= t;ca++) {  
                    BigInteger n = cin.nextBigInteger();  
                    int i = 0;  
                    for (i = 0;i < 100;i++) {  
                        if (n.compareTo(res[i]) != 1) break;  
                    }  
                    System.out.println(res[i]);  
                }  
            }  
            cin.close();  
        }  
    }  
    彼时当年少,莫负好时光。
  • 相关阅读:
    【华为云技术分享】云小课 | 磁盘容量不够用?小课教你来扩容!
    什么时候用用value,什么时候用innerHTML?
    零碎知识点总结
    hnctf安恒--蜘蛛侠呀
    sqli-labs Less-11 and Less-12
    sql注入--基于报错的注入
    ‘百度杯’十月场web ---login
    “百度杯”CTF比赛 九月场---123
    百度杯 ctf 九月场---Text
    【迎圣诞,拿大奖】+流量分析+Writeup分享
  • 原文地址:https://www.cnblogs.com/l609929321/p/7834825.html
Copyright © 2011-2022 走看看