Skip to content

Instantly share code, notes, and snippets.

@completejavascript
Created September 15, 2018 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save completejavascript/b265bbf83ab14883a7a2d52ac10f3008 to your computer and use it in GitHub Desktop.
Save completejavascript/b265bbf83ab14883a7a2d52ac10f3008 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int main()
{
//freopen("input.txt","r",stdin);
int NUM_COL = 0;
while(true)
{
// Nhập số lượng cột
cin >> NUM_COL;
if(NUM_COL == 0) break;
// Xâu đầu vào
char str[201];
cin >> str;
// Tính độ dài của xâu
int length = 0;
while(str[length] != '\0') length++;
// Lưu ma trận
char Mat[101][21];
// Lưu toạ độ hàng và cột đang đứng
int col = 0, row = 0;
// biến thiên chỉ số cột: = 1 nếu đi từ trái sang phải
// ngược lại = -1
int delta = 1;
// Duyệt xâu để chuyển xâu thành ma trận
for(int i = 0; i < length; i++)
{
// Ban đầu là đi từ trái sang phải => chỉ số cột tăng
Mat[row][col] = str[i];
col += delta;
// Khi đi đến cuối thì quay về trái
if(col >= NUM_COL)
{
Mat[row][col] = '\0';
row++;
Mat[row][col] = '\0';
delta = -1;
col += delta;
}
else if (col < 0) // Khi đi đến đầu thì quay sang bên phải
{
row++;
delta = 1;
col += delta;
}
}
// Tìm ra kết quả
int count = 0;
for(int j = 0; j < NUM_COL; j++)
{
for(int i = 0; i < row; i++)
str[count++] = Mat[i][j];
}
str[count] = '\0';
cout << str << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment