Advertisement
MonsterScripter

CodinGame_2023_09_01__20_51_57__count_letters_percent.java

Sep 1st, 2023
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4.  * Objectif
  5.  *
  6.  * Vous devez trouver le pourcentage (arrondi) que les caractères donnés représentent dans une chaîne donnée (sans tenir compte de la casse).
  7.  *
  8.  * Entrée
  9.  * Ligne 1 : Une chaîne s
  10.  * Ligne 2 : Un entier n pour le nombre de caractères donnés
  11.  * n lignes suivantes : Un caractère c
  12.  *
  13.  * Sortie
  14.  * n lignes : Le pourcentage (arrondi) que le caractère donné c représente dans la chaîne s
  15.  *
  16.  * Contraintes
  17.  * n ≥ 1
  18.  * c est un caractère alphabétique en minuscules
  19.  *
  20.  * Exemple
  21.  * Entrée
  22.  * Bonjour
  23.  * 1
  24.  * j
  25.  *
  26.  * Sortie
  27.  * 15%
  28.  */
  29. class Solution {
  30.  
  31.     public static void main(String args[]) {
  32.         Scanner in = new Scanner(System.in);
  33.         String s = in.nextLine();
  34.         int n = in.nextInt();
  35.         for (int i = 0; i < n; i++) {
  36.             String c = in.next();
  37.             int x = 0;
  38.             for (int j=0; j < s.length(); j++) {
  39.                 if (Character.toLowerCase(s.charAt(j)) == c.charAt(0)) {
  40.                     x++;
  41.                 }
  42.             }
  43.             System.out.println((int) Math.ceil((x / ((double) s.length())) * 100) + "%");
  44.         }
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement