ZeroJudge D190: Age Sort

UVa 11462-Age Sort

You're given the ages of all citizens of a country, aged one year or older (inclusive). You know that no one in the country lives to be 100 years old or older. Now, your task is simple: sort all ages in ascending order.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The input consists of multiple sets of data, each starting with an integer N (where 0 < N ≤ 2000000), representing the number of people. The next line contains N integers representing their ages. When N=0, it signifies the end of input, and this set of data should not be processed.For each set of data, print a line containing N integers separated by spaces. These integers represent the ages of the people, sorted in ascending order.
5
3 4 2 1 5
5
2 3 2 3 1
0
1 2 3 4 5
1 2 2 3 3

Thought Process

Collect the integers to be sorted into an array, then use a sorting algorithm to sort them, and finally use a for loop to output them from the first data to the N-1th data.

Sample Code-ZeroJudge D190: Age Sort

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(nullptr);
    int N;
    while (cin >> N && N != 0)
    {
        vector<int>num;
        for (int i = 0; i<N; i++)
        {
            int tmp;
            cin >> tmp;
            num.push_back(tmp);
        }
        sort(num.begin(), num.end());
        for (int i = 0; i<N; i++)
        {
            cout << num[i] << " ";
        }
        cout << "\n";
    }
}

//ZeroJudge D190
//Dr. SeanXD

Comments