HDU 1178 Heritage from father 数学公式 JAVA
来源:互联网 发布:国际数据公司 收购 编辑:程序博客网 时间:2024/05/18 02:32
题意:求∑ni=1i∗(i+1)2 ,并用科学计数法保留3位小数表示出来。
思路:写出公式即可:∑ni=1i(i+1)2=12∑ni=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
- HDU 1178 Heritage from father 数学公式 JAVA
- 1178 hdu Heritage from father
- hdu 1178 Heritage from father
- HDU 1178 Heritage from father
- HDU 1178 Heritage from father
- HDU 1178Heritage from father
- HDU 1178 Heritage from father
- hdu---1178Heritage from father
- HDU Heritage from father
- hdu(1178)Heritage from father
- HDU--1178--Heritage from father(求和公式)
- hdoj 1178 Heritage from father (数学题)
- Heritage from father
- Heritage from father
- Heritage from father
- Heritage from father
- HDU-Heritage from father-数列&&科学记数法
- hdu1178数学题 Heritage from father
- UVA11624 Fire BFS
- JQ的学习
- [Servlet&JSP] 过滤器的使用
- 【我的JS第三本】JavaScript_DOM编程艺术第二版读书笔记
- 清空mysql 查询缓存
- HDU 1178 Heritage from father 数学公式 JAVA
- C++设计模式新解四 观察者模式+事件订阅
- 以太网帧格式
- android-文字的处理(最后一条 不用setCompoundDrawables 在text上加图片)
- C++中类成员函数作为回调函数
- 三个容易混淆的jquery选择器
- 网格的学习—2
- Android内存泄露总结(一)
- linux 进程管理