分散式ID,分散式鎖,限流演算法,微服務原則,CAP,BASE,雙寫一致性
阿新 • • 發佈:2021-10-05
明明想在學校中請一些同學一起做一項問卷調查。
為了實驗的客觀性,他先用計算機生成了NN個11到10001000之間的隨機整數,對於其中重複的數字,只保留一個,把其餘相同的數去掉,不同的數對應著不同的學生的學號。
然後再把這些數從小到大排序,按照排好的順序去找同學做調查。
請你協助明明完成“去重”與“排序”的工作。
輸入格式
輸入檔案包含22行,第11行為11個正整數,表示所生成的隨機數的個數:NN。
第22行有NN個用空格隔開的正整數,為所產生的隨機數。
輸出格式
輸出檔案也是22行,第11行為11個正整數MM,表示不相同的隨機數的個數。
第22行為MM個用空格隔開的正整數,為從小到大排好序的不相同的隨機數。
資料範圍
1≤N≤1001≤N≤100
輸入樣例:
10
20 40 32 67 40 20 89 300 400 15
輸出樣例:
ANS1:(hash)
8
15 20 32 40 67 89 300 400
CODE
#include <iostream> using namespace std; #define MAXN 1001 bool hash[MAXN]; int n, tmp, ans; int main(){ cin >> n; for(int i=1; i<=n; i++){ cin >> tmp; hash[tmp] = 1; } for(int i=1; i<=1000; i++) ans += hash[i]; cout << ans << endl; return 0; }
ANS2:(set)
#include <cstdio> #include <set> using namespace std; int n, tmp; set<int> s; int main(){ scanf("%d", &n); while(n--){ scanf("%d", &tmp); s.insert(tmp); } printf("%d\n", s.size()); for(set<int>::iterator it = s.begin(); it != s.end(); it++) printf("%d ", *it); return 0; }