遞歸的函數(shù) Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic PRoblem Description
給定一個(gè)函數(shù) 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)。 看起來簡(jiǎn)單的一個(gè)函數(shù)?你能做對(duì)嗎?
Input
輸入包含多組測(cè)試數(shù)據(jù),對(duì)于每組測(cè)試數(shù)據(jù): 輸入只有一行為 3 個(gè)整數(shù)a, b, c(a, b, c < 30)。
Output
對(duì)于每組測(cè)試數(shù)據(jù),輸出函數(shù)的計(jì)算結(jié)果。
Example Input
1 1 1
2 2 2
Example Output
2
4
#include <bits/stdc++.h>using namespace std;int x[31][31][31]={0};int f( int a, int b, int c){ if(a<=0||b<=0||c<=0) return 1; else if(a>20||b>20||c>20) return f(20,20,20); else if (x[a][b][c]) return x[a][b][c]; else if(a<b&&b<c) return x[a][b][c]=f(a,b,c-1)+f(a,b-1,c-1)-f(a,b-1,c-1); else return x[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);}int main(){ int a,b,c; while (cin>>a>>b>>c) { cout<<f(a,b,c)<<endl; } return 0;}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注