地址:https://leetcode-cn.com/problems/zigzag-conversion/
思路:直接分析一下结构就能够分析规律
Code:
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<unordered_map>
using namespace std;
typedef long long LL;
class Solution {
public:
string convert(string s, int numRows) {
if(numRows==1) return s;
string res="";
int len=s.length(),k=numRows*2-2;
for(int i=0;i<len;i+=k)
res+=s[i];
for(int i=1;i<numRows-1;++i)
for(int j=i,p=k;j<len;p+=k*2,j+=k)
{
res+=s[j];
if(p-j<len) res+=s[p-j];
}
for(int i=numRows-1;i<len;i+=k)
res+=s[i];
return res;
}
};
int main()
{
int m;
string str;
Solution So;
cin>>str>>m;
cout<<So.convert(str,m)<<endl;
return 0;
}