yan044 @ 2022-01-19 10:58:51
#include<bits/stdc++.h>
using namespace std;
stack<char> p;
string s;
int main()
{
cin >> s;
if (s[0] == '-')
{
cout << "-";
for (int i = 1 ; i <= s.length() ; i++)
p.push(s[i]);
}
else
{
for (int i = 0 ; i <= s.length() ; i++)
p.push(s[i]);
}
** while (p.top() == '0')
p.pop();**
while(!p.empty())
{
cout << p.top();
p.pop();
}
return 0;
}
by _maojun_ @ 2022-01-19 11:13:17
因为你用的是栈啊,这样只能去后缀0,要去前导0要用队列吧……
#include<bits/stdc++.h>
using namespace std;
queue<char> p;
string s;
int main()
{
cin >> s;
if (s[0] == '-')
{
cout << "-";
for (int i = s.length() - 1 ; i >= 1 ; i--)
p.push(s[i]);
}
else
{
for (int i = s.length() - 1 ; i >= 0 ; i--)
p.push(s[i]);
}
while (p.front() == '0')
p.pop();
while(!p.empty())
{
cout << p.front();
p.pop();
}
return 0;
}
只是要把push的顺序改一下。
by _maojun_ @ 2022-01-19 11:13:59
我也不知道这个代码能不能AC,至少能过样例(
by yan044 @ 2022-01-19 11:47:05
@maojun 用栈的话他不就直接反转了,然后就是要去掉栈的头呀
by _maojun_ @ 2022-01-19 21:33:00
但是用栈你是先反转再去前导0,那不就成去后缀0了?
by _maojun_ @ 2022-01-19 21:39:55
非要用栈的话得先在字符串上去前导0(也就是字符串的后缀0),但这样就没有用栈的必要了……
by sfeihmi @ 2022-01-22 23:25:03
for (int i = 0 ; i <= s.length() ; i++) 去掉等号就可以了应该
by krjt @ 2022-03-19 09:21:46
//代码:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
int chk(int x){
int ans=0,t=x;
while(x){
ans=ans*10+x%10;
x/=10;
}
return ans;
}
int main(){
int n;
cin>>n;
cout<<chk(n);
return 0;
}