【C++】反轉(zhuǎn)整數(shù)

反轉(zhuǎn)整數(shù)
對(duì)于輸入的一個(gè)正整數(shù),輸出其反轉(zhuǎn)形式

要求使用c++ class編寫程序??梢詣?chuàng)建如下class

輸入描述
一個(gè)正整數(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長(zhǎng)度
 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ù),那么就當(dāng)私有數(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 }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容