题解:这是洛谷的第9道题目,考察的还是循环的使用。数字反转,我们就用简单的取位,放到数组里,然后输出即可。这里有几个问题需要注意:
1 . 数字可能是负数(很简单,取个绝对值,反转过来之后加上‘-’即可)
源代码:
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <math.h>
using namespace std;
int main()
{
bool isnot = false;
long long a, b, bArray[100], counter = 0,sum = 0,c;
cin >> a;
a > 0 ? isnot = true : isnot = false;
c = a = abs(a);
if (c != 0)
{
while (a != 0)
{
b = a % 10; a = a / 10;
if (b == 0 && counter == 0)
continue;
bArray[counter] = b;
counter++;
}
if (isnot)
{
for (size_t i = 0; i <counter; i++)
sum += pow(10, counter - 1 - i) * bArray[i];
cout << sum << endl;
}
else
{
for (size_t i = 0; i < counter; i++)
sum += pow(10, counter - 1 - i) * bArray[i];
cout << -sum << endl;
}
}
else
{
if (a == 0)
cout << a << endl;
}
system("pause");
return 0;
}