60分求助

P1914 小书童——凯撒密码

sk_007 @ 2023-10-04 10:39:54


#include<iostream>
#include<string.h>
using namespace std;
int main()
{   
    char a[101];
    int n,k;
    cin>>n;
    cin>>a;
    k=strlen(a);
    for(int i=0;i<k;i++)
    {
        a[i] += 1*n;
        while(int(a[i])>123)
        {
            a[i]-=26;
        }
        cout<<a[i];
    } 

    return 0;
}```

by _O__o_ @ 2023-10-04 11:20:18

我看看


by _O__o_ @ 2023-10-04 11:32:06

建议不直接加n,改为双重循环,n每次加一

该思路AC代码:```

include<iostream>

include<string.h>

using namespace std; int main() { //cout << char('q' + 26) << '1'; char a[101]; int n,k; cin>>n; cin>>a; k=strlen(a); for(int i=0;i<k;i++) { for(int j=0;j<n;j++) { a[i] += 1; if(a[i] > 122) a[i] -= 26; }

// while(int(a[i])>122)//123->122 // { // a[i]-=25;//26->25 // } } cout << a; return 0; }


by _O__o_ @ 2023-10-04 11:32:29

#include<iostream>
#include<string.h>
using namespace std;
int main()
{   //cout << char('q' + 26) << '1';
    char a[101];
    int n,k;
    cin>>n;
    cin>>a;
    k=strlen(a);
    for(int i=0;i<k;i++)
    {
        for(int j=0;j<n;j++)
        {
            a[i] += 1;
            if(a[i] > 122)
                a[i] -= 26;
        }

//      while(int(a[i])>122)//123->122
//        {
//            a[i]-=25;//26->25 
//        }
    } 
    cout << a;
    return 0;
}

by _O__o_ @ 2023-10-04 11:34:40

你这个但凡n太大溢出那么输出就不对


by _O__o_ @ 2023-10-04 11:43:51

@sk_007

@sk_007@sk_007

@sk_007@sk_007

@sk_007


by sk_007 @ 2023-10-04 16:00:50

@_O_o 谢谢大佬


|