#UVa:10696-f91

灆洢 2011-12-17 11:56:20

從題目即可看出f91(N)=N-10,當N>=101。那麼當N<=100真的要下去遞迴看看嗎?首先可以先手算出當90<=N<=100:

f91(90) = f91(f91(101))
= f91(91) = f91(f91(102))
= f91(92) = f91(f91(103))
= f91(93) = f91(f91(104))
= ......
= f91(99) = f91(f91(105))
= f91(100) = f91(f91(111))
= f91(101) = 91

剩下的數值即可利用下列式子證明出1<=N<90都會等於91:

  f91(N) = f91(f91(N+11))
= ... = f91(f91(f91(...f91(a)))) // 90 <= a <= 100
= f91(f91(f91(...f91(91))))
= 91

C++(0.072)

/*******************************************************/
/* UVa 10696 f91                                       */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2011/12/17                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  int N;
  while( scanf( "%d", &N ) != EOF && N ){
    if( N <= 100 )
      printf( "f91(%d) = 91\n", N );
    else
      printf( "f91(%d) = %d\n", N, N-10 );
  }
  return 0;
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料