e-olymp Problem11 Big accuracy

传送门:点我

Big accuracy

The rational fraction m/n is given. Write it in the decimal notation with k digits after the decimal point.

Input

Three numbers mnk are written in one line. 0 < mn ≤ 1000 ≤ k ≤ 1000.

Output

Print k digits after the decimal point of the given number.

Time limit 1 second
Memory limit 64 MiB
Input example #1
1 2 3
Output example #1
0.500

题意:给你N,M,K,求N/M ,保留K位。
本题有点小坑的地方是,K等于0的时候是取整数部分,保留K位是直接扔掉后面的,不需要四舍五入。
JAVA代码:
import java.text.DecimalFormat;
import java.util.*;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main 
{    
  public static void main (String[] argv) 
  {
      new Main();
  }
  boolean test = false;
  public Main(){
      FastReader in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
      //FastReader in = new FastReader(new BufferedReader(new FileReader("Main.in")));
      Scanner input = new Scanner(System.in);
      BigDecimal x = input.nextBigDecimal();
      BigDecimal y = input.nextBigDecimal();
      int k = input.nextInt();
      if(k == 0){
          BigDecimal ans = x.divide(y,1,BigDecimal.ROUND_DOWN);
          int answer = ans.intValue();
          System.out.println(answer);
      }//如果只需要整数部分,就直接取int
      else{
          BigDecimal ans = x.divide(y,k,BigDecimal.ROUND_DOWN);
          System.out.println(ans);
      }//保留K位,并且不需要四舍五入
      input.close();
  }
  static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;
        public FastReader(BufferedReader in)
        {            
            br = in;
        }
 
        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    String line = br.readLine();
                    if (line == null || line.length() == 0) return "";
                    st = new StringTokenizer(line);
                }
                catch (IOException  e)
                {
                    return "";
                    //e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt()
        {
            return Integer.parseInt(next());
        }
 
        long nextLong()
        {
            return Long.parseLong(next());
        }
 
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
 
        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                return "";
                //e.printStackTrace();
            }
            return str;
        }
    }
}
 
原文地址:https://www.cnblogs.com/Esquecer/p/9052281.html