SDUT-2176 遞迴的函數(JAVA*)__LINUX

來源:互聯網
上載者:User
遞迴的函數 Time Limit: 1000 ms  Memory Limit: 65536 KiB Submit  Statistic Problem Description 給定一個函數 f(a, b, c): 如果 a ≤ 0 或 b ≤ 0 或 c ≤ 0 傳回值為 1; 如果 a > 20 或 b > 20 或 c > 20 傳回值為 f(20, 20, 20); 如果 a < b 並且 b < c 返回 f(a, b, c−1) + f(a, b−1, c−1) − f(a, b−1, c); 其它情況返回 f(a−1, b, c) + f(a−1, b−1, c) + f(a−1, b, c−1) − f(a-1, b-1, c-1)。 看起來簡單的一個函數。你能做對嗎。 Input 輸入包含多組測試資料,對於每組測試資料: 輸入只有一行為 3 個整數a, b, c(a, b, c < 30)。 Output 對於每組測試資料,輸出函數的計算結果。 Sample Input
1 1 12 2 2
Sample Output
24
Hint   Source

qinchuan

import java.util.*;public class Main {static int s[][][]=new int[33][33][33];static int f(int a,int b,int c){if(a<=0||b<=0||c<=0)return 1;//因為題目中描述的判定條件有交叉的部分,所以要提前returnif(a>20||b>20||c>20)return s[a][b][c]=f(20,20,20);if(s[a][b][c]!=0)return s[a][b][c];if(a<b&&b<c)return s[a][b][c]=f(a,b,c-1)+f(a,b-1,c-1)-f(a,b-1,c);elsereturn s[a][b][c]= f(a-1,b,c) + f(a-1,b-1,c) + f(a-1,b,c-1)- f(a-1,b-1,c-1);}public static void main(String[] args) {Scanner cin = new Scanner(System.in);while(cin.hasNext()){int a=cin.nextInt();int b=cin.nextInt();int c=cin.nextInt();int ans = f(a,b,c);System.out.println(ans);}cin.close();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.