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.
Wednesday, November 4, 2015
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.
Sunday, November 1, 2015
Funny C Program:Zhishen Lu Eating Steamed Bun v1
Zhishen Lu,who is called "flower monk",is one of the 108 heroes in All Men Are Brothers,which is one of the Four Great Classical Novels of Chinese literature.It's said that he went to the daxiangguo temple in Prefecture of Kaifeng at a noon in a hurry to bum lunch.But there were only 99 steamed buns for 99 monks in the temple exactly.So what happened later?
The elder Zhiqing didn't want to offend Zhishen Lu,so he figured out a good idea.
Firstly,he arranged Zhishen Lu at a specific position.
Then,the elder said to all the people,"let's form a ring to number off beginning from me.The 5th men can get a steamed bun and leave."Other people continued to number off from 1 to 5,of course.
Finally,all men but Zhishen Lu ate a steamed bun.What a surprise!
Here comes the problem,"how did it happen?Or,where are Zhishen Lu?"
Smart friends,can you find the position number of Zhishen Lu?
As a matter of fact,I knew this story from a mooc teaching C programming.And I was prompted to use sieve method.Although prompted,I spent one day to find the correct answer.Too long,right?What's worse,I still have some doubts after knowing the answer.It appeared terrible,however,I learned a lot.
Now share my first version of the source code.
//Zhishen Lu Eating Steamed Bun v1
#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&&!monk[++i])
{
if (++count%5==0)
{
monk[i]=1;//Get a steamed bun
steabun++;
}
if (i==100)
i=0;//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;
}
Through looking at it,do you find any problems.If not,debug or run it in person.
As a matter of fact,when I finnished this code,I was ecstatic,with great achievability of solving this problem.But quickly,I felt disappointed looking at the result printed on the screen,"The position number of Zhishen Lu is 1. "
Of course,it's wrong,for the elder was at the 1st position.How to correct?I was frustrated.Want to know more?Follow my posts next several days.
In addition,I wrote,compile,and run the code on C4droid on my Android phone,and my blogs are posted through the mobile phone,too.As the operations on the Android phone are limited,such as no debugger on it,so you can barely see pictures in my blogs,and the code was not highlighted.I'm sorry and embarrassed for my economic condition not to buy a computer.But I'm still glad to share.I hope you can gain something through reading my blogs.Thanks.
The elder Zhiqing didn't want to offend Zhishen Lu,so he figured out a good idea.
Firstly,he arranged Zhishen Lu at a specific position.
Then,the elder said to all the people,"let's form a ring to number off beginning from me.The 5th men can get a steamed bun and leave."Other people continued to number off from 1 to 5,of course.
Finally,all men but Zhishen Lu ate a steamed bun.What a surprise!
Here comes the problem,"how did it happen?Or,where are Zhishen Lu?"
Smart friends,can you find the position number of Zhishen Lu?
As a matter of fact,I knew this story from a mooc teaching C programming.And I was prompted to use sieve method.Although prompted,I spent one day to find the correct answer.Too long,right?What's worse,I still have some doubts after knowing the answer.It appeared terrible,however,I learned a lot.
Now share my first version of the source code.
//Zhishen Lu Eating Steamed Bun v1
#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&&!monk[++i])
{
if (++count%5==0)
{
monk[i]=1;//Get a steamed bun
steabun++;
}
if (i==100)
i=0;//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;
}
Through looking at it,do you find any problems.If not,debug or run it in person.
As a matter of fact,when I finnished this code,I was ecstatic,with great achievability of solving this problem.But quickly,I felt disappointed looking at the result printed on the screen,"The position number of Zhishen Lu is 1. "
Of course,it's wrong,for the elder was at the 1st position.How to correct?I was frustrated.Want to know more?Follow my posts next several days.
In addition,I wrote,compile,and run the code on C4droid on my Android phone,and my blogs are posted through the mobile phone,too.As the operations on the Android phone are limited,such as no debugger on it,so you can barely see pictures in my blogs,and the code was not highlighted.I'm sorry and embarrassed for my economic condition not to buy a computer.But I'm still glad to share.I hope you can gain something through reading my blogs.Thanks.
Subscribe to:
Posts (Atom)