http://www.dotcpp.com/oj/problem1600.htmlhtml
题目描述:ios
s01串初始为" 0"
按如下方式变换
0变1,1变01
数据规模和约定
0~19 c++
输入:tcp
1个整数(0-19)spa
输出:code
n次变换后s01串htm
样例输入:blog
3ci
样例输出:string
101
解题思路:
咱们能够定义两个串str1 str2,一个(str1)用来做为每次遍历用的,一个(str2)做为对每一位的每一次处理以后获得的串进行暂时存储;原来串(str1)遍历完以前,都把每次遍历结果都存储到第二个串(str2)中(注意是链接存储,不是覆盖);最后把获得的新串(str2)又赋值给str1;让str1进行下一次处理;知道处理n次以后结束;n就是输入的n(表示处理几回)
// // main.cpp // c++prime // // Created by SJCHEN on 2019/1/19. // Copyright © 2019 SJCHEN. All rights reserved. // #include <iostream> #include <algorithm> #include <cstring> using namespace std; int main() { string str1 = "0", str2; int n; cin >> n; if (n == 0) { cout << "0" << endl; return 0; } while (n--) { str2 = ""; for (int i = 0; i < str1.size(); i++) { if (str1[i] == '0') { str2 += "1"; } else { str2 += "01"; } } str1 = str2; } cout << str1 << endl; return 0; }