HDU 1178 Heritage from father 数学公式 JAVA

题意:求 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("");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值