基础题,注意细节,详细见注释。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
int num; //输入
cin >> num;
if (num == 0) //输入为0
{
cout << 0 << endl;
continue;
}
int temp = abs(num); //转换为绝对值
int count = 0; //末尾0的个数
while (temp % 10 == 0) //计算末尾0的个数,并将其去除
{
++count;
temp /= 10;
}
if (num < 0) //输入为负数,输出负号
cout << "-";
while (temp > 0) //输出正数的逆向
{
cout << temp % 10;
temp /= 10;
}
while (count--) //输出末尾的0
cout << 0;
cout << endl;
}
return 0;
}
继续加油。
本文介绍了一个基础编程题目,通过C++实现,该程序读取一系列整数,逆序打印每个数的非零部分,同时忽略末尾的零。特别地,对于负数,会保留符号。此题旨在测试对输入输出、循环和条件判断的理解。
1066

被折叠的 条评论
为什么被折叠?



