实质是计算数组中的重复个数
int* histogram(int a[], int length int M) {
int * b = new int[M];
for(int i = 0; i < M, i++){
int num = 0;
for(int j = 0; j < length; j++){
if(a[j] == i){
num++;
}
}
b[i] = num;
}
return b;
}
int main()
{
int M = 5;
int length = 5;
int a[length] = { 1, 2, 2, 1, 4};
int* b = histogram(a, length, M);
int k = 0;
for (int i = 0; i < M; i++) {
cout << b[i] << " ";
k += b[i];
}
if(k == length){
cout << "数组中的值均在0~M-1之间" << endl;
}
else{
cout << "数组中的值不全在0~M-1之间" << endl;
}
}