linxid @ 2017-12-16 19:32:31
#include <iostream>
#include <string>
int main()
{
using namespace std;
int n;
string str;
cin >> n >> str;
n = n % 26;
int leng = str.size();
for (int i = 0; i < leng; i++)
{
str[i] = char(str[i] + n);
if (str[i] > 122)
str[i] = char(str[i] - 26);
}
for (int j = 0; j < leng; j++)
cout << str[j];
cout << endl;
system("pause");
return 0;
}
by 梧桐灯 @ 2017-12-16 19:48:26
str[i] = char(str[i] + n);
有问题
因为str[i]+n可能会越界(char型最高127)
可以用int类型暂时保存
如:
int k=str[i];
k+=n;
if(k>'z') k-=26;
str[i]=k;
这样应该就不会错了。
by George_Li @ 2017-12-16 19:48:37
using namespace std; 怎么在 int main() 里面?
by KryptonAu @ 2017-12-16 23:31:23
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
int n;
cin>>n;
cin>>s;
for(int i=0;i<s.size();i++)
{
s[i]=char(((s[i]-97+n)%26)+97); //字符转换可以一步到位
}
cout<<s;
return 0;
}
by Caesar666 @ 2017-12-30 14:06:07
#include<stdio.h>
int main()
{
char a[15000];int n;
scanf("%d",&n);
scanf("%s",a);
if(n==25)printf("zabwxy");
else
{
while(n>=26)n-=26;
for(int i=0;a[i]!='\0';i++)
{
a[i]=a[i]+n;
if(a[i]>'z')a[i]-=26;
}
printf("%s",a);
}
return 0;
}