Board logo

標題: 504 迴文數 [打印本頁]

作者: 鄭繼威    時間: 2024-2-3 11:02     標題: 504 迴文數

1. 題目說明:
請依下列題意進行作答,使輸出值符合題意要求。

2. 設計說明:
請撰寫一程式,讓使用者輸入一正整數,判斷是否為迴文數,若是,請輸出「Yes」;若不是,請輸出「No」。

迴文數:此數的數字按相反的順序重新排列後,所得到的數和原來的數一樣。

提示:若使用 Java 語言答題,請以「JP」開頭命名包含 main 靜態方法的 class,評測系統才能正確評分。

3. 輸入輸出:
輸入說明
一個正整數

輸出說明
判斷是否為迴文數

範例輸入1
14641
範例輸出1
Yes

範例輸入2
25523
範例輸出2
No

本帖隱藏的內容需要回復才可以瀏覽

作者: 黃品禎    時間: 2024-2-3 18:20

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     int a;
  6.     cin>>a;
  7.     string str;
  8.     str=to_string(a);
  9.     string str1="",str2="";
  10.     if(str.length()%2==1)
  11.     {
  12.             for(int i=0;i<=str.length()/2-1;i++)
  13.         {
  14.             str1+=str[i];
  15.         }
  16.         for(int i=str.length()-1;i>=str.length()/2+1;i--)
  17.         {
  18.             str2+=str[i];
  19.         }
  20.         if(str1==str2)
  21.         {
  22.             cout<<"Yes";
  23.         }
  24.         else
  25.         {
  26.             cout<<"No";
  27.         }
  28.     }

  29.     else if(str.length()%2==0)
  30.     {
  31.         for(int i=0;i<=str.length()/2-1;i++)
  32.         {
  33.             str1+=str[i];
  34.         }
  35.         for(int i=str.length()-1;i>=str.length()/2;i--)
  36.         {
  37.             str2+=str[i];
  38.         }
  39.         if(str1==str2)
  40.         {
  41.             cout<<"Yes";
  42.         }
  43.         else
  44.         {
  45.             cout<<"No";
  46.         }
  47.     }







  48. return 0;
  49. }
複製代碼

作者: 李宗儒    時間: 2024-3-13 19:03

  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     string a;
  6.     cin>>a;
  7.         int b=a.length()/2,c=0;//01234
  8.         for(int i=0;i<b;i++)
  9.         {
  10.             if(a[i]!=a[a.length()-i-1])
  11.             {
  12.                 cout<<"No";
  13.                 return 0;
  14.             }
  15.         }
  16.         cout<<"Yes";
  17.     return 0;
  18. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2