1.問題說明
11
10
20
40
32
67
40
20
89
300
400
15
10
15
20//只顯示1次
32
40//只顯示1次
67
89
300
400
正常的算法:
1.遍歷所有數組,去除掉重復的數字
2.使用XX排序法,進行數字的排序。
眼前一亮的機器算法
1.生成1-1000的數組,全部給0
2.輸入,或生成隨機數,將隨機數的數字所對應的數組值置1
輸入數字 15,則a[15] = 1;
3.遍歷所有數組,如果a[i]>0,則輸出i。
#include<iostream>
using namespace std;int main()
{int i, k, N, L;int a, Hash[1001];while (cin >> N){/*初始化1000個數組*/for (i = 0; i<1001; i++)Hash[i] = 0;/*輸入排列的數組,將這個數字的地方置1*/for (i = 0; i<N; i++){cin >> a;Hash[a]++;}/*遍歷1000個數組 如果這個地方是大于1就打印*/for (i = 0; i<1001; i++)if (Hash[i]>0)cout << i << endl;}return(0);
}
擴展
int main()
{int arr[100], sum = 0;char c;int x = 4;while (x--){cin >> c;arr[c] = 1;}for (int i = 0; i < 100; i++)if (arr[i] == 1)sum++;cout << sum;system("pause");
}