#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std
;
int main()
{
cout
<< "请输入一个区间:" << endl
;
int x
, y
;
long sum
= 0;
cin
>> x
>> y
;
while (x
< 0 || y
< 0 || x
>= y
) {
cout
<< "区间输入错误!请重新输入:\n" << endl
;
cin
>> x
>> y
;
}
if (x
== 0 && y
== 1) {
cout
<< x
<< " " << y
<< endl
;
return 0;
}
int* array
= new int[y
];
array
[0] = 0;
array
[1] = 1;
cout
<< "输出数列: " ;
if (x
== 1){
cout
<< x
<< " ";
sum
+= x
;
}
for (int i
= 2; i
<= y
; i
++) {
array
[i
] = array
[i
- 1] + array
[i
- 2];
if (i
>= x
) {
cout
<< array
[i
] << " ";
sum
+= array
[i
];
}
}
cout
<< "\n输出数列和:" << sum
<< endl
;
cout
<< "\n-----------------------------------" << endl
;
system("pause");
return EXIT_SUCCESS
;
}
测试如下:
转载请注明原文地址: https://lol.8miu.com/read-11963.html