传话游戏 2013编程之美

来源:互联网 发布:夜来风雨声花落知多少 编辑:程序博客网 时间:2024/05/16 17:19

 我写了一个答案,在我的编译器是可以通过的,提交之后给出runtime error.甚是费解,求那个高人,大婶,帮忙看看

 

  1. //author bruce 
  2. //2013/4/17 
  3. #include <string>  
  4. #include <iostream> 
  5. #include <map> 
  6. #include <cstdio>  
  7. #include <cstring>  
  8. #include <cmath>  
  9. using namespace std; 
  10.  
  11. map<string,string> WORDS;  
  12. char a_word[20],b_word[20]; //字符长度限制 
  13. char Total_words[100];  //alice说的话 
  14. char    *p; 
  15. int  T,M,N;//分别表示组数,转换列表长度,以及参赛的人数 
  16. char *d=" ";//delim  
  17. void passMessage(); 
  18. int main()  
  19. {  
  20.     cout<<"请输入测试数据组数"<<endl; 
  21.     scanf("%d%*c",&T); 
  22.     for(int NM_NUM = 1;NM_NUM <= T;NM_NUM++)  
  23.     { 
  24.         WORDS.clear();  
  25.         cout<<"请分别输入参赛人数和列表长度(注意 人数一定要大于1人,否则传话又有什么意思呢)"<<endl; 
  26.         scanf("%d %d%*c",&N,&M); 
  27.          
  28.  
  29.         for(int  i = M;i > 0;i--) 
  30.         {  
  31.             cout<<"请分别输入转换前单词a,和转换后单词b"<<endl; 
  32.             scanf("%s %s%*c",a_word,b_word);  
  33.             WORDS[a_word] = b_word;  
  34.         } 
  35.         cout<<"请输入alice想说的话"<<endl; 
  36.         gets(Total_words); 
  37.         p = strtok(Total_words,d); 
  38.         printf("传话完毕之后 Case #%d:\n ",NM_NUM); 
  39.         //cout<<"传话完毕之后"<<endl; 
  40.         passMessage(); 
  41.          
  42.         while(  p=strtok(NULL,d)  ) 
  43.         { 
  44.              
  45.             cout<<""
  46.             passMessage();  
  47.         }  
  48.         cout<<endl; 
  49.     } 
  50.     return 0;  
  51. }  
  52. void passMessage()  
  53. {  
  54.     string messageWords; 
  55.     messageWords = p;  
  56.     bool judge; 
  57.     for(int i = 1;i < N;i++) 
  58.     {  
  59.         judge = WORDS.count(messageWords); 
  60.         if(judge) messageWords = WORDS[messageWords];  
  61.         else  
  62.             break
  63.     }  
  64.     cout<<messageWords<<endl;  
  65. }  

本文出自 “LuoZhengWu” 博客,请务必保留此出处http://brucemars.blog.51cto.com/5288106/1175603

0 0
原创粉丝点击