面向对象程序设计上机练习六(类和对象)

来源:互联网 发布:有趣的好友分组知乎 编辑:程序博客网 时间:2024/06/08 08:56

probloem

面向对象程序设计上机练习六(类和对象)
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description

用类成员函数完成5个整型数组元素的输入、从小到大排序、排序后数组元素的输出。
Input

输入5个数组元素。
Output

输出5个数组元素从小到大排序后的结果。(最后一个数后面既没有空格也没有换行)
Example Input

8 9 1 5 4
Example Output

1 4 5 8 9
Hint

Author

zlh

code

#include <iostream>using namespace std;class toSort{private:    int a[6];public:    void in(int i,int m);    void qsort(int l, int r);    void showSort()    {        int i;    for(i=0;i<5;i++)    {        if(i==4)            cout << a[i] << endl;        else            cout << a[i] << " ";    }    }};void toSort::in(int i,int m){    a[i] = m;    i++;}void toSort::qsort(int l,int r)  //快排函数{    int k =a[l],i=0,j=r;    if(l>=r) return ;    while(i<j)    {        while(i<j&&a[j]>=k) j--;            a[i] = a[j];        while(i<j&&a[i]<=k) i++;            a[j] = a[i];    }    a[i] = k;    qsort(0,i-1);    qsort(i+1,r);}int main(){    toSort newSort;    int i,m;    i = 0;    while(i<5)    {        cin >> m;        newSort.in(i,m);        i++;    }    newSort.qsort(0,4);    newSort.showSort();}
阅读全文
0 0