怎么把前面的0给去掉。注意如果逆序后的数有前导0,不用输出前导0,如100,逆序后输出1.
但是如果是40001,不能去掉0
```c++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x;
scanf("%d", &x);
while (x != 0)
{
printf("%d", x % 10);
x = x / 10;
}
return 0;
}
每次取到个位数,不直接输出,而是逐个累加成逆序的整数,最后再输出这个整数即可
#include <stdio.h>
int main()
{
int x;
scanf("%d", &x);
int result = 0;
while(x!=0)
{
result = result*10 + x%10;
x = x/10;
}
printf("%d\n", result);
return 0;
}
你稍等,代码马上给你
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num;
cin>>num;
int a[1005];
int tmp=0,t;
while(num)
{
tmp++;
a[tmp]=num%10;
num/=10;
}
for(int i=1;i<=tmp;i++)
{
if(a[i]!=0)
{
t=i;
break;
}
}
for(int i=t;i<=tmp;i++) cout<<a[i];
return 0;
}
这个比较简单,我们列好方程,即可求出
x13+x22+x3*0.5=100
x1+x2+x3=100
解决方案如下:
#include <iostream>
#include <string>
std::string removeLeadingZeros(std::string num) {
// 去除前导零
int i = num.length() - 1;
while (i > 0 && num[i] == '0') {
num.pop_back();
i--;
}
// 逆序字符串
std::reverse(num.begin(), num.end());
return num;
}
int main() {
std::string input;
std::cout << "请输入一个数字: ";
std::cin >> input;
std::string result = removeLeadingZeros(input);
std::cout << "去除前导零后的结果为: " << result << std::endl;
return 0;
}
这段代码实现了一个函数removeLeadingZeros
,用于去除数字的前导零。函数首先遍历数字字符串,从最后一个字符开始向前遍历,直到遇到第一个非零字符为止。然后使用std::reverse
函数将字符串逆序。最后返回去除前导零的结果。
在main
函数中,先读取用户输入的数字字符串,然后调用removeLeadingZeros
函数并输出结果。
请注意,该代码假设输入的数字字符串不包含非数字字符,并且数字字符串不为空。如果有需要,可以加入相关的输入验证。