Dear friends,have you found the mistakes in Zhishen Lu Eating Steamed Bun v1?To tell you the truth,I didn't discovery them through observing by myself.What's worth,the grammar is right but the result is wrong,which means there are logical mistakes hard to find.
Then I consulted a condisciple,with whose help I realized that when the value of monk[i] equals 1,which means that monk has got a steamed bun,the loop will terminate immediately,while not what l expect,continuing next circle.After thinking deeply,Zhishen Lu Eating Steamed Bun v2 came out.
//Zhishen Lu Eating Steamed Bun v2
#include <stdio.h>
int main(void)
{
int monk[101]={0},count=0,steabun=0,i=1;//Easy to count.monk[1]~monk[100] for the flower monk Zhishen Lu and other 99 monks,value 0 means they will participate in numbering off.count to number off,steabun for the number of steamed buns already divided and i as a simple counter.
while (steabun<99)
{
while (monk[i])
i++;//Make sure that the monk hasn't got a steamed bun yet
if (++count%5==0)
{
monk[i]=1;//Get a steamed bun
steabun++;
}
if (i==100)
i=1;//Next circle
}
for(i=1;monk[i];i++) ;//Do you know why ";" exists?
printf("The position number of Zhishen Lu is %d.\n",i);
return 0;
}
What's the result?"The position number of Zhishen Lu is 157."
……
It's Okay.I can bear it,since I experienced the failure of the first version.
What exactly is that problem?I'd like to leave it for all of you readers to think about.Thanks for your consistent attentions.
No comments:
Post a Comment