Cod sursa(job #3278708)

Utilizator SpiderManSimoiu Robert SpiderMan Data 20 februarie 2025 16:25:39
Problema Heapuri cu reuniune Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 4.67 kb
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
    private static final String P1[] = {"zero", "unu", "doi", "trei", "patru", "cinci", "sase", "sapte", "opt",
            "noua", "zece",
            "unsprezece", "doisprezece", "treisprezece", "paisprezece", "cincisprezece", "saisprezece", "saptesprezece",
            "optsprezece", "nouasprezece"};
    private static final String P2[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "zece", "unsprezece",
            "douasprezece", "treisprezece", "paisprezece", "cincisprezece", "saisprezece", "saptesprezece",
            "optsprezece",
            "nouasprezece"};
    private static final String P3[] = {" ", " ", "douazeci", "treizeci", "patruzeci", "cincizeci", "saizeci",
            "saptezeci",
            "optzeci", "nouazeci"};
    private static final String P4[] = {"zero", "unu", "doua", "trei", "patru", "cinci", "sase", "sapte", "opt",
            "noua"};

    public static boolean check(int[] S, int index, int value, boolean equal) {
        return (index < 0 || (equal ? S[index] == value : S[index] > value));
    }

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scan = new Scanner(new FileInputStream("numere9.in"));
        PrintWriter pw = new PrintWriter("numere9.out");

        for (int t = scan.nextInt(); t > 0; --t) {
            String n = scan.next();
            String rez = "";

            int[] S = new int[10];
            for (int j = 0; j < n.length(); ++j) {
                S[j] = n.charAt(j) - '0';
            }

            if (n.length() == 1) rez += P1[S[0]];
            else {
                for (int j = 0; j < n.length(); ++j) {
                    if (n.length() - j == 10) {
                        if (S[j] == 1) rez += "un miliard ";
                        else if (S[j] == 2) rez += "doua miliarde ";
                    } else if (n.length() - j == 9 || n.length() - j == 6 || n.length() - j == 3) {
                        if (S[j] == 1) rez += "o suta ";
                        else if (S[j] > 1) rez += P4[S[j]] + " sute ";
                    } else if (n.length() - j == 8 || n.length() - j == 5 || n.length() - j == 2) {
                        if (S[j] > 1) rez += P3[S[j]] + " ";
                    } else if (n.length() - j == 7) {
                        if (check(S, j - 2, 0, true) && check(S, j - 1, 0, true) && S[j] == 1) rez += "un milion ";
                        else if (check(S, j - 2, 0, true) && check(S, j - 1, 0, true) && S[j] > 1)
                            rez += P4[S[j]] + " milioane ";
                        else if (check(S, j - 1, 1, true))
                            rez += P2[10 + S[j]] + (check(S, j - 2, 0, true) ? " milioane " : " de milioane ");
                        else if (check(S, j - 1, 1, false) && S[j] > 0)
                            rez += "si " + P4[S[j]] + " de milioane ";
                        else if (check(S, j - 1, 1, false)) rez += " de milioane ";
                        else if (check(S, j - 2, 0, false) && S[j] > 0) rez += P4[S[j]] + " de milioane ";
                        else if (check(S, j - 2, 0, false)) rez += " de milioane ";
                    } else if (n.length() - j == 4) {
                        if (check(S, j - 2, 0, true) && check(S, j - 1, 0, true) && S[j] == 1) rez += "o mie ";
                        else if (check(S, j - 2, 0, true) && check(S, j - 1, 0, true) && S[j] > 1)
                            rez += P4[S[j]] + " mii ";
                        else if (check(S, j - 1, 1, true))
                            rez += P2[10 + S[j]] + (check(S, j - 2, 0, true) ? " mii " : " de mii ");
                        else if (check(S, j - 1, 1, false) && S[j] > 0) rez += "si " + P4[S[j]] + " de mii ";
                        else if (check(S, j - 1, 1, false)) rez += "de mii ";
                        else if (check(S, j - 2, 0, false) && S[j] > 0) rez += P4[S[j]] + " de mii ";
                        else if (check(S, j - 2, 0, false)) rez += "de mii ";
                    } else if (n.length() - j == 1) {
                        if (check(S, j - 2, 0, true) && check(S, j - 1, 0, true) && S[j] > 0) rez += P1[S[j]];
                        else if (check(S, j - 1, 1, true)) rez += P1[10 + S[j]];
                        else if (check(S, j - 1, 1, false) && S[j] > 0) rez += "si " + P1[S[j]];
                        else if (check(S, j - 2, 0, false) && S[j] > 0) rez += P1[S[j]];
                    }
                }
            }

            pw.write(rez + "\n");
        }

        pw.close();
        scan.close();
    }
}