为什么错了?没一个过的

P1914 小书童——凯撒密码

fightclub @ 2022-11-02 19:32:39

#include<stdio.h>
int main(){
    int n;
    scanf("%d",&n);
    char c;
    c=getchar();
    while((c=getchar())!='\n'){
        if(c+n<='z') putchar(c+n);
        else putchar(c+n-26);
    }
    return 0;
}

by 晴空一鹤 @ 2022-11-02 19:33:56

@fightclub

把第6行去掉就可以啦


by fightclub @ 2022-11-02 19:36:04

@晴空一鹤 还是不行,因为我在dev cpp里面运行了,然后它会顺便把n也给读进去,所以我就这么写了


by 晴空一鹤 @ 2022-11-02 19:40:05

@fightclub

那就先把不是字符的读完再开始继续读。

即把第6行改成 c=getchar();while(c<'a'||c>'z')c=getchar();

再把第7行那个getchar()放到循环末尾


by fightclub @ 2022-11-02 20:05:38

@晴空一鹤 TLE了。。。。。。


by 晴空一鹤 @ 2022-11-02 20:15:52

@fightclub 换行符问题吧,建议string,我3年前的代码供参考:

#include <iostream>
using namespace std;
int main() 
{
  int n;
  string a;
  cin>>n;
  cin>>a;
  for(int i=0;i<a.size();i++)
  {
      a[i]-=26;
      a[i]+=n;
      if(a[i]+26>'z')
      a[i]-=26;
      a[i]+=26;
  }
  cout<<a;
}

by fightclub @ 2022-11-09 23:48:55

@晴空一鹤 OK,过了,谢谢


|