48.輸入任意正整數,編程判斷該數是否為迴文數(迴文數是指從左至右讀與從右至左讀一樣,如12321)

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   for   div   log   

//1、輸入一個數,將其每一位分離,並儲存如一個數組//2、判斷數組最後錄入的一位是第幾位//3、迴圈判斷是否滿足回問數的要求#include<iostream>using namespace std;int main(){    int n,temp;    int k=0;    int a[20];    cout<<"please input an number: "<<endl;    cin>>n;    for(int i=0;i<20;i++)//用於將每一位分離,並存入數組中    {        if(n>=1)        {            temp=n%10;//分離的關鍵步驟            a[i]=temp;            n=n/10;            k++;//計數器,能知道這個數一共有幾位        }    }    for(int m=0;m<k;m++)//判斷這個數是不是迴文數    {        if(a[m]!=a[k-m-1])        {            cout<<"這不是迴文數!"<<endl;            break;//跳出迴圈        }else        {            cout<<"這是迴文數!"<<endl;            break;//跳出迴圈        }    }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.