2022年7月2日 星期六

迴文

 #include <stdio.h>//基本輸入輸出指令 

#include <stdlib.h>

#include <string.h>//字串長度要引入字串標頭檔 

int main(int argc, char *argv[]) 

{

int T;//根據題目要讀幾筆測資 Test

char S[101];//不超過100+反斜線0 

scanf("%d",&T) ;

while(T--)

{

scanf("%s",S);//字串變數不用加& 

int L=strlen(S);

int i=0,j=L-1;//i指到最前 j指到最後=字串長度-1 

int Yes=1;

while(i<j)//什麼條偉要繼續比較   i=j 或 i>j 

{

if(S[i]!=S[j])//判斷,一開始先假設yes, 

{

Yes=0;//發現有一個不一樣,就no不用比把1改0 

break;//脫離loop 

}

i++;//i<j繼續判斷 

j--;

}

if(Yes==1)//1就是回文 

{

printf("yes\n");//yes是小寫 

}

else

{

printf("no\n");

}

}

return 0;

}

沒有留言:

張貼留言

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...