Kwnx23lqb @ 2023-06-11 22:01:03
#include<stdio.h>
char tran(char c,int n){
char r;
if(c+n<=122)r=c+n;
if(c+n>122)r=tran(c,n-26);
return r;
}
int main(){
int n;
char c,r;
scanf("%d",&n);
getchar();
while(1){
c=getchar();
if(c<97||c>122)return 0;
r=tran(c,n);
printf("%c",r);
}
}
by Sqj147 @ 2023-06-16 22:08:48
getchar()
把回车也读进去了
by Sqj147 @ 2023-06-16 22:14:40
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
char a;
while(cin >> a) {
if(a + n > 122) {
a += (n - 26);
cout << a;
continue;
}
a += n;
cout << a;
}
system("pause");
return 0;
}
by Kwnx23lqb @ 2023-06-21 12:13:29
@Sqj147 谢谢解答,但是我while循环前已经用过一个getchar吃掉回车了啊
by Sqj147 @ 2023-06-21 13:16:39
@Kwnx23lqb
要使用2个getchar()
Linux中换行符是’\n’而Windows中是’\r\n’(多一个字符),有些数据在Windows中生成,而在洛谷评测机Linux环境下评测。这种情况在字符串输入中非常常见。
by Kwnx23lqb @ 2023-06-25 18:37:47
@Sqj147 哇,原来是这样,谢谢大佬,终于知道为啥错了。