求助!这题用自定义函数怎么做?求大神解答!在线等,急!!!!!!

P1307 [NOIP2011 普及组] 数字反转

_52Hertz_Ustinian @ 2024-10-10 17:10:49

试了好久,只能用递归才做得对。求助!!!


by abssortpow1145145 @ 2024-10-12 21:01:48

ACd代码求关(尽供参考)


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

by Noah_Shen @ 2024-10-13 19:07:38

AC代码 仅供参考

#include <bits/stdc++.h>
using namespace std;
int temp;
int main(){
    char a[12];
    cin >> a;
    if(a[0] == '0'){
        cout << 0;
        return 0;
    }
    int lena = strlen(a);
    bool isnegative = (a[0] == '-');
    if(isnegative){
        cout << '-';
    }
    bool iszero = (a[lena - 1] == '0');
    for(int i = lena - 1; i >= (isnegative ? 1 : 0); i--){
        if(a[i] == '0' && iszero){
            continue;
        }
        iszero = false;
        cout << a[i];
    }
    return 0;
}

by _52Hertz_Ustinian @ 2024-10-20 14:29:18

谢谢大神,已经解决了


|