3,4点过不去,考虑了特判,双重循环做法

P1914 小书童——凯撒密码

__er @ 2022-04-27 12:22:12

60 Code:

#include <bits/stdc++.h>
using namespace std;
string a;
long long i, n, j;

int main() {
    cin >> n;
    cin >> a;
    for (i = 0; i < a.length(); i++) {
        for (j = 1; j <= n; j++) {
            if (a[i] == 'z') {
                a[i] = 'a';
            }
            a[i]++;
        }
    }
    cout << a;
    return 0;
}

by liangbowen @ 2022-04-27 12:33:22

#3 4 是WA还是TLE


by 编码落寞 @ 2022-04-27 12:45:30

@YMXX

            if (a[i] == 'z') {
                a[i] = 'a';
            }
            a[i]++;

如果当前为z,那么加1,不直接变为b了吗


by suyi1111 @ 2022-04-27 12:49:07

当a[i]是z时,a[i]先经过前面的判断加了1,又经过后面的啊“a[i]++;”,加了2,可是j只加了1.


by char_cha_ch @ 2022-04-27 13:00:48

这个的话,应该判断有没有超过z,最好不要你那样


by char_cha_ch @ 2022-04-27 13:04:07

还有长度的话应该另设变量,不然很容易tle


by todayfinish @ 2022-04-27 13:07:03

 #include <bits/stdc++.h>
 using namespace std;
 typedef unsigned long long ll;
 #define ri register ll
 ll gcd(ll a,ll b) {return b? gcd(b,a%b):a;} 

 ll n,l;
 string s;

 signed main()
 {
     ios::sync_with_stdio(0),cin.tie(0);
     cin>>n>>s;
    l=s.length();
     for(ri i=0;i<=l-1;i++)
     {
         ri k=s[i]-'a';
         k+=n;
         k%=26;
         s[i]=k+'a';
     }
     cout<<s;
     return 0;
 }

不用感谢


by __er @ 2022-04-27 13:19:48

@liangbowen WA


by __er @ 2022-04-27 13:20:10

@kirihara233 谢谢


by __er @ 2022-04-27 13:21:33

@suyihang 谢谢,AC了


by Southern_Dynasty @ 2022-04-27 13:21:47

@YMXX 特判 a_i 是否为 z 的时候 continue


| 下一页