TOJ 1257 感受水題的力量

來源:互聯網
上載者:User

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

500910

Sample Output

f91(500) = 490f91(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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.