-
Notifications
You must be signed in to change notification settings - Fork 0
/
zigzeg.cpp
41 lines (39 loc) · 841 Bytes
/
zigzeg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// zigzeg.cpp
//
//
// Created by Ally Wang on 12/17/14.
//
//
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string convert(string s, int nRows) {
//The cases no need to do anything
if (nRows<=1 || nRows>=s.size()) return s;
vector<string> r(nRows);
int row = 0;
int step = 1;
for(int i=0; i<s.size(); i ++) {
if (row == nRows-1) step = -1;
if (row == 0) step = 1;
//cout << row <<endl;
r[row] += s[i];
row += step;
}
string result;
for (int i=0; i<nRows; i++){
result += r[i];
}
return result;
}
int main(int argc, char**argv){
string s;
int r;
s = "abcdefghijklmn";
r = 3;
cout << s << " : " << convert(s, 4) << endl;
cout << s << " : " << convert(s, 5) <<endl;
}