fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. struct candidates {
  8. string name;
  9. double numb;
  10. };
  11.  
  12. bool c(candidates x, candidates y)
  13. {
  14. return x.numb > y.numb;
  15. }
  16.  
  17. int main() {
  18. int candidateNum, ballotNum;
  19. cin >> candidateNum >> ballotNum;
  20. candidates* candidate = new candidates[candidateNum+1];
  21. string s = "";
  22. for (int i = 0; i < candidateNum; i++)
  23. {
  24. cin >> s;
  25. candidate[i].name = s;
  26. candidate[i].numb = 0;
  27. }
  28. candidate[candidateNum].name = "Invalid";
  29. candidate[candidateNum].numb = 0;
  30. int count = 0, num;
  31. for (int i = 0; i < ballotNum; i++)
  32. {
  33. cin >> s;
  34. count = 0;
  35. for (int j = 0; j < candidateNum; j++)
  36. {
  37. if (s[j] == 'X')
  38. {
  39. count++;
  40. num = j;
  41. }
  42. if (isalpha(s[j]) && s[j] != 'X')
  43. count=2;
  44. }
  45. if (count == 1)
  46. {
  47. candidate[num].numb++;
  48. }
  49. else
  50. candidate[candidateNum].numb++;
  51.  
  52. }
  53. sort(candidate, candidate + candidateNum,c);
  54. for (int i = 0; i <= candidateNum; i++)
  55. {
  56. cout << fixed << setprecision(2) << candidate[i].name << " " << (candidate[i].numb*100)/ballotNum << "%\n";
  57. }
  58. return 0;
  59. }
Success #stdin #stdout 0s 4416KB
stdin
4 7
Loudy
Apples
Dogman
Miller
.X..
X...
....
..X.
..XX
..X.
..X.
stdout
Dogman 42.86%
Loudy 14.29%
Apples 14.29%
Miller 0.00%
Invalid 28.57%