蓝桥杯试题 算法训练 s01串

试题 算法训练 s01串

资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
  s01串初始为"0"
  按以下方式变换
  0变1,1变01
输入格式
  1个整数(0~19)
输出格式
  n次变换后s01串
样例输入
3
样例输出
101
数据规模和约定
  0~19

PS:S01串也就是f(n) = f(n - 2) + f(n - 1)这个简单递归
开始一直不明白这个题目是什么意思,后来看了不少博客才知道是递归

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner sc=new Scanner(System.in);
		int n = sc.nextInt();
		System.out.println(sum(n));
	}
	public static String sum(int num) {
		if(num == 0) {
            return "0";
        }else if(num == 1) {
            return "1";
        }else {
            return sum(num - 2) + sum(num - 1);
        }
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_44517477/article/details/105464848