[英]how do i reverse a number which starts with zero in cpp?
how do i reverse a number which starts with zero in c++?我如何反转 c++ 中以零开头的数字? like 000021 to 120000 reverse a number which starts with zero像 000021 到 120000 反转一个以零开头的数字
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
while (T--) {
int n, l;
cin >> n;
while (n != 0) {
l = n % 10;
cout << l;
n /= 10;
if (n != 0)
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
you need to take input number as string.您需要将输入数字作为字符串。
string s = "00012";
string str="";
for(int i=s.size()-1;i>=0;i--){
str+=s[i];
}
cout<<stoi(str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.