s01串

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; }
相關文章
相關標籤/搜索