方法1:
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 const int N = 101; 6 int cnt[N]; 7 8 int main() 9 { 10 int n, k; 11 while ( cin >> n ) 12 { 13 memset( cnt, 0, sizeof(cnt) ); 14 for ( int i = 1; i <= n; i++ ) 15 { 16 int tmp; 17 cin >> tmp; 18 cnt[tmp]++; 19 } 20 cin >> k; 21 int kinds = 0; 22 for ( int i = 1; i <= n; i++ ) 23 { 24 if ( cnt[i] ) kinds++; 25 } 26 int sum = n - kinds; 27 if ( sum < k ) 28 { 29 k -= sum; 30 kinds -= k; 31 } 32 cout << kinds << endl; 33 } 34 return 0; 35 }
方法2:
引入set判断重复,代码只有些许改变。
1 #include <iostream> 2 #include <set> 3 using namespace std; 4 5 int main() 6 { 7 int n, k; 8 while ( cin >> n ) 9 { 10 set<int> s; 11 for ( int i = 1; i <= n; i++ ) 12 { 13 int tmp; 14 cin >> tmp; 15 s.insert(tmp); 16 } 17 cin >> k; 18 int kinds = s.size(), sum = n - kinds; 19 if ( sum < k ) 20 { 21 k -= sum; 22 kinds -= k; 23 } 24 cout << kinds << endl; 25 } 26 return 0; 27 }