poj 2039 To and Fro【字符数组操作】
这道题同样没有太多好说的,总之要细心就好了!
AC的代码:
#include <stdio.h> #include <string.h> //先输入到一个字符数组之后在进行处理 char letters[22][202];//第一行代表列 char myInput[202]; void testPrint(int n) { //测试输出 int i,j; for(j=0;j<(int)strlen(myInput)/n;j++) { for(i=0;i<n;i++) { printf("%c ",letters[i][j]); } printf("\n"); } } void process(int N) { int row; //行数 int i=0; int j; while(i<(int)strlen(myInput)) { //先判断行数奇偶,确定输入方向 row=i/N; if (row%2==0) { //正向输入 for(j=0;j<N;j++) { letters[j][row]=myInput[i]; i++; } } else { //反向输入 for(j=N-1;j>=0;j--) { letters[j][row]=myInput[i]; i++; } } } //转换完毕,测试输出 //testPrint(N); //ok } void Print(int n) { int i,j; for(i=0;i<n;i++) { for(j=0;j<(int)strlen(myInput)/n;j++) { printf("%c",letters[i][j]); } } printf("\n"); } int main() { int N;//代表输入的字符数组排成多少列 while(~scanf("%d",&N) && N!=0) { scanf("%s",myInput); //将输入的数组变成二维字符数组 process(N); //正式输出 Print(N); } return 0; }
最后更新:2017-04-03 05:39:37