如果我使用递归方法将卡片放在两侧并使用c ans s n次,我如何打印所有可能的方法? [英] How do i print all possible ways if i drop card have two sides with c ans s n times using recursion method?

查看:86
本文介绍了如果我使用递归方法将卡片放在两侧并使用c ans s n次,我如何打印所有可能的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用递归方法使用C ans S n次使用两个sidws,我如何打印所有可能的方法?



例如你放弃它4在给定的会话中,所有可能的丢弃方式如下:



CCCC CCCS CCSC CCSS CSCC CSCS CSSC CSSS SCCC SCCS SCSC SCSS SSCC SSCS SSSC SSSS



我的尝试:



如果打印所有可能的方式如果我放弃卡有两个s with与C ans S n次?



例如你在给定的会话中放弃它4次,所有可能的放弃它的方法都是如下:



CCCC CCCS CCSC CCSS CSCC CSCS CSSC CSSS SCCC SCCS SCSC SCSS SSCC SSCS SSSC SSSS

How do I print all possible ways if I drop card have two sidws with C ans S n times using recursion method ?

For example of you drop it 4 times in a given session, all possible ways to drop it are as follows:

CCCC CCCS CCSC CCSS CSCC CSCS CSSC CSSS SCCC SCCS SCSC SCSS SSCC SSCS SSSC SSSS

What I have tried:

How do I print all possible ways if I drop card have two sidws with C ans S n times?

For example of you drop it 4 times in a given session, all possible ways to drop it are as follows:

CCCC CCCS CCSC CCSS CSCC CSCS CSSC CSSS SCCC SCCS SCSC SCSS SSCC SSCS SSSC SSSS

推荐答案

很简单:例如,你的步骤函数可以获取当前构建的字符串和递归部分作为参数:

It is simple: your step function could, for instance, get the currently built string and the recursion dept as paramenters:
public static void step(String s, int n)



n == 0 会停止条件,该函数应该只打印字符串和退出。

n> 0 是一个构建步骤,该函数应该自己调用两次,首先是用<$ c扩充的字符串$ c>C(和 n-1 ),然后使用S增加字符串(以及 n-1 )。

实施细节留待练习。


n==0 would the stop condition, the function should then just print the string and exit.
n>0 is a building-step, the function should call itself twice, first withe the string augmented with "C" (and n-1), then with the string augmented with "S" (and n-1).
Implementation details are left as exercise.


这篇关于如果我使用递归方法将卡片放在两侧并使用c ans s n次,我如何打印所有可能的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆