給定含有n個整數的序列,要求對這個序列進行去重操做。所謂去重,是指對這個序列中每一個重複出現的數,只保留該數第一次出現的位置,刪除其他位置。ios
5 10 12 93 12 75
10 12 93 75
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int n, a[20001], b[200000] = { 0 }; 6 cin >> n; 7 for (int i = 0; i < n; ++i) 8 { 9 cin >> a[i]; 10 } 11 for (int i = 0; i < n; ++i) 12 { 13 if (b[a[i]] == 0) 14 { 15 b[a[i]] = 1;//標記 16 cout << a[i] << " "; 17 } 18 } 19 20 21 return 0; 22 }
1 #include<cstdio> 2 #include<cstdlib> 3 int main() 4 { 5 int a[20001]; 6 int n; 7 scanf("%d", &n); 8 for (int i = 1; i <= n; i++) 9 { 10 scanf("%d", &a[i]); 11 for (int j = 1; j < i; j++) 12 { 13 if (a[j] == a[i]) 14 { 15 i = i - 1; 16 n = n - 1; 17 } 18 } 19 } 20 for (int i = 1; i <= n; i++) 21 { 22 printf("%d ", a[i]); 23 } 24 }