ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1263. Elections

WA #1 - Correct output for sample input - Need help with output formating.
Posted by Apurv Nagar 9 Apr 2016 15:48
My code is giving correct output but only till one decimal place when the percentage is in whole number (50.0% instead of 50.00%). I'm using DecimalFormat. Below is my code:-

    import java.util.*;
    import java.lang.*;
    import java.text.DecimalFormat;
    public class Main
    {
        public static void main (String[] args) throws java.lang.Exception
        {
            Scanner sc = new Scanner(System.in);
                int n = sc.nextInt(); //candidates
            int m = sc.nextInt(); //electors
            double div = 0.0; double per = 0.0;
            int t = m;
            int v[] = new int[n];
            while (m != 0)
            {
                int cv = sc.nextInt();
                cv = cv - 1;
                v[cv] = v[cv] + 1;
                m--;
             }
            for (int i = 0; i <=n-1; i++)
            {
                int a = v[i];
                div = t/a;
                per = (1/div * 100.0);
                per = Double.parseDouble(new DecimalFormat("##.##").format(per));
                System.out.println(per + "%");
            }
        }
    }

O/P  (For sample Input)
50.0%
33.33%
16.67%