杭电1129 Do the Untwist

来源:互联网 发布:五星大饭店知乎 编辑:程序博客网 时间:2024/05/16 00:48
杭电1129 Do the Untwist
      杭电1129 题目很长,读了下来,其实意思就是把输入的字符串按规则转化为数组c中的元素,让你根据公式c[i] = (p[k*i mod n] - i) mod 28得到p中元素的值,我们可以把这个公式化简一下,就可得到对应的公式 c[i]=(p[k*i mod n])mod28-i mod28;所以p[k*i mod n]=(c[i] + i mod 28)mod 28;根据这个公式的到p数组中元素的值,再一次把数组转化为相应的字符输出就可以了。
Ac代码
#include<iostream>using namespace std;int main(){    int p[500], c[500];    char str[500];    int n;    while (cin>>n)    {        memset(str, 0, sizeof(str));        if (n == 0)            break;        cin >> str;        int len = strlen(str);        for (int i = 0; i < len; i++)        {            if (str[i] == '_')            {                c[i] = 0;            }            else if (str[i] == '.')            {                c[i] = 27;            }            else            {                c[i] = str[i] - 'a' + 1;            }        }        for (int i = 0; i < len; i++)        {            p[n*i%len] = (c[i] + i % 28)%28;        }        for (int i = 0; i < len; i++)        {            if (p[i] == 0)            {                str[i] = '_';            }            else if (p[i] == 27)            {                str[i] = '.';            }            else            {                str[i] = p[i] - 1 + 'a';            }        }        str[len] = '\0';        cout << str << endl;    }return 0;}


0 0
原创粉丝点击