ZeroJudge E973: Survey of Satisfaction

Professor Linyang, who specializes in statistics, recently posed a question about calculating the frequency of satisfaction ratings from 0 to 9 in a survey, to explore the formulation of different marketing strategies.

For example, "11221812" indicates that satisfaction rating 1 appears 4 times, satisfaction rating 2 appears 3 times, and satisfaction rating 8 appears 1 time.

Wenwen always gets a headache when she sees a long string of numbers. Therefore, the geeky engineer who admires Wenwen decides to utilize what he learned in programming class to help her calculate the frequencies of each satisfaction rating in the survey, successfully inviting her out for fun! Given a string of numbers representing the satisfaction ratings of each question in the survey, the program should calculate the frequency of each satisfaction rating, sort them in descending order of frequency, and output the satisfaction ratings.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line contains a positive integer N (1 <= N < 2^63), representing the satisfaction ratings of each question in the survey. (If stored in a variable of type long long int, when reading in with scanf(), please use %lld as the format specifier string.)For each test case, output K (1 <= K <= 10) satisfaction ratings. K represents the number of satisfaction ratings (0 to 9) that appear in N. The satisfaction ratings should be output in descending order of occurrence, and if the occurrences are the same, output the lower satisfaction rating first. Separate the satisfaction ratings by a space.
24252644265582 4 5 6 8
12345678910111213141 2 3 4 0 5 6 7 8 9

Thought Process

Using EOF to receive characters, you can collect data one character at a time without needing to use long long int or string + stringstream to handle the data. When receiving a character, increment the count of that digit in a map. You can then use a while (true) loop for output determination. In each iteration of the while loop, run a for loop to determine the current maximum value in the map, output it, and set its corresponding map value to 0. Once all map values greater than 0 have been output, break the while loop, and finally print a newline character.

Sample Code-ZeroJudge E973: Survey of Satisfaction

#include <iostream>
#include <map>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    map<int, int>MAP;
    char ch;
    while (cin >> ch)
    {
        MAP[int(ch - '0')]++;
    }
    while (true)
    {
        int max = -999;
        int max_num;
        for (int i = 0; i<10; i++)
        {
            if (MAP[i] > max)
            {
                max = MAP[i];
                max_num = i;
            }
        }
        if (max <= 0)
        {
            break;
        }
        else
        {
            cout << max_num << " ";
            MAP[max_num] = 0;
        }
    }
    cout << "\n";
}

//ZeroJudge E973
//Dr. SeanXD

Comments