POJ 2039 && HDU 1200 To and Fro(水~)

来源:互联网 发布:软件质量管理体系文件 编辑:程序博客网 时间:2024/05/18 16:15

Description
给出2<=N<=20和加密过的字符串(长度<=200),输出解密后的字符串。
加密规则:例如theresnoplacelikehomeonasnowynightx,按列写出,共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
再按照奇数行,左至右,偶数行,右到左的顺序按行写出即可
Input
多组用例,每组用例第一行为字符串每行字符数n,第二行为字符串,以n=0结束输入
Output
对于每组用例,按题意描述输出
Sample Input
5
toioynnkpheleaigshareconhtomesnlewx
3
ttyohhieneesiaabss
0
Sample Output
theresnoplacelikehomeonasnowynightx
thisistheeasyoneab
Solution
简单字符串处理
Code

#include<stdio.h>#include<string.h>int main(){    int n;    char map[250][250];    while(scanf("%d",&n),n)    {        getchar();        char c[250];        gets(c);        int len=strlen(c);        int m=len/n;        int k=0;        for(int i=0;i<m;i++)        {            if(i%2==0)                for(int j=0;j<n;j++)                    map[i][j]=c[k++];            else if(i%2)                for(int j=n-1;j>=0;j--)                    map[i][j]=c[k++];        }        for(int i=0;i<n;i++)            for(int j=0;j<m;j++)                printf("%c",map[j][i]);        printf("\n");    }    return 0;} 
0 0
原创粉丝点击