Maybe you have already been impatient,or you got the right answer through your own efforts.However,the steps of my sharing will not stop.
Since if-else statement can't prevent "while" circle increasing i to101,why not swap their places?Putting the if-else statement behind the "while" circle could effectively avoid i becoming greater than 100.
Following is the source code.
//Zhishen Lu Eating Steamed Bun v4
#include <stdio.h>
int main(void)
{
int monk[101]={0},count=0,steabun=0,i=0;//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 (i<100)
i++;
else
i=1;//Make sure to mumber off continuously
if (++count%5==0)
{
monk[i]=1;//Get a steamed bun
steabun++;
}
}
for(i=1;monk[i];i++) ;//Do you know why ";" exists?
printf("The position number of Zhishen Lu is %d.\n",i);
return 0;
}
Facts showed that I was in vain.You may have run the program,so what sentence was on the screen?"The position number of Zhishen Lu is 1."It looked like that I returned to the origin.
Acturally,after every circle,i is reset to 1 and the elder,who is the first monk,will participate in numbering off regardless of the value of monk[1].In the meantime,if monk[100] equals 1,"while" circle will add 1 to i,which means the compiler is going to determine whether the value of monk[101] equals 0 or not.That's what we don't expect.
No comments:
Post a Comment