精華區beta NTUBIME104HW 關於我們 聯絡資訊
#include <iostream> #include <fstream> using namespace std; int main(){ char s[46000][15], rs[15]; ifstream fin; fin.open("words.txt"); //開啟名為 "words.txt" 的檔案 if(fin.fail()) { cout << "The file was not successfully opened." << "\nPlease check that the file currently exists." << endl; system("PAUSE"); exit(1); }//判斷是否成功開啟 int len = 0; for(int j=0; j<45407; j++){ fin >> s[j]; //讀入字串到 s if(fin.eof()) break; len = strlen(s[j]); for(int i=0; i<len ;i++) if(s[j][i]<='Z') s[j][i] += 'a'-'A'; //將所有的字母轉換為小寫 } len = 0; for(int i=0; i<45407; i++){ for(int j=i+1; j<45407; j++){ for(int k=0; k<strlen(s[i]); k++) rs[k] = s[i][strlen(s[i])-k-1]; //將字串反轉儲存於字元陣列 rs 中 rs[strlen(s[i])] = '\0'; if(!strcmp(rs, s[j])){ if(strlen(s[i])>len){ len = strlen(s[i]); } } } /* 判斷是否有另一字串的反轉是自己本身 若是則判斷此字串的長度 是否較當前找到最長合法字串的長度大 若是則更新 len 的值 */ } cout << len << endl; //輸出最大長度 len system("pause"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.70.209.87
g85101878 :新年快樂~耶比壽喜燒賣當當! 01/01 08:59
※ 編輯: roymustang 來自: 219.70.209.87 (01/02 00:00)