fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. const int TEN = 10;
  6. const int MAX_SIZE = 10000;
  7. int workerHours[MAX_SIZE + 1];
  8.  
  9. void extractNumbersFromLine(const char *line, int& workerId, int& hours) {
  10. int numbersFound = 0;
  11. for (int j = 0; line[j] != '\0'; ++j) {
  12. if (line[j] >= '0' && line[j] <= '9') {
  13. int number = 0;
  14. while (line[j] >= '0' && line[j] <= '9') {
  15. number = number * TEN + (line[j] - '0');
  16. ++j;
  17. }
  18. if (numbersFound == 0) {
  19. workerId = number;
  20. ++numbersFound;
  21. } else if (numbersFound == 1) {
  22. hours = number;
  23. ++numbersFound;
  24. }
  25. }
  26. }
  27. }
  28.  
  29. void updateMaxHours(int workerId, int hours, int& maxHours, int& workerWithMaxHours) {
  30. if (workerHours[workerId] > maxHours) {
  31. maxHours = workerHours[workerId];
  32. workerWithMaxHours = workerId;
  33. } else if (workerHours[workerId] == maxHours && workerId < workerWithMaxHours) {
  34. workerWithMaxHours = workerId;
  35. }
  36. }
  37.  
  38. int main() {
  39. int numberOfLines, maxHours = -1, workerWithMaxHours = -1;
  40. cin >> numberOfLines;
  41. cin.ignore();
  42. for (int i = 0; i < numberOfLines; ++i) {
  43. char line[MAX_SIZE];
  44. cin.getline(line, MAX_SIZE);
  45. int workerId = 0, hours = 0;
  46. extractNumbersFromLine(line, workerId, hours);
  47. workerHours[workerId] += hours;
  48. updateMaxHours(workerId, hours, maxHours, workerWithMaxHours);
  49. }
  50. cout << workerWithMaxHours;
  51. return 0;
  52. }
Success #stdin #stdout 0s 5292KB
stdin
5
Muncitorul 1 a muncit 10 ore
Muncitorul 23 a muncit 5 ore
Muncitorul 3 a muncit 3 ore
Muncitorul 23 a muncit 11 ore
Muncitorul 1 a muncit 2 ore
stdout
23