好吧,我必须找到数组中有多少个不同的数字.
例如,如果数组是:1 9 4 5 8 3 1 3 5
输出应为6,因为1,9,4,5,8,3是唯一的,1,3,5是重复的(不是唯一的).
所以,这是我的代码到目前为止.....没有正常思考.
#include <iostream>
using namespace std;
int main() {
int r = 0, a[50], n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int j = 0; j < n; j++) {
for (int k = 0; k < j; k++) {
if (a[k] != a[j]) r++;
}
}
cout << r << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)