fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAX_BANKNOTES = 8;
  5. const string VALID_BANKNOTES[] = {"1", "5", "10", "20", "50", "100", "200", "500"};
  6. int frqBankNotes[MAX_BANKNOTES] = {0};
  7.  
  8. int isValidBanknote(const string currNum) {
  9. for (int i = 0; i < MAX_BANKNOTES; ++i) {
  10. if (currNum == VALID_BANKNOTES[i]) {
  11. return i;
  12. }
  13. }
  14. return -1;
  15. }
  16.  
  17. string getMostFrqBankNote(const int &noValidBanknotes) {
  18. if (noValidBanknotes == 0) {
  19. return "NU EXISTA";
  20. }
  21. int mostFrqIndex = 0;
  22. for (int i = 0; i < MAX_BANKNOTES; ++i) {
  23. if (frqBankNotes[i] >= frqBankNotes[mostFrqIndex]) {
  24. mostFrqIndex = i;
  25. }
  26. }
  27. return VALID_BANKNOTES[mostFrqIndex];
  28. }
  29.  
  30. void splitNumbers(const string &text, int &noValidBanknotes) {
  31. const int len = (int)text.size();
  32. string currNum;
  33. for (int i = 0; i <= len; ++i) {
  34. if (isdigit(text[i]) && (text[i] != '0' || !currNum.empty())) {
  35. currNum += text[i];
  36. } else if (!currNum.empty()) {
  37. const int indexBankNote = isValidBanknote(currNum);
  38. if (indexBankNote >= 0) {
  39. ++frqBankNotes[indexBankNote];
  40. ++noValidBanknotes;
  41. }
  42. currNum.clear();
  43. }
  44. }
  45. }
  46.  
  47. string formatResult(const int &noValidBanknotes) {
  48. return to_string(noValidBanknotes) + '\n' + getMostFrqBankNote(noValidBanknotes);
  49. }
  50.  
  51. int main() {
  52. string text;
  53. int noValidBanknotes = 0;
  54. while (getline(cin, text)) {
  55. splitNumbers(text, noValidBanknotes);
  56. }
  57. cout << formatResult(noValidBanknotes);
  58. return 0;
  59. }
  60.  
  61.  
Success #stdin #stdout 0s 5316KB
stdin
2
20,.&3,.300.100
Abc100 21
stdout
3
100