C/C++

Помогите написать код для задачи на с++

SK
Sergei Kovalev
283
#include <iostream>
#include <iomanip>
using namespace std;
unsigned long long fib(int n) {
const auto m = 1000;
static unsigned long long a[m];
if (0 == n || 1 == n) return n;
if (a[n] > 0) return a[n];
return a[n] = fib(n - 1) + fib(n - 2);
}
unsigned integer(const char* msg) {
cout << msg;
unsigned value;
cin >> value;
cin.ignore(cin.rdbuf()->in_avail());
return value;
}
int main() {
auto a = integer("a = ");
auto b = integer("b = ");
auto x = 0U;
auto k = 0U;
auto n = fib(x);
while (n <= b) {
if (n >= a) cout << setw(4) << ++k << ": \"" << n << "\"\n";
n = fib(++x);
}
system("pause > nul");
}
Геннадий Кашкан
Геннадий Кашкан
77 818
Лучший ответ
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
const char filename[] = "a.txt";
ofstream oa;
unsigned a,b;
do
{
cout << "Введите a: ";
cin >> a;
} while(!a);
do
{
cout << "Введите b: ";
cin >> b;
} while(a >= b);
int x = 1, y = 1, z;
int find = 0;
oa.open(filename);
if(a == 1)
{
oa << "1: \"1\"" << endl;
oa << "2: \"1\"" << endl;
find += 2;
}
else if(a == 2)
{
oa << "1: \"1\"" << endl;
find ++;
}
z = x + y;
for(int u = 3; z <= b; u++)
{
x = y;
y = z;
if( a <= z )
{
find++;
oa << find << ": \"" << z << '\"' << endl;
}
z = x + y;
}
oa.close();

return 0;
}
Мозг О-Еб
Мозг О-Еб
84 375