题目:用c语言实现一个产生N个4位随机数,并统计0~9在个位,十位,百位,千位出现的次数。
基本程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main(void)
{
static int count[4][10];//统计0~9在个位,十位,百位,千位出现的次数
const int a[] = {0,1,2,3,4,5,6,7,8,9};
int b[4];
int i = 0;
int j = 0;
int k = 0;
int random;
srand((unsigned)time(NULL));
while(k++ < 100)
{
random = rand()%10000;
printf("%d/n",random);
b[0] = random%10;
b[1] = random/10%10;
b[2] = random/100%10;
b[3] = random/1000;
for(i=0; i<4; i++)
{
for(j=0; j<10; j++)
{
if(b[i] == a[j])
count[i][j]++;
}
}
}
for(i=0; i<4; i++)
{
for(j=0; j<10; j++)
{
printf("count[%d][%d]=%d ",i,j,count[i][j]);
}
printf("/n");
}
}
注:如有错误请指点。