J - Progressive Scramble(字符串模拟) VJ补题

J - Progressive Scramble

在这里插入图片描述

Input

The input to your program consists of a single integer 1≤n≤100
on its own line. This number is followed by n lines, each containing the letter ‘e’ or ‘d’, a single space, and then a message made up of lowercase letters (a–z) and spaces, continuing to the end of the line. Each message is between 1 and 80 characters long. The letters ‘d’ and ‘e’ indicate that your program decrypts or encrypts the subsequent string, respectively.

Output

Output the result of encrypting or decrypting each message from the input on its own separate line. Note that differences in whitespace are significant in this problem. Therefore your output must match the correct output character-for-character, including spaces.

Sample Input 1
7
e testing multiple letters rrrrrrrrrrrrr
e this particularly long sentence can test encryption
d tajbbrsjcloiuvmywwhwjqqqinauzmpuuxyllejbvv nqhfvoxlz
e my pie
d mkk in
e the quick brown fox jumps over the lazy dog
d taffwqzbmmofuqddjyvvezlatthchzzs eeqrqoosgn
Sample Output 1
tyqjsfmmzteygwhmmycwpulddvmdvmdvmdvmdv
tajbbrsjcloiuvmywwhwjqqqinauzmpuuxyllejbvv nqhfvoxlz
this particularly long sentence can test encryption
mkk in
my pie
taffwqzbmmofuqddjyvvezlatthchzzs eeqrqoosgn
the quick brown fox jumps over the lazy dog

当为‘e’时,正常算,当为‘d’时,通过之前单位的值,连续+27推出未被取模的结果,在正常转化为字符串输出

易错

#include<stdio.h>
#include<string.h>
int t;
char str[105];
int id[105];
int temp[105];
int main()
{
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        memset(id,0,sizeof(id));
        memset(temp,0,sizeof(temp));
        memset(str,0,sizeof(str));
        char op;
        scanf("%c",&op);
        gets(str);
        int len = strlen(str);
        if(op == 'e')
        {
            for(int i = 1; i < len; i++)
            {
                if(str[i] == ' ')
                    id[i] = id[i-1];
                else
                    id[i] = id[i-1] + str[i] - 'a' + 1;
            }
            for(int i=1; i<len; i++)
                id[i]%=27;
            for(int i=1; i<len; i++)
            {
                if(id[i] == 0)
                    str[i]=' ';
                else
                    str[i]=id[i]+'a'-1;
            }
        }
        else if(op == 'd')
        {
            for(int i = 1; i < len; i++)
            {
                if(str[i]==' ')
                    id[i]=0;
                else
                    id[i] = str[i] - 'a' + 1;
            }
            for(int i = 2; i < len; i++)
            {
                temp[i] = temp[i-1] + id[i-1];
                while(id[i] < temp[i])
                    id[i]+=27;
                id[i] = (id[i] - temp[i]) % 27;
            }
            for(int i = 1; i < len; i++)
            {
                if(id[i] == 0)
                    str[i]=' ';
                else
                    str[i] = id[i] + 'a' - 1;
            }
        }
        puts(str+1);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值