輸入整數,倒着輸出整數

#include<bits/stdc++.h>
using namespace std;
static bool L = false;
int fun(int a)
{
    if(a>0)
    {
       if(a%10==0){//判斷最後一位是否爲0
           if(L==false){防止中間有0,最後不能輸出
               a=a/10;
               fun(a);
           }
           else
           {
               cout<<a%10; 
               a=a/10; 
                fun(a); 
       }
       }else{
           L=true;
           cout<<a%10; 
               a=a/10; 
                fun(a);
       }
    }
       
    return 0;
}

int main()
{
    
    int i;
    cin>>i;
    if(i<0){
        cout<<"-";//直接判斷正負
    }
    fun(fabs(i));//取絕對值
    return 0;
}

 

發佈了38 篇原創文章 · 獲贊 21 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章