cjnoy @ 2022-12-30 14:44:43
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
string a;
cin >> n;
cin >> a;
int i;
for (i = 0; i < a.size(); i++)
{
a[i] = a[i] + n;
if (a[i] > 'z')
a[i] = a[i] - 26;
}
cout << a;
return 0;
}
by xhz0311 @ 2022-12-30 14:45:55
n>26呢?题目可没有保证这一点啊。
by xhz0311 @ 2022-12-30 14:46:06
@cjnoy
by 2345A @ 2022-12-30 14:53:51
@cjnoy
#include <iostream>
using namespace std;
int main()
{
string a;
int b, i, j;
cin >> b;
cin >> a;
for(i=0;i<=a.size()-1;i++)
{
for (j = 1; j <= b; j++)
{
a[i]++;
if (a[i] > 122)
{
a[i] = 97;
}
}
}
cout << a;
return 0;
}
by cjnoy @ 2022-12-30 15:20:26
@xhz0311
n大于26,我试了一下,没有问题诶
by cjnoy @ 2022-12-30 15:23:28
@baiqian
谢谢大佬!但是我想知道为什么原先那个代码有WA的地方嘞
by 2345A @ 2023-01-02 12:02:21
@cjnoy 要-1
我for(i=0;i<=a.size()-1;i++)
你for (i = 0; i < a.size(); i++)
by bluetored @ 2023-02-10 10:44:33
@cjnoy 不知道你现在会了吗,我的第一次代码跟你的差不多,改了两遍才AC.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
string s;
cin >> n>>s;
n %= 26;
for (int i = 0; i < s.size(); i++) {
int x = s[i] + n;
if (x > 'z')s[i] = x - 26;
else s[i] = x;
}
cout << s;
return 0;
}
by cjnoy @ 2023-02-23 19:11:51
@bluetored 原来是这样,看了大佬的解释就会啦,很感谢很感谢!!!