反轉(zhuǎn)整數(shù)
對于輸入的一個正整數(shù)招刨,輸出其反轉(zhuǎn)形式
要求使用c++ class編寫程序神僵。可以創(chuàng)建如下class
輸入描述
一個正整數(shù)a 冀痕,且1=<a<=1,000,000,000
輸出描述
a的反轉(zhuǎn)形式
樣例輸入
1011
樣例輸出
1101
1 #include <iostream>
2 using namespace std;
3
4 class Integer{
5 private:
6 int _num;
7 //getLength()函數(shù)獲取_num長度
8 int getLength(){
9 int tmp = 0, _tmp = _num;
10 do{
11 _tmp=_tmp / 10;
12 tmp++;
13 } while (_tmp!=0);
14 return tmp;//既然返回值是數(shù)據(jù)福铅,那么就當私有數(shù)據(jù)成員處理
15 }
16 public:
17 //Integer類構(gòu)造函數(shù)
18 Integer(int num){
19 _num = num;
20 }
21 //反轉(zhuǎn)_num
22 int inversed(){
23 int temp=0;
24 int temp1 = getLength();
25 int temp2 = _num;
26 for (int i = 0; i < temp1-1; i++){
27 temp = (temp+temp2 % 10)*10;
28 temp2=temp2 / 10;
29 }
30 temp = temp + temp2;
31 return temp;
32 }
33 };
34
35 int main() {
36 int n;
37 cin >> n;
38 Integer integer(n);
39 cout << integer.inversed() << endl;
40 return 0;
41 }