TJU Sequence

After the final exam, Shawn started to play a computer game SW. In the game, Shawn needs to make his equipments. Just like the pattern of most of other games, SW’s equipment can be improved by stones, but different level stones have different ability. The 1st level stone can increase the property of equipment by 0.6, the 2nd is 0.7, and the kth level Lk can increase Lk-1×0.6+ Lk-2×0.5(k≥3). Now, he wants to know how much property the kth level Lk stone can increase.

Input

The first line of the input is an integer T(1≤T≤100) which indicates the sum of test case. Each of the following T lines contains an integer k(1≤k≤100).

Output

For every case output “Case i: Result” in a line where i is the case number and Result is the property the ith level stone can increase. Leave 2 digits after the decimal point.

Sample Input

3
1
4
7

Sample Output

Case 1: 0.60
Case 2: 0.78
Case 3: 0.95
 
 
题意概述:这个题就是一个数学题,题目也比较简单,就不多说了。Lk=Lk-1×0.6+ Lk-2×0.5(k≥3),输入一个k,输出对应的
Lk,其中第一项和第二项题目中已经给出。
 
解题思路:在编译阶段就把结果计算出来,存放在一个数组中,这样再输入k时,直接输出对应的Lk即可。起初我是想用递归求解的,但是递归的话反倒会花费很多的时间,因为每次输入一个数都要递归调用很多次,反倒会花费很多的时间,还不如只递归一次计算出所有的结果,反而会节省时间。
 
源代码:
#include<iostream> #include<iomanip>     //控制输出格式,如设置精度、设置长度等  using namespace std; int main() {     float result[100];  //存放结果的数组      result[0]=0.6;      //初始化第一个数      result[1]=0.7;     for(int i=2;i<100;++i)   //计算各个结果,把结果存放在数组中           result[i]=0.6*result[i-1]+0.5*result[i-2];     int T,temp;     cin>>T;     for(int i=1;i<=T;++i)     {          cin>>temp;          cout<<"Case "<<i<<": ";           cout<<setiosflags(ios::fixed)<<setprecision(2)<<result[temp-1]<<endl;  //控制输出格式      }     return 0; } 

你可能感兴趣的:(递归,源代码,ACM编程)