UVA10696 f91【数学函数+打表】(老师程序代码注释)

 

McCarthy is a famous theorician of computer science. In his work, he defined a recursive function, called f91, that takes as input a positive integer N and returns a positive integer defined as follows:

    • If N ≤ 100, then f91(N) = f91(f91(N + 11));

    • If N ≥ 101, then f91(N) = N − 10.

  Write a program, that computes McCarthy’s f91.

Input

The input tests will consist of a series of positive integers, each integer is at most 1,000,000. There will be at most 250,000 test cases. Each number is on a line on its own. The end of the input is reached when the number ‘0’ is met. The number ‘0’ shall not be considered as part of the test set.

Output

The program shall output each result on a line by its own, following the format given in the sample output.

Sample Input

500

91

0

Sample Output

f91(500) = 490

f91(91) = 91

 

 

问题链接UVA10696 f91

问题简述:(略)

问题分析

  这是一个数学函数计算问题,为了快打表是必要的。

程序说明:(略)
 

题记:(略)

 

参考链接https://blog.csdn.net/tigerisland45/article/details/79319615

AC的C++语言程序如下:

/* UVA10696 f91 */
 
#include <iostream>
 
using namespace std;
 
const int N = 1e6; //10的六次方 
int f91t[N + 1];
 
int f91(int n)//还是用递归计算,打表的计算只计算一次,避免重复递归(查表),只是减少递归计算的有效办法 
{
    if(n <= 100)
        return f91(f91(n + 11)); //函数计算(根据题意) 
    else // if(n >= 101)
        return n - 10;
}
 
void maketable()
{
    for(int i=1; i<=N; i++)
        f91t[i] = f91(i);
}
 
int main()
{
    maketable(); //先把表打出来 
 
    int n;
    while(~scanf("%d", &n) && n)
        printf("f91(%d) = %d\n", n, f91t[n]);//根据表中的数据,查表 
 
    return 0;
}

注意打表、查表可以减少递归运算的次数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值