题目:http://acm.hdu.edu.cn/showproblem.php?pid=1200
思路分析:
输入一个数n,再输入字符串s,你要把字符串s分成n列,这么分呢?每一行有n个数,奇数行顺输出,偶数行逆输出,再以列为单位,依次输出。
输入数n: 5 输入字符串s: toioynnkpheleaigshareconhtomesnlewx将字符串分成n列:t o i o y h p k n n e l e a i r a h s g e c o n h s e m o t n l e w x输出: theresnoplacelikehomeonasnowynightx
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
char a[100][100];
int i,j,n;
while(cin>>n && n!=0)
{
cin>>s;
int m=0;
for(i=0;i<s.length()/n;i++)
{
if(i%2==0)
{
for(j=0;j<n;j++)
{
// a[i][j]=s[j+i*n];
a[i][j]=s[m++];
}
}
else
{
for(j=n-1;j>=0;j--)
{
// a[i][j]=s[j+i*n];
a[i][j]=s[m++];
}
}
}
for(j=0;j<n;j++)
{
for(i=0;i<s.length()/n;i++)
{
cout<<a[i][j];
}
}
cout<<endl;
}
return 0;
}