HDU 1178 Heritage from father 数学公式 JAVA

来源:互联网 发布:国际数据公司 收购 编辑:程序博客网 时间:2024/05/18 02:32

题意:求ni=1i(i+1)2,并用科学计数法保留3位小数表示出来。

思路:写出公式即可:ni=1i(i+1)2=12ni=1(i2+i)=12[n(2n+1)(n+1)6+i(i+1)2]

我菜成狗的java,吐槽一下,java的BigInteger写起来好爽呀,都是泪。

http://acm.hdu.edu.cn/showproblem.php?pid=1178

/*********************************************    Problem : HDU 1178    Author  : NMfloat    InkTime (c) NM . All Rights Reserved .********************************************/import java.io.*;import java.math.*;import java.util.*;public class Main {    public static void main(String argv[]) {        Scanner cin = new Scanner(System.in);        BigInteger two = BigInteger.valueOf(2);        BigInteger six = BigInteger.valueOf(6);        BigInteger n , ans;        while(cin.hasNext()) {            n = cin.nextBigInteger();            if(n.compareTo(BigInteger.ZERO)==0) break;            ans = n.multiply(n.multiply(two).add(BigInteger.ONE)).multiply(n.add(BigInteger.ONE)).divide(six);            ans = ans.add(n.multiply(n.add(BigInteger.ONE)).divide(two)).divide(two);            String a = ans.toString();            char A[] = a.toCharArray();            int lena = a.length();            if(lena == 1) {                System.out.printf("%s.00E%d",a,lena-1);            }            else if(lena == 2) {                String b = a.substring(0,1);                String c = a.substring(1,2);                System.out.printf("%s.%s0E%d",b,c,lena-1);            }            else if(lena == 3) {                System.out.printf("%c.%c%cE%d",A[0],A[1],A[2],lena-1);            }            else {                if(A[3] >= '5') A[2] ++ ;                if(A[2] > '9') { A[1] ++ ; A[2] = '0';}                if(A[1] > '9') { A[0] ++ ; A[1] = '0';}                if(A[0] > '9') {                    lena ++;                    A[0] = '1'; A[1] = '0'; A[2] = '0';                }                System.out.printf("%c.%c%cE%d",A[0],A[1],A[2],lena-1);            }            System.out.println("");        }    }}
0 0
原创粉丝点击