Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //You will be given a string array on the console (single line, space-separated). Your task is to make statistics about the elements of //the array. Find out how many times each word occurs in the array. After which, sort the result by the count of occurrences in //descending order and print statistics about every word in the following format:
- //{word} -> {count} times ({percentage:F2}%)
- namespace Array_histogram
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] arr = Console.ReadLine().Split(' ').ToArray();
- List<string> words = new List<string>();
- List<int> occurrences = new List<int>();
- for (int i = 0; i < arr.Length; i++)
- {
- if (!words.Contains(arr[i]))
- {
- words.Add(arr[i]);
- occurrences.Add(1);
- }
- else
- {
- int wordIndex = words.FindIndex(a => a == arr[i]);
- occurrences[wordIndex]++;
- }
- }
- BubbleSort(words, occurrences);
- int sumAllOccurrences = occurrences.Sum();
- double percent = 100.0 / sumAllOccurrences;
- for (int i = 0; i < occurrences.Count; i++)
- {
- Console.WriteLine("{0} -> {1} times ({2:F2}%)", words[i], occurrences[i], (double)occurrences[i] * percent);
- }
- }
- static void BubbleSort( List<string> words, List<int> occur)
- {
- bool swapped;
- do
- {
- swapped = false;
- for (int i = 0; i < occur.Count - 1; i++)
- {
- if (occur[i] < occur[i+1])
- {
- Swap(words, occur, i, i + 1);
- swapped = true;
- }
- }
- } while (swapped);
- }
- static void Swap(List<string> words, List<int> occur, int i1, int i2)
- {
- string tempWords = words[i1];
- int tempOccur = occur[i1];
- words[i1] = words[i2];
- occur[i1] = occur[i2];
- words[i2] = tempWords;
- occur[i2] = tempOccur;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement