算法训练 s01串

问题描述
  s01串初始为"0"
  按以下方式变换
  0变1,1变01
输入格式
  1个整数(0~19)
输出格式
  n次变换后s01串
样例输入
3 
样例输出
101
数据规模和约定
0~19

C测试代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6     int n, i, j;
 7     scanf("%d", &n);
 8     char str[20000] = "0", s[20000];
 9     while (n--)
10     {
11         for (i = 0, j = 0; str[i]; i++, j++)
12         {
13             if (str[i] == '0')
14             {
15                 s[j] = '1';
16             }
17             else if (str[i] == '1')
18             {
19                 s[j] = '0';
20                 s[++j] = str[i];
21             }            
22         }
23         s[j] = '\0';
24         strcpy(str, s);
25     }
26     puts(str);
27     return 0;
28 }

Java测试代码

import java.util.Scanner;

public class Main {

    public static String getS(char c) {
        if (c == '0') {
            return "1";
        } else {
            return "01";
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();
        String s1 = "0";
        String s2 = "";
        if (n != 0) {
            for (int i = 0; i < n; i++) {
                s2 = "";
                int len = s1.length();
                for (int j = 0; j < len; j++) {
                    s2 = s2.concat(getS(s1.charAt(j)));
                }
                s1 = s2;
            }
            System.out.println(s1);
        } else {
            System.out.println("0");
        }
    }
}

 

 

posted @ 2016-05-20 20:13  新生代黑马  阅读(581)  评论(0编辑  收藏  举报