James's survey assignment

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.