Friday, November 6, 2015

Funny C Program:Zhishen Lu Eating Steamed Bun v6

Because of my puzzle,I'd like to share the modified code straightly,hoping you can explain why to alter like this.

//Zhishen Lu Eating Steamed Bun v6
#include <stdio.h>
int main(void)
{
int monk[101],count=0,steabun=0,i;//Easy to count.monk[1]~monk[100] for the flower monk Zhishen Lu and other 99 monks,count to number off,steabun for the number of steamed buns already divided and i as a simple counter.
for (i=1;i<101;i++)
monk[i]=0;//Initialization of the array.Value 0 means they will participate in numbering off.
i=0;
while (steabun<99)
{
if (++i<=100&&!monk[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++;
}
}
else if (i>100)
i=0;//Make sure to mumber off continuously
}
for(i=1;monk[i];i++) ;//Do you know why ";" exists?
printf("The position number of Zhishen Lu is %d.\n",i);
return 0;
}

Find any differences between the 6th version and the 5th one?
Right,we initialised monk[1] to monk[100] through a "for" circle in the latest version while not when defining the array.
But I learned from the textbook that an array could be defined and initialized like this:"int a[10]={0};"which means every array element equals 0.Then what's wrong with the 5th version?If interested,pay attention to my posts.

Funny C Program:Zhishen Lu Eating Steamed Bun v5

"If the two conditions of judgement mentioned in the previous version are combined together,will the problem be solved?"I asked myself.Then I tried to make it again and again.
The process was tough.You might notice that in the previous version,expressions in brackets are both in contrast to the right conditions to continuously number off.As I tried to put them together,using "&&" or "||" was a thorny problem.I tried many times,but still in vain.Eventually,I noticed that the right condition is very certain.Why not number off directly as the expression meets the condition?As to other cases,consider them expectly.
Here comes the 5th version of this program.

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

How about this version?Seems perfect?No!
The screen showed nothing at all when running this program.
But I couldn't discover the errors by myself.I looked for help from a friend,and the problem was solved successfully.But it's not convictive enough,I thought.Want to know more about the next version?It's coming soon!

Wednesday, November 4, 2015

Funny C Program:Zhishen Lu Eating Steamed Bun v4

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.

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.