Java large number practice third bomb

Source: Internet
Author: User

POJ 1001 Topics Link http://poj.org/problem?id=1001

Calculate the N-order of floating-point numbers, requiring no scientific notation, no trailing and beginning zeros.

Code

Import Java.util.*;import java.math.*;p ublic class main{public static void Main (string[] args) {Scanner in=new Scanner ( system.in); while (In.hasnext ()) {BigDecimal a;int b;a=in.nextbigdecimal (); B=in.nextint (); BigDecimal C=a.pow (b); String Ans=c.striptrailingzeros (). toplainstring ();//Remove trailing zeros, convert to non-scientific notation string if (Ans.charat (0) = = ' 0 ') {//If starting with 0 ans= Ans.substring (1);//Returns the String}system.out.println (ans) starting with position 1;}}

POJ 1131 Topics Link: http://poj.org/problem?id=1131

Octal floating-point numbers converted to decimal

0.75 = 7/8+5/(8*8) =0.953125

Code

Import Java.util.*;import java.math.*;p ublic class main{public static void Main (string[] args) {Scanner in=new Scanner ( system.in); while (In.hasnext ()) {BigDecimal a;a=in.nextbigdecimal (); String ans=a.toplainstring (); BigDecimal fin=bigdecimal.zero;for (int i=2;i<ans.length (); i++) {Fin=fin.add (bigdecimal.valueof (Ans.charat (i)- Divide (bigdecimal.valueof (8). POW (i-1)));} String Fans=fin.striptrailingzeros (). toplainstring (); System.out.println (A + "[8] =" +fans+ "[10]");}}}

POJ 1220

Conversion of arbitrary binary numbers (2-62) to each other

The method is converted into 10 and then converted into the desired system by taking the remainder method.

Code

Import Java.util.*;import java.math.*;p ublic class main{public static void Main (string[] args) {Scanner in=new Scanner ( system.in); int n=in.nextint (); while (n-->0) {int a,b;a=in.nextint (); B=in.nextint (); String Str=in.next (); BigInteger big=biginteger.zero;int k=0;for (int i=str.length () -1;i>=0;i--) {if (Str.charat (i) >= ' A ' && Str.charat (i) <= ' Z ') {//system.out.print (Str.charat (i)-' A ' +10); Big=big.add (biginteger.valueof (Str.charat (i)-' A ' +10). Multiply (Biginteger.valueof (a). Pow (k++));} else if (Str.charat (i) >= ' a ' &&str.charat (i) <= ' z ') {big=big.add (biginteger.valueof (Str.charat (i)-' a ' + Multiply (biginteger.valueof (a). Pow (k++))); else if (Str.charat (i) >= ' 0 ' &&str.charat (i) <= ' 9 ') {//system.out.print (Str.charat (i)-' 0 '); big=big.add (Biginteger.valueof (Str.charat (i)-' 0 '). Multiply (Biginteger.valueof (a). Pow (k++)));}} System.out.println ("Ten:" +big); K=0;int num[]=new int[100000];int sign=0;if (big.equals (Biginteger.zero)) {sign=1;} Residual method while (!big.equals (Biginteger.zero){Num[k++]=big.mod (biginteger.valueof (b)). Intvalue ();//Convert to Intbig=big.divide (biginteger.valueof (b));} System.out.println (A + "" +str); System.out.print (b + "), if (sign==1)//special, if input 0, the above method is not processed System.out.print (" 0 "); for (int i=k-1;i>=0;i--) {if (num [i]>=0&&num[i]<=9) System.out.print (Num[i]); else if (num[i]>=10&&num[i]<=35) {char c= ( char) (num[i]+ ' A '-10); System.out.print (c);} else if (num[i]>=36&&num[i]<=62) {char c= (char) (num[i]+ ' a '-36); System.out.print (c);}} System.out.println (); System.out.println ();}}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java large number practice third bomb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.