James's survey assignment
Description
James's teacher assigned a survey assignment. James wanted to ask some students to do a questionnaire survey in the school. For the sake of objectivity, I want to use my computer knowledge to help myself. He first generates n random integers (0 <n ≤ 1000) between 1 and 1000 on the computer. For repeated numbers, only one is retained and the remaining identical numbers are removed, different numbers correspond to different student IDs. Then sort these numbers from small to large and sort them to the students for investigation. Please help clearly complete "de-duplication" and "sorting.
-
Input
-
There are two rows in the input, and 1st act as one positive integer, indicating the number of random numbers generated: N 2nd rows have n positive integers separated by spaces, which are the random numbers generated.
-
Output
-
The output is also two rows. 1st acts as a positive integer m, indicating the number of random numbers that are not the same. 2nd act M positive integers separated by spaces. They are random numbers in ascending order.
-
Sample Input
-
1020 40 32 67 40 20 89 300 400 15
-
Sample output
-
815 20 32 40 67 89 300 400
#include<iostream> #include<algorithm> using namespace std; int main() { int n,i,j,count,k; int a[1001],b[1001]; while(cin>>n) { count=0; for( i=0;i<n;i++) { cin>>a[i]; } for(i=0;i<n;i++) for(j=1+i;j<n;j++) { if(a[i]==a[j]) { a[i]=0; count++; } } for(i=0,k=0;i<n;) { if(a[i]==0) i++; else { b[k]=a[i]; i++,k++; } } sort(b,b+n-count); cout<<n-count<<endl; for(k=0;k<n-count-1;k++) cout<<b[k]<<" "; cout<<b[n-count-1]<<endl; } return 0; }
James's survey assignment