Files
python/C++专题课程/基础知识/统计个数(数字).cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

46 lines
836 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>
using namespace std;
/*
用随机函数产生100个[0,99]范围内的随机整数,
统计个位上的数字分别为0,1,2,3,4,5,6,7,8,9的数的个数并打印出来
*/
#define MAX 101
void input(int num[]) {
int i;
srand((unsigned)time(NULL));
for(i=1; i<MAX; i++)
num[i]=rand()%100;
}
void output(int num[]) {
int i;
for(i=1; i<MAX; i++) {
printf("%5d", num[i]);
if(0==i%10)
printf("\n");
}
printf("\n");
}
void cal_num(int num[], int count[]) {
int i, mod;
for(i=1; i<MAX; i++) {
mod=num[i]%10;
count[mod]++;
}
}
int main() {
int num[MAX];
int i, count[10];
memset(count, 0, 10*sizeof(int)); /* initial count[] to 0 */
input(num);
printf("100 num:\n");
output(num);
cal_num(num, count);
for(i=0; i<10; i++)
printf("%d: %d\n", i, count[i]);
return 0;
}