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

Why RuntimeError on Test 6 Java
Posted by DWinter 3 Apr 2019 16:51
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

public class Task_1263 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        String str = in.nextLine();
        int candidateCount = Integer.valueOf(str.substring(0, 1));
        int votersCount = Integer.valueOf(str.substring(2));
        int[] votersForCandidates = new int[candidateCount];
        double[] votersPercents = new double[candidateCount];
        int voter;
        for (int i = 0; i < votersCount; i++) {
            voter = Integer.valueOf(in.nextLine());
            votersForCandidates[voter - 1]++;
        }
        for (int i = 0; i < candidateCount; i++) {

            votersPercents[i] = new BigDecimal((double) 100 * votersForCandidates[i] / votersCount).setScale(2, RoundingMode.HALF_UP).doubleValue();
        }
        for (int i = 0; i < candidateCount; i++) {
            out.printf("%.2f%%%n", votersPercents[i]);
        }
        out.flush();
    }
}
Re: Why RuntimeError on Test 6 Java
Posted by Lum Gashi 5 Mar 2020 22:31
Because of the Memory limit: which no more than 64 MB.
Re: Why RuntimeError on Test 6 Java
Posted by ToadMonster 6 Mar 2020 12:36
votersPercents array is useless and should be removed.
But the main error is you reading candidateCount in the wrong way. str.substring(0, 1) - how many digits can you read? What max candidateCount is?

You use Scanner class. It has method nextInt() - you can just use it, you don't need to read strings and split them into numbers manually.