題目描述
??對(duì)于輸入的一個(gè)正整數(shù),輸出其反轉(zhuǎn)形式
??要求使用c++ class編寫程序。可以創(chuàng)建如下class
輸入描述
??一個(gè)正整數(shù)a 绕娘,且1=<a<=1,000,000,000
輸出描述
??a的反轉(zhuǎn)形式
樣例輸入
??1011
樣例輸出
??1101
#include <iostream>
using namespace std;
class Integer{
private:
int _num;
//getLength()函數(shù)獲取_num長(zhǎng)度
int getLength(){
int tmp = 0, _tmp = _num;
do{
_tmp=_tmp / 10;
tmp++;
} while (_tmp!=0);
return tmp;//既然返回值是數(shù)據(jù),那么就當(dāng)私有數(shù)據(jù)成員處理
}
public:
//Integer類構(gòu)造函數(shù)
Integer(int num){
_num = num;
}
//反轉(zhuǎn)_num
int inversed(){
int temp=0;
int temp1 = getLength();
int temp2 = _num;
for (int i = 0; i < temp1-1; i++){
temp = (temp+temp2 % 10)*10;
temp2=temp2 / 10;
}
temp = temp + temp2;
return temp;
}
};
int main() {
int n;
cin >> n;
Integer integer(n);
cout << integer.inversed() << endl;
return 0;
}