TOJ 1257 感受水题的力量

本文介绍了一个由著名计算机科学家麦卡锡定义的递归函数f91,并提供了一种有效的方法来计算该函数的值。对于输入N≤100的情况,通过递归调用f91(N+11),而对于N≥101,则直接返回N-10。

Background

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.

The Problem

Write a program, that computes McCarthy's f91.

The 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

Hint

Huge input and output, scanf( ) and printf( ) are recommended.

这道题的力量实在太大,开始不管是递归还是计算都WA。。。

结果发现输出很bug,不按常理出题而已

开始的想法是101-10=91 然后再89 90 91中找临界。后来想到了mod11的问题就简单了。

但是一直WA后,就暴力的用递归然后穷尽看。

然后发现。。。

#include<stdlib.h>
#include<stdio.h>
int main(){
    int n = 0;
    while(scanf("%d",&n)){
        if(n==0)
            break;
        if(n>101){
           printf("f91(%d) = %d\n",n,n-10);
           continue;
        }        
        else
        {          
           printf("f91(%d) = %d\n",n,91);
           continue;
        }
       
    }
    system("pause");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值