样例和自己编的数据能过但RE

P1307 [NOIP2011 普及组] 数字反转

zzh3050426589 @ 2024-04-02 00:20:09

求大佬教蒟蒻优化

#include<iostream>
using namespace std;
string reverse(string s,int start,int end)
{
    while(start<end)
    {
        char temp=s[start];
        s[start]=s[end];
        s[end]=temp;
        start++;
        end--;
     } 
     int count=0;
     for(int i=0;i<s.length();i++)
     {
        if(s[i]!='0'&&s[i]!='-')count++;
        if(count==0&&s[i]=='0')continue;
        else cout<<s[i];
     }
}
int main()
{
    string num;
    cin>>num;
    if(num[0]=='-')
    {
        int len=num.length();
        int start=1;
        reverse(num,start,len-1);
    }
    else 
    {
        int len=num.length();
        int start=0;
        reverse(num,start,len-1);
    }
    return 0;
}

by huzihang666 @ 2024-04-14 11:29:09

你可能时间超了


by qusia_MC @ 2024-05-02 15:06:42

@baby2013 应该不是这个问题

@zzh3050426589 你的string 定义了吗就用


by qusia_MC @ 2024-05-02 15:09:52

@zzh3050426589

#include<bits/stdc++.h>//string包含在万能头里
using namespace std;
void reverse(string s,int start,int end)//你的返回类型是string但是似乎你没有返回值
{
    while(start<end)
    {
        char temp=s[start];
        s[start]=s[end];
        s[end]=temp;
        start++;
        end--;
     } 
     int count=0;
     for(int i=0;i<s.length();i++)
     {
        if(s[i]!='0'&&s[i]!='-')count++;
        if(count==0&&s[i]=='0'&&i!=s.size()-1)continue;//特判一下不是最后一个(排除是0)
        else cout<<s[i];
     }
return ;
}
int main()
{
    string num;
    cin>>num;
    if(num[0]=='-')
    {
        int len=num.length();
        int start=1;
        reverse(num,start,len-1);
    }
    else 
    {
        int len=num.length();
        int start=0;
        reverse(num,start,len-1);
    }
    return 0;
}

by zzh3050426589 @ 2024-06-01 10:45:43

@qusia_MC 谢谢大佬指出orz


|