"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!
Friday, November 6, 2015
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.
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.
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.
Monday, November 2, 2015
Funny C Program:Zhishen Lu Eating Steamed Bun v2
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.
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.
Subscribe to:
Posts (Atom)