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;
}