Poj1001 power java usage

Source: Internet
Author: User

BigDecimal in java
Java. math. BigDecimal
The data range that other common types cannot represent, such as double, cannot exceed 16 bits, but this class can. Today, I wrote the 1001 question of ACM at Peking University. I wrote it in C ++ and then in java. I think java is simple, but I am not familiar with java. Learn java from now on.
This question uses BigDecimal in java.
You cannot perform operations on the objects of common ++. Instead, use the method in its class.
BigDecimal add (BigDecimal augend): Addition
BigDecimal subtract (BigDecimal subtrahend): subtraction
BigDecimal divide (BigDecimal divisor): Division
BigDecimal pow (int n): Power
BigDecimal multiply (BigDecimal multiplicand): Multiplication
The resulting decimal number must be zero after the decimal point. BigDecimal stripTrailingZeros () is used to return the BigDecimal number, but all BigDecimal values with Zero tail are removed from this representation.
Use the String toPlainString () to return the String representation of the BigDecimal without an exponential field. In general, it is a direct display, which is not represented by scientific notation.
Finally, paste the question code (java) of ACM1001 in the back and remember it later.

import java.io.*; import java.util.*; import java.math.BigDecimal;  public class Main {     public static void main(String args[])throws Exception     {         Scanner cin=new Scanner(System.in);         while(cin.hasNext())         {             BigDecimal r=cin.nextBigDecimal();             int n=cin.nextInt();             r=r.pow(n).stripTrailingZeros();             String m_string=r.toPlainString();             if(m_string.charAt(0)=='0')             m_string=m_string.substring(1);             System.out.println(m_string);         }     } }

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.