#include <stdio.h>
int main()
{
int marks[40] = {83, 86, 97, 83, 93, 83, 86, 52, 49, 41, 42, 47, 90, 59, 63, 86, 40, 46, 92, 56, 51, 48, 67, 49, 42, 90, 42, 83, 47, 95, 69, 82, 82, 58, 69, 67, 53, 56, 71, 62};
int i,j,count[101],tm;
for(i=0;i<101;i++)
{
count[i]=0;
}
for(i=0;i<40;i++)
{
tm=marks[i];
count[tm]=count[tm]+1;
}
for(i=0;i<=100;i++)
{
if(count[i]!=0)
{
printf("Marks: %d count: %d\n",i,count[i]);
}
}
return 0;
}
这是我的代码。我在这里不明白。
首先i=0
,marks[i]
,marks[0]
表示marks[0]=83
所以
tm=marks[0]=83
然后
count[tm]=count[tm]+1;
我不明白这一行。
答案 0 :(得分:1)
我在您的代码中添加了一些注释,以便您可以更轻松地了解正在发生的事情。
#include <stdio.h>
int main(void) // use void if your function has no parameters
{
int marks[] = {83, 86, 97, 83, 93, 83, 86, 52, 49, 41, 42, 47, 90, 59, 63, 86, 40, 46, 92, 56, 51, 48, 67, 49, 42, 90, 42, 83, 47, 95, 69, 82, 82, 58, 69, 67, 53, 56, 71, 62}; // you can leave out the array size if you instantiate it afterwards
int i, tm; // j is never used, so leave it out
int count[101]; // count[i] tells you in the end how often the mark i occured in the marks array
for(i=0; i < 101; i++) // initialize the count array with zeros
{
count[i]=0;
}
for(i=0;i<40;i++) // loop over the marks array and increment thhe count array at the current mark position by one
{
tm=marks[i];
count[tm]=count[tm]+1; // increment the count of mark i by one
}
// marks[i] tells you how often i appears in the marks array -> marks is a frequency table
for(i=0;i<=100;i++) // print out how often a mark appeared, but only if it appeared at least once
{
if(count[i]!=0)
{
printf("Marks: %d count: %d\n",i,count[i]);
}
}
return (0);
}
答案 1 :(得分:0)
这两行
tm=marks[i];
count[tm]=count[tm]+1;
执行以下操作:
marks[i]
tm
count[tm]
1
添加到3中提取的内容。count[tm]
中,覆盖3中已拉出的值。通过在步骤4中添加1,您可以计算特定标记的出现次数。