10696 - f91
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.
McCarthy是一个有名的资讯专家。他定义了一个递回的函数叫做 f91 。它输入一个正整数N并且依据以下的规则传回一个正整数:
. 如果N <= 100, 那么f91(N) = f91( f91( N+11) )
. 如果 N >= 101, 那么 f91(N) = N-10
请你写一个程式来计算 f91
Input
每组测试资料一列。含有1个正整数 N( N <= 1000000)。输入最多有250000组测试资料。
若 N=0 代表输入结束。
Output
对每组测试资料输出一列 f91(N),。输出格式请参考Sample Output。
Sample input
500 91 0
Sample output
f91(500) = 490 f91(91) = 91
解题思路:寻找到规律只要小于100经过题目给的公式计算后得到的数始终是91
#include<stdio.h> int main() {int n; while(scanf("%d",&n)!=EOF){if(n==0)break; if(n<=100) printf("f91(%d) = 91\n",n); else printf("f91(%d) = %d\n",n,n-10); } return 0; }