为什么全错啊

P5705 【深基2.例7】数字反转

lininglong2012 @ 2024-10-27 09:09:03

#include<bits/stdc++.h>
using namespace std;
int main(){
    string n;
    cin>>n;
    char a[n.size()]={0};
    for (int i = 0; i < n.size(); i++)
    {
        a[n.size()-i]=n[i];
    }
    for (int i = 0; i < n.size()+1; i++)
    {
        cout<<a[i];
    }
    return 0;
}

by _A8cde_ @ 2024-10-27 09:15:25

第二个循环的i初始化成1就可以了

#include<bits/stdc++.h>
using namespace std;
int main(){
    string n;
    cin>>n;
    char a[n.size()]={0};
    for (int i = 0; i < n.size(); i++)
    {
        a[n.size()-i]=n[i];
    }
    for (int i = 1; i < n.size()+1; i++)
    {
        cout<<a[i];
    }
    return 0;
}

by _A8cde_ @ 2024-10-27 09:20:18

互关么


by _A8cde_ @ 2024-10-27 09:30:21

@lininglong2012


|