题解 | #s01串#
s01串
https://ac.nowcoder.com/acm/problem/14356
题目
s01串初始为"0" 按以下方式变换 0变1,1变01
输入描述:
1个整数(0~19)
输出描述:
n次变换后s01串
分析
递归思想: 1,找大问题:把一个s01串变换n次
2.找到小问题:把一个s01串变换
3.找到重复问题:把每一位都对应变换
AC代码:
public class Main{
//递归方法
public static String change(String s,int n){
//while控制外层循环
while(n > 0){
String pro = "";
for(int i = 0; i < s.length(); i++){
//用charAt()方法确定是0还是1,对应转换
//借助pro来存放转换后的字符串
if(s.charAt(i) == '0'){
pro += "1";
}
else {
pro += "01" ;
}
}
//n--,和新的字符串递归要放在for循坏外面
n--;
return change(pro,n);
}
//当n=0 时,要返回最终结果s,所以要在while循坏外
return s;
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n =scan.nextInt();
String s = "0";
System.out.println(change(s,n));
}
}