more.cpp

来自「包含C++中map」· C++ 代码 · 共 46 行

CPP
46
字号
#include<iostream>

using namespace std;

bool fibon_elem(int a,int &b)
{
	if(a<=0||a>1024)
	{
		b=0;
		return false;
	}
	b=1;
	int n_2=1,n_1=1;
	
	for(int ix=3;ix<=a;++ix)
	{
		b=n_2+n_1;
		n_2=n_1;
		n_1=b;
	}
	return true;
}

int main()
{
	int pos,elem;
	char ch;
	bool more=true;

	while(more)
	{
		cout << "Please enter a position : ";
		cin >> pos;

		if(fibon_elem(pos,elem))
			cout << " elements # "<< pos << " is "<< elem << endl;
		else
			cout << "Sorry! Couldn't calculate element # " << pos << endl;

		cout <<"Would you like to try again?(y/n) ";
		cin >> ch;
		if(ch != 'y' && ch != 'Y')
			more = false;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?