2016年2月28日 星期日

UVA c002: 10696 - f91

內容 :
McCarthy是一個有名的資訊專家。他定義了一個遞迴的函數叫做 f91 。它輸入一個正整數N並且依據以下的規則傳回一個正整數:
. 如果 N <= 100, 那麼 f91(N) = f91( f91( N+11) )
. 如果 N >= 101, 那麼 f91(N) = N-10
請你寫一個程式來計算 f91
輸入說明 : 
每組測試資料一列。含有1個正整數 N( N <= 1000000)。輸入最多有250000組測試資料。 若 N=0 代表輸入結束。
輸出說明 : 
對每組測試資料輸出一列 f91(N),。輸出格式請參考Sample Output。
範例輸入 : help
500
91
0
範例輸出:
f91(500) = 490
f91(91) = 91
提示 : 
* 中文翻譯:Lucky 貓  
標籤:
出處: 
UVa10696 (管理:)

import java.util.Scanner;

public class UVAc002 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
if(n==0) break;
System.out.println("f91("+n+") = "+f91(n));
}
}
static private int f91(int n){
if(n<=100){
return f91(f91(n+11));
}
else{
return n-10;
}
}

}

沒有留言:

張貼留言