小编use*_*143的帖子

在数组中查找唯一的数字

好吧,我必须找到数组中有多少个不同的数字.

例如,如果数组是: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)

c++ arrays for-loop numbers

5
推荐指数
3
解决办法
4万
查看次数

标签 统计

arrays ×1

c++ ×1

for-loop ×1

numbers ×1