close
Content
明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。
Input
每組输入有2行,第1行为1个正整数,表示所生成的随机数的个数:N
第2行有N个用空格隔开的正整数,为所产生的随机数。
Output
每組输出也是2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。
Sample Input #1
10 20 40 32 67 40 20 89 300 400 15
Sample Output #1
8 15 20 32 40 67 89 300 400
測資資訊:
記憶體限制: 512 MB
公開 測資點#0 (10%): 1.0s , <1K
公開 測資點#1 (10%): 1.0s , <1K
公開 測資點#2 (10%): 1.0s , <1K
公開 測資點#3 (10%): 1.0s , <1K
公開 測資點#4 (10%): 1.0s , <1K
公開 測資點#5 (10%): 1.0s , <1K
公開 測資點#6 (10%): 1.0s , <1K
公開 測資點#7 (10%): 1.0s , <1K
公開 測資點#8 (10%): 1.0s , <1K
公開 測資點#9 (10%): 1.0s , <1K
公開 測資點#0 (10%): 1.0s , <1K
公開 測資點#1 (10%): 1.0s , <1K
公開 測資點#2 (10%): 1.0s , <1K
公開 測資點#3 (10%): 1.0s , <1K
公開 測資點#4 (10%): 1.0s , <1K
公開 測資點#5 (10%): 1.0s , <1K
公開 測資點#6 (10%): 1.0s , <1K
公開 測資點#7 (10%): 1.0s , <1K
公開 測資點#8 (10%): 1.0s , <1K
公開 測資點#9 (10%): 1.0s , <1K
#include <iostream> #include <map> #include <algorithm> //sort , unique #include <functional> //less<int>() using namespace std; int main() { /*這兩行可以加速*/ ios_base::sync_with_stdio(false); cin.tie(0); //但這一行打了就不能打開terminal int n; while(cin>>n){ int arr[n]; for(int i=0;i<n;i++) { cin>>arr[i]; } sort(arr,arr+n,less<int>());//排序 int count=unique(arr,arr+n)-arr;//去掉重複 cout<<count<<"\n"; //cout<<sizeof(arr)/sizeof(arr[0])<<"\n"; 這是顯示陣列的函數 (沒用到) for(int j=0;j<count;j++){ cout<<arr[j]<<" "; } } return 0; } /* # 排序 sort(arr,arr+陣列元數數量,排序參數(這個之後再補充筆記)) # 去掉重複 1. set這次沒用到,下次再寫 2.unique int count=unique(arr,arr+n)-arr; conut為陣列去重之後的數量 unique他是將重複的數往後放,所以要去重輸出的話 for(int j=0;j<count;j++) { cout<<arr[j]<<" "; } */
文章標籤
全站熱搜