对于彩票游戏,我需要显示每次用户在屏幕上为数字1-42输入一组新数字时输入的数字的频率,例如,它可能会显示
数字1已被选中x次数7被选中x次数28被选中x次等,这必须在函数中完成这里是我的代码请帮助谢谢
void numberFrequency(int *nums,int *frequency)
{
int i;
int j;
int temp;
int freq[42]={1,2,3,4,5,6,7,8,9,10,11,
12,13,14,15,16,17,18,19,
20,21,22,23,24,25,26,27,
28,29,30,31,32,33,34,35,
36,37,38,39,40,41,42};
for (i=0;i<NUMS;i++)
{
for (j=0;j<FREQUENCY;j++)
{
if (*(nums+i)==j+1);
{
temp = *(frequency+j);
temp++;
*(frequency+j) = temp;
}
}
}
for(j=0;j<FREQUENCY;j++)
{
if (*(frequency+j)>=1)
{
printf("\nThe number %d was entered %d times ",j+1,*(frequency+j));
}
}
}
答案 0 :(得分:0)
此代码假定有效输入。 此代码将打印每个号码被选中的时间。
void numberFrequency(int *nums)
{
int i;
int freq[42];
for(i=0;i<42;i++)
freq[i] = 0;//init counters to 0;
for (i=0;i<NUMS;i++)
freq[nums[i]]++;
for(i=0;i<42;i++)
printf("The num:%d entered $d times",i+1,freq[i]);
}