Getting started sorting exercises 4 odd single solution to a problem increasing sequence

Written for: "Informatics Olympiad through a" second chapter superiors Exercise 2

Title Description

Given a length \ (N (1 \ le n \ le 500) \) a sequence of positive integers, wherein the set of all odd removed in ascending output.

Input Format

A first input behavior \ (N \) .
A second input behavior \ (N \) positive integers, separated by spaces therebetween. Ensure that at least a data input odd.

Output Format

Wherein the output of the odd elements in ascending order, separated by a comma between any two.

Sample input

10
1 3 2 6 5 4 9 8 7 10

Sample Output

1,3,5,7,9

Topic analysis

This question is the main test and how to select the odd sort.
So how to select all odd, in fact, we can select all odd only when the input is ignored while the even number.
We can open an array a[]and an integer variable cnt, cntinitially \ (0 \) .
Then we cycle \ (N \) times, each time you enter a number x, if this number is odd, then we execute:

a[cnt++] = x;

It is added into the array a[]of.
Then the input end, all the odd remain in the array a[]in from a[0]to a[cnt-1].
Then we sort them and outputs can be.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
int a[500], n, x, cnt;
int main() {
    cin >> n;
    while (n --) {
        cin >> x;
        if (x % 2) a[cnt++] = x;
    }
    sort(a, a+cnt);
    for (int i = 0; i < cnt ; i ++)
        cout << (i ? "," : "") << a[i];
    cout << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/zifeiynoip/p/11450502.html