介绍本文内容前先了解几个数列:
数列名 | 公式 | 例子 |
---|---|---|
Fibonacci(斐波那契) | f(x)=f(x-1) +f(x-2);f(1)=f(2)=1 | 1,1,2,3,5,8,13,21 |
Lucas(卢卡斯) | f(x)=1,x∈{1,2} f(x)=f(x−1)+f(x−2),x∈[3,∞) | 1,3,4,7,11,18,29,47 |
Pell | f(1)=1,f(2)=2,f(x)=2*f(x-1)+f(x-2),x>2 | 1,2,5,12,29,70,169,408 |
Triangular | f(x)=x(x+1)/2,x>1 | 1,3,6,10,15,21,28,36 |
Square | f(x)=x*x,x>1 | 1,4,9,16,25,36,49,64 |
Pentagonal | f(x)=x*(3x-1)/2 | 1,5,12,22,35,51,70,92 |
介绍完几个特殊数列后,本文主要依据Array
和Vector
的用法来存储数列。解决方法就是使用可存放连续整数值的容器类型,这种容器类型不仅有自己的名称,也可以通过容器的位置获取具体元素值。
C++中通过内置的Array
(数组)和标准库提供的Vector
类来定义容器,根据个人喜好使用Vector
或者array
,目前大多数的人还是更加倾向于Array
,当然Vector
的用法也要掌握。
【代码已上传至自己github,供参考:github】
1、Array和Vector
定义array
,必须要指定数据类型、名称和指定其尺度大小(也就是array
所存储元素个数),例如:
const int seq_size=18;//在容器内存放18个数值,后面会分为6组使用
int pell_seq[seq_size];
定义vector
,必须要有头文件vector
,vector
是个类模板,类名之后必须用尖括号<>
指定元素类型,容器大小则写在变量名后的小括号()
内,例如:
#include <vector>
vector<int> vec_name(vec_size);
如果我们想要获取容器内某个位置元素值,都可以用索引(index)
的方式,通过下标运算符([])
获得。例如:
pell_seq[0]=1;
pell_seq[1]=2;
for循环
为实现上述数列,需要依次迭代vector
或array
中多个元素,可以使用C++中另一个重要的循环:for循环
。for
循环包括如下几个部分:
for(init-statement;condition;expression){
statement
}
其中:
init-statement
:在循环开始会执行一次,即设置初始值。
condition
:用于循环控制,其值会在每次循环迭代之前计算出来,若condition
为true
,statement会被执行,statement可以是单个语句或者语句块;若condition
为false
,那么statement一次都不会执行。
expression
:会在循环每次迭代结束后进行求值。
例如:
for (int ix = 2; ix < seq_size;++ix) {
pell_seq[ix]=pell_seq[ix-2]+2*pell_seq[ix-1];
}
如果我们想打印出数列中每个值,我们可以迭代整个数列集合,例如:
cout << "The first" << seq_size << "elements of the Pell Series:\n\t";
for (int ix = 0; ix < seq_size; ++ix) {
cout << pell_seq[ix] << " ";
cout << "\n";
}
注意:如果使用vector
,格式会发生变化,vector
知道自己的大小是多少,vec_name.size()
会返回vector
的大小,上面代码修改为:
cout << "The first" << pell_seq.size() << "elements of the Pell Series:\n\t";
for (int ix = 0; ix < pell_seq.size(); ++ix) {
cout << pell_seq[ix] << " ";
cout << "\n";
}
2、具体案例
int main() {
int num = 1;
int usr_guess; //用户猜测的数字
bool num_seq = true; //显示下一组数列
bool guess_again = true; //用户想再猜一次
int guess_num = 0; //用户猜的总次数
int guess_right = 0;//用户猜对次数
char usr_rsp; //用户的回答 内循环用
char try_again; //用户的回答 外循环用
double usr_score = 0.0;//评分比值,采用double双精度类型
const int max_tries = 3;//设置最多猜测次数
int cur_tuple = 0;//表示要显示的元素索引值
const int seq_size = 18;//设置数列最大长度
int elem_seq[seq_size] = { //每个数列存储前三个数字进行猜测
1,2,3, //Fibonacci(斐波那契)
3,4,7, //Lucas(卢卡斯)
2,5,12, //Pell
3,6,10, //Triangular
4,9,16, //Square
5,12,22 //Pentagonal
};
string seq_names[seq_size] = {
"Fibonacci",
"Lucas",
"Pell",
"Triangular",
"Square",
"Pentagonal"
};
while (num_seq == true && cur_tuple < seq_size) {
int try_cnt = 0;//猜的次数与最多次数比较
bool got_it = false; //用户是否猜对
//开始猜测数字
cout << "The first two elements of the squence are: "
<< elem_seq[cur_tuple] << ","
<< elem_seq[cur_tuple + 1] << "."
<< "\nWhat is the next element?\n";
//用户猜错且想再次猜
while (guess_again == true && got_it == false && (try_cnt++ <= max_tries))
{
std::cout << "please input your num:" << endl;
std::cin >> usr_guess;
guess_num++;
//如果猜正确
if (usr_guess == elem_seq[cur_tuple+2]) {
std::cout << "Your guess is right!"
<< elem_seq[cur_tuple + 2]
<< " is the next element in the "
<< seq_names[cur_tuple / 3]
<< " sequence.\n";
got_it = true;
guess_right++;
}
//用户猜错
else {
//判断猜的次数 switch
switch (try_cnt)
{
case(1):
std::cout << "Oops!Nice guess but not quiye it! \n" << endl;
break;
case(2):
std::cout << "Hmm.Sorry.Wrong a second time.\n" << endl;
break;
case(3):
std::cout << "Ah,this is harder than it looks.\n" << endl;
break;
default:
std::cout << "It must be getting pretty frustrating by now! \n" << endl;
break;
}
//是否再试一次
std::cout << "Error!Want to try again?(y/n):";
std::cin >> usr_rsp;
if (usr_rsp == 'N' || usr_rsp == 'n')
guess_again = false;
}
}//内层循环结束
std::cout << "want to try another sequence again?(y/n):";
std::cin >> try_again;
if (try_again == 'N' || try_again == 'n')
num_seq = false;
//用户还想玩,从下一个数列开始
else
cur_tuple += 3;
}//外循环结束
return 0;
}