## Distinct Numbers [題目連結](https://cses.fi/problemset/task/1621) 給定一整數 $n$,接下來一行有 $n$ 個整數,請你判斷這些整數當中有幾個不重複的元素 $2\ 3\ 3\ 2\ 3$ => $2\ 3$ --- 題目標籤 : 排序、迴圈、判斷 本題提示 : 請不要使用 set、map 等工具快速判斷,排序後的重複元素必定排在一起 解題思路 : 雖然 set 能夠快速地解決這個問題,但偷懶的方式應該在學會正確的方法,同時避免後面的學習有問題 接續提示,重複元素排在一起,所以我們只要計算前後不相同的元素有幾個,也就是不同元素的交界處 最後交界處 $+1$ 就等於答案,這個可以用多個不同顏色的積木平放去理解 ```cpp= #include<bits/stdc++.h> using namespace std ; int num[200005] ; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) ; int n ; cin >> n ; for (int i=0;i<n;i++) cin >> num[i] ; sort(num, num+n) ; int ans = 1 ; // ans 交界處數量(預先 +1) for (int i=1;i<n;i++) if (num[i] != num[i-1]) // 交界處 ans++ ; cout << ans ; return 0 ; } ``` ```cpp= #include<bits/stdc++.h> using namespace std ; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) ; int n, now ; set<int> s ; // set 可去除重複元素 cin >> n ; for (int i=0;i<n;i++) { cin >> now ; s.insert(now) ; } cout << s.size() ; return 0 ; } ```