Advertisement
Spocoman

03. Histogram

Dec 24th, 2021 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Histogram(input) {
  2.     let count = Number(input[0]);
  3.     let p1 = 0;
  4.     let p2 = 0;
  5.     let p3 = 0;
  6.     let p4 = 0;
  7.     let p5 = 0;
  8.  
  9.     for (let i = 1; i <= count; i++) {
  10.         let number = Number(input[i]);
  11.         if (number < 200) {
  12.             p1 += 1;
  13.         } else if (number < 400) {
  14.             p2 += 1;
  15.         } else if (number < 600) {
  16.             p3 += 1;
  17.         } else if (number < 800) {
  18.             p4 += 1;
  19.         } else {
  20.             p5 += 1;
  21.         }
  22.     }
  23.     console.log(`${(p1 / count * 100).toFixed(2)}%`);
  24.     console.log(`${(p2 / count * 100).toFixed(2)}%`);
  25.     console.log(`${(p3 / count * 100).toFixed(2)}%`);
  26.     console.log(`${(p4 / count * 100).toFixed(2)}%`);
  27.     console.log(`${(p5 / count * 100).toFixed(2)}%`);
  28. }
  29.  
  30.  
  31. Тарикатско решение:)
  32.  
  33. function Histogram(input) {
  34.     let num = Number(input.shift());
  35.     let histogram = [0, 0, 0, 0, 0];
  36.  
  37.     for (let i = 0; i < num; i++) {
  38.         let n = Number(input.shift());
  39.         histogram[(n < 200 ? 0 : n < 400 ? 1 : n < 600 ? 2 : n < 800 ? 3 : 4)]++;
  40.     }
  41.  
  42.     for (let h of histogram) {
  43.         console.log(`${(h / num * 100).toFixed(2)}%`);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement