-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzag.cpp
More file actions
41 lines (38 loc) · 910 Bytes
/
zigzag.cpp
File metadata and controls
41 lines (38 loc) · 910 Bytes
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
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std;
int main(){
int i, j=0, k, n;
string s,ss;
getline(cin,s);
for(i=s.size()-1;i>=0;i--){
if(s[i]==' '){
ss = s.substr(i,s.size()-i+1);
n = atoi(ss.c_str());
ss = s.substr(0,i);
//cout<<"str: "<<ss<<endl;
break;
}
}
if(n == 1){cout<<ss; return 0;}
//cout<<"num: "<<n<<endl;
vector<string> sv(min(int(ss.size()), n));
bool change_dir = false;
for(char c: ss){
sv[j]+=c;
if(j == 0 || j == n-1){
change_dir = !change_dir;
}
j += change_dir?1:-1;
//cout<<"c: "<<c<<" j: "<<j<<endl;
}
//cout<<sv[0]<<endl;
for(string r:sv){
cout<<r;
}
cout<<endl;
return 0;
}