征战蓝桥 —— 2017年第八届 —— C/C++A组第5题——字母组串
生活随笔
收集整理的這篇文章主要介紹了
征战蓝桥 —— 2017年第八届 —— C/C++A组第5题——字母组串
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
由 A,B,C 這3個字母就可以組成許多串。
比如:“A”,“AB”,“ABC”,“ABA”,“AACBB” …
現在,小明正在思考一個問題:
如果每個字母的個數有限定,能組成多少個已知長度的串呢?
他請好朋友來幫忙,很快得到了代碼,
解決方案超級簡單,然而最重要的部分卻語焉不詳。
請仔細分析源碼,填寫劃線部分缺少的內容。
#include <stdio.h>// a個A,b個B,c個C 字母,能組成多少個不同的長度為n的串。 int f(int a, int b, int c, int n) {if(a<0 || b<0 || c<0) return 0;if(n==0) return 1;return ______________________________________ ; // 填空 }int main() {printf("%d\n", f(1,1,1,2));printf("%d\n", f(1,2,3,3));return 0; }對于上面的測試數據,小明口算的結果應該是:
6
19
注意:只填寫劃線部分缺少的代碼,不要提交任何多余內容或說明性文字。
代碼
#include <stdio.h>// a個A,b個B,c個C 字母,能組成多少個不同的長度為n的串。 int f(int a, int b, int c, int n) {if (a < 0 || b < 0 || c < 0) return 0;if (n == 0) return 1;return f(a - 1, b, c, n - 1) + f(a, b - 1, c, n - 1) + f(a, b, c - 1, n - 1); // 填空 }int main() {printf("%d\n", f(1, 1, 1, 2));printf("%d\n", f(1, 2, 3, 3));return 0; }總結
以上是生活随笔為你收集整理的征战蓝桥 —— 2017年第八届 —— C/C++A组第5题——字母组串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 征战蓝桥 —— 2017年第八届 ——
- 下一篇: 2017/Province_Java_B