明明的随机数

题目描述

明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤1000),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。

Input Param 

     n               输入随机数的个数     

 inputArray      n个随机整数组成的数组 

     

Return Value

     OutputArray    输出处理后的随机整数


注:测试用例保证输入参数的正确性,答题者无需验证。测试用例不止一组。


 


输入描述:

输入多行,先输入随机整数的个数,再输入相应个数的整数



输出描述:

返回多行,处理后的结果


输入例子:
11
10
20
40
32
67
40
20
89
300
400
15

输出例子:
10
15
20
32
40
67
89
300
400

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 int main(void)
 7 {
 8     int n;
 9     while (cin >> n)
10     {
11         vector<int> inputArray(n);
12         vector<int>::iterator it;
13         for (int i = 0; i < n; i++)
14         {
15             cin >> inputArray[i];
16         }
17         sort(inputArray.begin(), inputArray.end());
18         inputArray.erase(unique(inputArray.begin(), inputArray.end()), inputArray.end());
19 
20         for (it = inputArray.begin(); it != inputArray.end(); it++)
21         {
22             cout << *it << endl;
23         }
24     }
25 
26     return 0;
27 }
 1 // 123.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 
 7 using namespace std;
 8 
 9 //写了个快排练练手
10 int partition(int *p,int low,int high)
11 {
12     int mid=(low+high)/2;
13     if(p[low]>p[high])
14         swap(p[low],p[high]);
15     if(p[mid]>p[high])
16         swap(p[mid],p[high]);
17     if(p[mid]>p[low])
18         swap(p[mid],p[low]);
19     int pivotkey = p[low];
20     while(low<high)
21     {
22         while(low<high && p[high]>=pivotkey)
23             high--;
24         p[low]=p[high];
25         while(low<high && p[low]<=pivotkey)
26             low++;
27         p[high] = p[low];
28     }
29     p[low]=pivotkey;
30     return low;
31 }
32 
33 void Qsort(int * p,int low,int high)
34 {
35     int pivot;
36     if(low<high)
37     {
38         pivot=partition(p,low,high);
39         Qsort(p,low,pivot-1);
40         Qsort(p,pivot+1,high);
41     }
42 
43 }
44 
45 int main()
46 {
47     int N;
48     int *p;
49     while(cin >> N)
50     {
51         p = new int[N];
52         for(int i=0;i<N;i++)
53         {
54             cin>>p[i];
55         }
56         Qsort(p,0,N-1);
57         for(int j=0;j<N;j++)
58         {
59             if(j>=1 && p[j]== p[j-1])
60                 continue;
61             cout<<p[j]<<endl;
62         }
63     }
64 
65     return 0;
66 }
原文地址:https://www.cnblogs.com/hhboboy/p/5522576.html