天天看点

转载:http://bbs1.chinaunix.net/thread-1640178-1-1.html

转载:http://bbs1.chinaunix.net/thread-1640178-1-1.html

有一个人站在电影院门口卖票,票价50,一开始手上没有找零的钱,

现在有两种人来买票,A拿着100元的钱,人数为m(m<20),B拿着50元的钱,

人数为n(n<20)。卖票的人必须用从B类人中那里得来钱找给A,所以卖票

的顺序是有限制的。

要求写一个程序打印出所有的买票序列:

例如:m =2 ,n = 3;

BABAB

BBAAB

BBBAA

BBABA

#################################

1. #include <stdio.h>

2. char str[500];

3. int n, m;

4. void oper(int a, int b,int m, int n)

5. {

6. if (a + b == m + n)

7. printf("%s\n", str);

8. else {

9. if (a < m && a < b) {

10. str[a + b] = 'A';

11. oper(a + 1, b, m, n);

12. }

13. if (b < n) {

14. str[a + b] = 'B';

15. oper(a, b + 1, m, n);

16. }

17. }

18. }

19. int main()

20. {

21. while (scanf("%d%d", &m, &n) == 2)

22. {

23. if (n < 0 || m < 0 || m > n)

24. printf("No result!\n");

25. else {

26. str[m + n] = 0;

27. oper(0, 0, m, n);

28. }

29. }

30. return 0;

31. }

##########实际上求组合############,再判断条件。

#include <stdio.h>

char str[500];

int n, m;

void oper(int a, int b,int m, int n)

{

if (a + b == m + n)

printf("%s\n", str);

if (a < m){

str[a + b] = 'A';

oper(a + 1, b, m, n);

}

if (b < n){

str[a + b] = 'B';

oper(a, b + 1, m, n);

}

}

int main()

{

while (scanf("%d%d", &m, &n) == 2)

{

if (n < 0 || m < 0 || m > n)

printf("No result!\n");

else {

str[m + n] = 0;

oper(0, 0, m, n);

}

}

return 0;

}