/*
1007. To and Fro 数组与下标(二维数组) 简单
题目大意:
给出一种加密方式,把一个字符串按列写成二维形式,再逐行从左到右或从右到左交替输出。给出加密后的字符串,还原本来的字符串。
解题思路:
按照解密规则把输入字符串写在二维数组上,再输出。
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
//重点1;思考方向错误,应该往二维数组方向思考,cols,rows 掌握数组从0算起,涉及到i和j的设计
int main(){
int cols;
int rows;
string nLetters;
while(1){
char a[201][201];
cin >> cols;
if(cols==0)
break;
cin >> nLetters;
rows = nLetters.length()/cols;
for(int i=0;i<rows;i++){
//错误1: if(rows/2==0) 变成定值
if(i%2==0){
for(int j=0;j<cols;j++){
a[i][j] = nLetters[i*cols+j]; //重点2:取数应该怎么取?i和j下标设计
}
}
else{
for(int j=0;j<cols;j++){
a[i][j] = nLetters[(i+1)*cols-1-j];
}
}
}
// 错误2:输出时i应为cols,j应为rows,同时数组下标应为a[j][i];
for(int i=0;i<cols;i++){
for(int j=0;j<rows;j++){
cout << a[j][i];
}
}
cout << endl;
}
return 0;
}
1007. To and Fro 数组与下标(二维数组) 简单
最新推荐文章于 2021-06-23 07:43:22 发布