#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
int a=2;
int ans=2;
for(int i=0;i<=9;i++){
ans=ans+pow(a,i);
}
cout<<ans<<endl;
return 0;
}
He hugged his son, knowing that he had been a good father.
He kissed his wife on the forehead one last time.
The old man smiled and closed his eyes.
Then, nothing happened. The watch beeped once and turn off. The man
stood standing there, very much alive. You would think that in that
moment he would have been overjoyed. Instead, for the first time in his
life, the man was scared
Two
Gratitude and giving thanks is really one of the most powerful things
you can practise every day, if you are committed to achieving anything
in life.
Money is easy. It’s easy to make; it’s easy to spend. And when it
comes to the end, we can’t take any of it with us. Let’s make truckloads
of it, but let’s not stress over it.
Three
You know, when I was younger, when I was only 14, I was only 14, I
was told by a drama teacher that I might do okay if I was happy to
settle for the fat girl parts.
Look at me now. Look at me now.
So what I feel like saying in those moments is, you know, any young
woman who is ever been put down by a teacher or a friend or even a
parent, just don’t listen to any of it, because that’s what I did.
Four
The life is what you make it. No matter what, you’re going to mess up
something; it’s universal truth. But the good part is you get to decide
how you’re going to mess it up.
So keep your head high, keep your chin up, and most importantly, keep
smiling, because life’s a beautiful thing and there’s so much to smile
about.
上面这个程序运行一次只能输出斐波那契数列中的一个数值, 下面对该程序进行一个改进, 使得可以输出指定的前 n 位的斐波那契数列, 程序如下:
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
int f(int n){
if(n==0){
return 1;
}else if(n==1){
return 1;
}else{
return f(n-1)+f(n-2);
}
}
int main(){
int n=5; //设置打印前 6 个斐波那契数列元素
for(int i=0;i<=n;i++){
int a=f(i);
printf("%d ",a);
}
system("pause");
return 0;
}
上述程序的运行结果是:
1 1 2 3 5 8
汉诺塔 (Tower of Hanoi)
Wikipedia 上对于汉诺塔问题的说明如下:
The Tower of Hanoi (also called the Tower of Brahma or Lucas’ Tower and sometimes pluralized) is a mathematical game or puzzle. It consists of three rods and a number of disks of different sizes, which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1. Only one disk can be moved at a time. 2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod. 3. No larger disk may be placed on top of a smaller disk.
With 3 disks, the puzzle can be solved in 7 moves. The minimal number of moves required to solve a Tower of Hanoi puzzle is 2n − 1, where n is the number of disks.
但是 A 柱上的圆盘并没有被移动完, 为了能移动完, 我们现在必须想办法让第 3 个圆盘到 C 柱(之后以此类推可以让第 4,5,6…64个圆盘到 C 柱);
于是, 我们把第 1 个圆盘从 B 柱移动到 A 柱, 把第 2 个圆盘从 C 柱移动到 B 柱, 把 第 1 个圆盘从 A 柱移动到 B 柱, 把第 3 个圆盘从 A 柱移动到 C 柱.
好啦, 现在第 3 个圆盘成功的到了 C 柱.
不过还没完, 我们需要重复上面的过程, 直到把第 64 个圆盘移动到 C 柱, 之后, 第 64 个圆盘在之后的移动过程中就可以不用动了, 一直待在 C 柱上.
对于剩下的 63 个圆盘重复和原来 64 个圆盘一样的移动过程即可.
这么移动的工作量是很大的, 移动完全部 64 个圆盘需要大约 1800 亿亿步:
18,446,744,073,709,551,615
根据上面的介绍, 用 C++ 实现的话, 程序是这样的:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
//假设函数 (int n, char a, char b, char c) 的作用是将 n 个圆盘从 a 移动到 c
void f(int n, char a, char b, char c){
if(n==1){
cout<<"将盘子"<<n<<"从"<<a<<"移动到"<<c<<endl;
//当只有 1 个盘子时, 直接从 a 移动到 c
}else{
f(n-1,a,c,b);
//将 n-1 个圆盘由 a 移动到 b
cout<<"将盘子"<<n<<"从"<<a<<"移动到"<<c<<endl;
f(n-1,b,a,c);
//将 n-1 个圆盘由 b 移动到 c
}
}
int main(){
f(3,'a','b','c');
return 0;
}
上面这个程序的运行结果如下 (圆盘的序号越大则圆盘越大):
假设有三个圆盘, 移动顺序是这样的:
将盘子1从a移动到c
将盘子2从a移动到b
将盘子1从c移动到b
将盘子3从a移动到c
将盘子1从b移动到a
将盘子2从b移动到c
将盘子1从a移动到c
假设有四个圆盘, 移动顺序是这样的:
将盘子1从a移动到b
将盘子2从a移动到c
将盘子1从b移动到c
将盘子3从a移动到b
将盘子1从c移动到a
将盘子2从c移动到b
将盘子1从a移动到b
将盘子4从a移动到c
将盘子1从b移动到c
将盘子2从b移动到a
将盘子1从c移动到a
将盘子3从b移动到c
将盘子1从a移动到b
将盘子1从a移动到b
将盘子2从a移动到c
将盘子1从b移动到c
最后, 来看几个关于汉诺塔问题的动图吧.
图 12:
图 12 By André Karwath aka Aka – Own work, CC BY-SA 2.5, https://commons.wikimedia.org/w/index.php?curid=85401
I hope you meet people with a different point of view.
I hope you live a life you’re proud of.
If you find that you’re not, I hope you have the strength to start all over again.
Two
Jack: Listen, Rose. You’re gonna get out of here, you’re gonna go on,
and you’re gonna make a lots of babies, and you’re gonna watch them
grow. You’re gonna die and old…an old lady warm in her bed, not here,
not this night. Not like this. do you understand me?
Three
You are the books you read, the films you watch, the music you listen
to, the people you meet, the dreams you have, and the conversations you
engage in. You are what you take from these.
You are the sound of the ocean, the breath of fresh air, the brightest light and the darkest corner.
You are every single day.
Four
If you think you are beaten, you are.
If you think you dare not, you don’t.
If you want to win, but think you can’t, it’s almost a cinch you won’t.
If you think you’ll lose, you’re lost; for out in the world we find
success begins with a fellow’s will. It’s all in the state of the mind.
Life’s battles don’t always go to the stronger and faster man.
But sooner or later the man who wins is the man who thinks he can.
Five
“Just that,” said the fox. “To me, you are still nothing more than a little boy who is just like a hundred thousand other little boys. And I have no need of you. And you, on your part, have no need of me. To you, I am nothing more than a fox like a hundred thousand other foxes. But if you tame me, then we shall need each other. To me, you will be unique in all the world. To you, I shall be unique in all the world…”