【學堂線上】反轉整數
阿新 • • 發佈:2019-01-09
題目描述
對於輸入的一個正整數,輸出其反轉形式
要求使用c++ class編寫程式。可以建立如下class
輸入描述
一個正整數a ,且1=<a<=1,000,000,000
輸出描述
a的反轉形式
樣例輸入
1011
樣例輸出
1101
#include <iostream>
using namespace std;
class Integer{
private:
int _num;
//getLength()函式獲取_num長度
int getLength(){
int tmp = 0, _tmp = _num;
do{
_tmp=_tmp / 10;
tmp++;
} while (_tmp!=0);
return tmp;//既然返回值是資料,那麼就當私有資料成員處理
}
public:
//Integer類建構函式
Integer(int num){
_num = num;
}
//反轉_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;
}