资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
s01串初始为"0" 按以下方式变换 0变1,1变01
输入格式
1个整数(0~19)
输出格式
n次变换后s01串
样例输入
3
样例输出
101
数据规模和约定
0~19
import java.util.*;
public class s01串 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String x = "0"; //开局一个0,结果全靠编
//循环次数为n次
for (int i = 0; i < n; i++) {
//循环调用
x = s(x);
}
System.out.println(x);
}
static String s(String x) {
//创建一个stringbuilder来装每次循环的结果
StringBuilder y = new StringBuilder();
//遍历字符串x,根据题意判断
for (int j = 0; j < x.length(); j++) {
char z = x.charAt(j);
if (z == '0') {
y.append("1");
} else if (z == '1') {
y.append("01");
}
}
//转为字符串,并返回
x = y.toString();
return x;
}
}