用户猜测数列程序

本文介绍了一个基于控制台的应用程序,该程序通过展示六个数学数列的前两个元素,邀请用户猜测下一个元素,旨在增强用户对数列模式的理解。数列包括斐波那契数列、卢卡斯数列、佩尔数列、三角数列、平方数列和五边形数列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//第一章程序
// u01.cpp : 定义控制台应用程序的入口点。
//time:2019/4/28
//初始化六个数列
//六个数列分别为
//fibonacci:1,1,2,3,5,8,13,21 元素为前两个元素之和  
//lucas:1,3,4,7,11,18,29,47
//pell:1,2,5,12,29,70,169,408
//triangular:1,3,6,10,15,21,28,36
//square:1,4,9,16,25,36,49,64
//pentagonal:1,5,12,22,35,51,70,92
//给定数列的前两个元素,让用户猜测下一个元素
//总共有六个数列可以供用户猜测

#include "stdafx.h"
using namespace std;//命名空间 没有这一句会出现错误:cout是未命名的标识符

int main()
{

	//显示数列
	//fribonacci数列定义
	const int seq_size = 10;//定义一个整型常量 seq_size 数列大小为10
	int fribonacci[ seq_size ];//定义一个名为pell_seq,大小为seq_size的数组
	int ix;//数列索引 ix最大值为9
	fribonacci[0] = 1;  //初始化数列前两个元素
	fribonacci[1] = 1;

	//利用循环语句数组初始化
	for( ix=2; ix<seq_size; ++ix )//ix++ ++ix
		fribonacci[ix] = fribonacci[ix-1] + fribonacci[ix-2];
	
	//利用for循环显示数列 
	cout << "the elements of Fibonacci are:\n";
	for ( ix = 0; ix < seq_size; ++ix )//  for循环  显示数列的前10个元素
		 cout << fribonacci [ix] << ' ';
	cout << endl;
	

	//将数列类别记录下来
	const int max_seq = 6;
	string seq_names[max_seq] = {  //定义一个string数组 存储数列类别名称
   "fibonacci",
   "lucas",
   "pell",
   "triangular",
   "square",
   "pentagonal"
	};

	//创建一个数组array 名字为 elem_seq 存储类型为int  存储六个数列,每个数列的前三个元素
	const int elem_num = 18;//int型常数elem_num
	int elem_seq[elem_num] = {
	1,2,3,
	3,4,7,
	2,5,12,
	3,6,10,
	4,9,16,
	5,12,22
    };

	// 初始化变量
    bool go_for_it = true;//用户继续猜测
	bool got_it = false;//用户回答正确
	bool next_seq = true;//显示下一组数列
	char try_again;//用户是否想继续猜其他数列
	char user_rsp;//用户是否想再尝试(同一数列猜错了继续猜)
	int user_guess;//用户猜测的数字
	int cur_tuple = 0;//数列的索引
	int next_elem;//数组下一个元素	
	int num_right = 0;//用户猜对的次数
	int num_try = 0;//用户猜测的次数
	double user_score = 0.0; //用户回答的正确率得分

	cout << "start game\n";

	// 利用两层循环
	//外循环 用户是否猜测其他数列 不猜了退出循环 退出游戏
	//总共猜测六个数列 六个数列的前两个元素存在一个数列elem_seq里
	while ((next_seq == true)&&(cur_tuple < elem_num))//为用户显示数列
	{
		cout << "The first two elements of the sequence are:"//从数列elem_seq中取元素
		     << elem_seq[cur_tuple] <<","     //第一个元素
			 << elem_seq[cur_tuple+1] << '\n' //第二个元素
			 << "What is the next element?\n";  

		//内循环
		while ((got_it == false )&& (go_for_it == true))//用户猜错了但还要继续猜
		{
			cout << "now please input your guess:";
			cin >> user_guess;//输入用户的猜测
			num_try++; //猜测次数+1
			next_elem = elem_seq[cur_tuple+2];//当前数列的下一个元素next_elem(第三个数)当前索引cur_tuple+2
			if(user_guess == next_elem)//用户猜测是否正确
			{
				got_it = true;
				num_right++;//猜测正确次数+1
				cout << " very good .yes \n"
					 << elem_seq[cur_tuple+2]
				     <<" is the next element in the"
				   //  << seq_names[cur_tuple]
					 << "sequence.\n";
			}
			else//否则猜测错误
			{
				cout << "your guess is wrong.\n";//告诉用户猜测错误
				//根据尝试次数不同,输出对应的文字
				if(num_try == 1)
					cout << "Nice guess but not quite it.\n";
				else
					if(num_try == 2)
						cout << "sorry,wrong a second time.\n";
					else
						if(num_try == 3)
							cout << "this is harder than it looks,isn't it.\n";
						else 
							cout << "It must be getting pretty frustrating by now.\n";
			
			cout << "do you want to try again? (Y/N)";
		    cin >> user_rsp;//用户输入是否继续尝试
			if((got_it == false)&&(user_rsp == 'N'))//猜错了且不继续猜测
				go_for_it = false;//跳出内循环
			}
		}
		//内循环结束

		//用户已经答对的前提下
		cout << "Do you want another sequence? (Y/N)";
		cin >> try_again;//输入用户是否想再猜测其他数列
		if ((try_again == 'N')||(try_again == 'n'))//如果用户想退出游戏
		{
			next_seq = false;
			cout << "exit the game\n";
			cout << "You have tried " << num_try << " times\n";
			cout << "You have succeed "<< num_right << " times\n";
			user_score = num_right / num_try 
		}//退出外循环
		else 
			{
				cur_tuple += 3;
				got_it = false;//回到外循环的条件
				go_for_it = true;
		     }//如果继续猜数列,跳到下一个数列的第一个元素
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值