Tuesday, November 3, 2015

Funny C Program:Zhishen Lu Eating Steamed Bun v3

As far as I am concerned,this is really a tough problem.Whether there is obvious significance,the interest drove me to insist on solving this problem.
I analyzed the code carefully.Surprisingly,I found that the 2th program can only be run rightly when monk[100] equals 0.That is to say,on the condition that monk[100] equals 1,the statement "i++;" will be run.Then what will happen?Obviously the code should be corrected further.
Here is the 3rd version.

//Zhishen Lu Eating Steamed Bun v3
#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)
{
if (i<100)
i++;
else
i=1;//Make sure to mumber off continuously
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++;
}
}
for(i=1;monk[i];i++) ;//Do you know why ";" exists?
printf("The position number of Zhishen Lu is %d.\n",i);
return 0;
}

Does it seem better?Compile and run it,please.
Screen shows the same as that of the previous version!
Amazed?Lost?Cogitate!
The if-else statement does make sure the range of i from 1 to 100,but the "while" statement doesn't.Have you found it?
Yes,when i equals 99,through if-else statement,i becomes 100,but when monk[100] doesn't equal 0,"i++;" statement in the loop will be run.Are you familiar with this process?Right,it's not different from the 2nd version in substance.
Therefore,how can we deal with it on earth?Wait for my next version,please.

No comments:

Post a Comment