我寫了一個答案,在我的編譯器是可以通過的,提交之後給出runtime error.甚是費解,求那個高人,大嬸,幫忙看看
- //author bruce
- //2013/4/17
- #include <string>
- #include <iostream>
- #include <map>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- using namespace std;
- map<string,string> WORDS;
- char a_word[20],b_word[20]; //字符長度限制
- char Total_words[100]; //alice說的話
- char *p;
- int T,M,N;//分別表示組數,轉換列表長度,以及參賽的人數
- char *d=" ";//delim
- void passMessage();
- int main()
- {
- cout<<"請輸入測試數據組數"<<endl;
- scanf("%d%*c",&T);
- for(int NM_NUM = 1;NM_NUM <= T;NM_NUM++)
- {
- WORDS.clear();
- cout<<"請分別輸入參賽人數和列表長度(注意 人數一定要大於1人,否則傳話又有什麼意思呢)"<<endl;
- scanf("%d %d%*c",&N,&M);
- for(int i = M;i > 0;i--)
- {
- cout<<"請分別輸入轉換前單詞a,和轉換後單詞b"<<endl;
- scanf("%s %s%*c",a_word,b_word);
- WORDS[a_word] = b_word;
- }
- cout<<"請輸入alice想說的話"<<endl;
- gets(Total_words);
- p = strtok(Total_words,d);
- printf("傳話完畢之後 Case #%d:\n ",NM_NUM);
- //cout<<"傳話完畢之後"<<endl;
- passMessage();
- while( p=strtok(NULL,d) )
- {
- cout<<"";
- passMessage();
- }
- cout<<endl;
- }
- return 0;
- }
- void passMessage()
- {
- string messageWords;
- messageWords = p;
- bool judge;
- for(int i = 1;i < N;i++)
- {
- judge = WORDS.count(messageWords);
- if(judge) messageWords = WORDS[messageWords];
- else
- break;
- }
- cout<<messageWords<<endl;
- }