百練 / 2016電腦學科夏令營上機考試: C

來源:互聯網
上載者:User

題目來源:http://noi.openjudge.cn/ch0108/20/ 20:反反覆複

總時間限制: 1000ms      記憶體限制: 65536kB

描述

Mo和Larry發明了一種資訊加密方法。他們首先決定好列數,然後將資訊(只包含字母)從上往下依次填入各列,並在末尾補充一些隨機字母使其成為一個完整的字母矩陣。例如,若資訊是“There's no placelike home on a snowy night”並且有5列,Mo會寫成:

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

注意Mo只會填入字母,且全部是小寫形式。在這個例子中,Mo用字母“x”填充了資訊使之成為一個完整的矩陣,當然他使用任何字母都是可以的。

Mo根據這個矩陣重寫資訊:首先從左至右寫下第一行,然後從右至左寫下第二行,再從左至右寫下第三行……以此左右交替地從上到下寫下各行字母,形成新的字串。這樣,例子中的資訊就被加密為:toioynnkpheleaigshareconhtomesnlewx。

你的工作是協助Larry從加密後的資訊中還原出原始資訊(包括填充的字母)。

輸入

第一行包含一個整數(範圍2到20),表示使用的列數。
第二行是一個長度不超過200的字串。

輸出

一行,即原始資訊。

範例輸入

5

toioynnkpheleaigshareconhtomesnlewx

範例輸出

theresnoplacelikehomeonasnowynightx

-----------------------------------------------------

解題思路

找到行列轉換的規律即可

-----------------------------------------------------

代碼

#include<iostream>#include<fstream>#include<string>using namespace std;int main(){    #ifndef ONLINE_JUDGE    ifstream fin("xly2016C.txt");    int n = 0;    fin >> n;    string src;    fin >> src;    int len = src.size();    int row = len/n;    int i = 0, j = 0, ind = 0;    for (i=0; i<n; i++)    {        for (j=0; j<row; j++)        {            if (j%2==0)            {                ind = (j/2)*2*n + i;                cout << src.at(ind);            }            else            {                ind = (j/2+1)*2*n - i-1;                cout << src.at(ind);            }        }    }    fin.close();    return 0;    #endif // LOCAL FILE    #ifdef ONLINE_JUDGE    int n = 0;    cin >> n;    string src;    cin >> src;    int len = src.size();    int row = len/n;    int i = 0, j = 0, ind = 0;    for (i=0; i<n; i++)    {        for (j=0; j<row; j++)        {            if (j%2==0)            {                ind = (j/2)*2*n + i;                cout << src.at(ind);            }            else            {                ind = (j/2+1)*2*n - i-1;                cout << src.at(ind);            }        }    }    return 0;    #endif // ONLINE_JUDGE}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.