蒟蒻求大佬!

P1307 [NOIP2011 普及组] 数字反转

马兔up @ 2023-02-15 22:41:12

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>
using namespace std; 
int main(){
    string s;
    cin >>s;
    int index=0;
    if(s[0]=='-') //字符  
    {
        index=1;
        cout <<"-"; 
    }

    for(int i=s.length()-1;i>=index;i--)
    {

        if(s[i]>='0'&&s[i]<='9' && (i!=s.length()-1||s[i]!='0'))
            cout <<s[i];
    }
} 

by Kevin_Mamba @ 2023-02-16 06:24:47

@马兔up 你这去前导零的方式有点不对啊。


by Kevin_Mamba @ 2023-02-16 06:28:24

@马兔up

    int i=s.length()-1;
    while(~i&&s[i]=='0') i--;
    for(;i>index;i--)
    {
        cout <<s[i];
    }
    cout<<s[index]<<'\n';

你那样写至多去掉一个前导零。


by 马兔up @ 2023-02-16 11:09:37

@Kevin_Mamba 大佬tql!


by Alan_Moyar @ 2023-03-11 10:01:40

#include <cstdio>
#include <cstring>
using namespace std;
char s[100],c[100]; 
int main(){
      int n,l;
      scanf("%d",&n);
      sprintf(s,"%d",n);
      l=strlen(s);
for (int i=0; i<=l-1; ++i)  
           c[l-i-1]=s[i];
      if(n<0) 
         printf("-");
      sscanf(c,"%d",&n);
      printf("%d",n);
      return 0;}

|