天天看點

【藍橋杯ALGO-139】s01串 Java版

試題 算法訓練 s01串

資源限制

時間限制:1.0s 記憶體限制:256.0MB

問題描述

  s01串初始為"0"

  按以下方式變換

  0變1,1變01

輸入格式

  1個整數(0~19)

輸出格式

  n次變換後s01串

樣例輸入

3

樣例輸出

101

資料規模和約定

  0~19

Java 代碼:

import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(in.readLine());
        String str = fun(n);
        System.out.print(str);
    }

    public static String fun(int n) {
        if (n == 1) {
            return "1";
        } else if (n == 0) {
            return "0";
        }
        String str = fun(n - 1);
        char[] ch = str.toCharArray();
        String res = "";
        for (char s : ch) {
            if (s == '0') {
                res += "1";
            } else if (s == '1') {
                res += "01";
            }
        }
        return res;
    }
}