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.
Monday, November 2, 2015
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.
Saturday, October 31, 2015
What Do You Think About The Universal Two-Child Policy In China?
Several days ago,Communist Party of China Central Committee stated that the universal two-child policy would be implementated from then on,which means China further relaxed its family planning policy,whose history are more than 30years.
Maybe you thought the family planning policy beyond understanding.Then what do you think about the new policy?
Although it seemed unimaginable for the government to prevent families giving births to more children,it's under special backgroun,or the population of China today would be more unimaginable.On the other hand,to relax the family planning policy was a tendency,of course.
Are you afraid of population increasing of China since the universal two-child policy?Acturally,the era is chaging,and concepts of people are changing,too.The costs to bring up a child are becoming more and more,especially in urban areas.And there are also some other factors that impact the concepts of people.So it's time to carry out the universal two-child policy in China.
What's your opinion?Mind sharing with us?
Maybe you thought the family planning policy beyond understanding.Then what do you think about the new policy?
Although it seemed unimaginable for the government to prevent families giving births to more children,it's under special backgroun,or the population of China today would be more unimaginable.On the other hand,to relax the family planning policy was a tendency,of course.
Are you afraid of population increasing of China since the universal two-child policy?Acturally,the era is chaging,and concepts of people are changing,too.The costs to bring up a child are becoming more and more,especially in urban areas.And there are also some other factors that impact the concepts of people.So it's time to carry out the universal two-child policy in China.
What's your opinion?Mind sharing with us?
Thursday, October 29, 2015
Go After Your Interest,and Realize Your Dream
I posted a blog yesterday discussing whether it's worthy to go to university.And I did also say that I won't stop schooling.But it's really hard for me to learn my major related to biology,which was transferred by my school.
The biggest problem comes from low interest.In fact,I am good at logical thinking while not memorizing,so the scores of some of my courses such as higher mathematics and linear algebra are very high,but those of professional courses are relatively low.As a result,I have started to learn programming and software development by myself.I think it's interesting to make the computer work as you want.
I will go after my interest and keep trying to realize my dream to be a programmer.And what about you?
If you are a programmer,I need your advice,of course.Thank you!
The biggest problem comes from low interest.In fact,I am good at logical thinking while not memorizing,so the scores of some of my courses such as higher mathematics and linear algebra are very high,but those of professional courses are relatively low.As a result,I have started to learn programming and software development by myself.I think it's interesting to make the computer work as you want.
I will go after my interest and keep trying to realize my dream to be a programmer.And what about you?
If you are a programmer,I need your advice,of course.Thank you!
Subscribe to:
Posts (Atom)