传话游戏 2013编程之美
2014-04-01 14:32
169 查看
我写了一个答案,在我的编译器是可以通过的,提交之后给出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;
}
本文出自 “LuoZhengWu” 博客,请务必保留此出处http://brucemars.blog.51cto.com/5288106/1175603
//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;
}
本文出自 “LuoZhengWu” 博客,请务必保留此出处http://brucemars.blog.51cto.com/5288106/1175603
相关文章推荐
- 2013 编程之美资格赛 第一题 传话游戏 解题报告
- 2013微软编程之美资格赛试题(一)解题思路及C++ Code
- 编程之美资格赛第一题:传话游戏
- 编程之美2013全国挑战赛资格赛第3题
- 传话游戏(2013编程之美全国挑战赛资格赛)
- 2013 编程之美挑战赛 相似字符串
- 2013 编程之美挑战赛 集会
- 编程之美2013 资格赛第二题 长方形
- 2013 编程之美挑战赛 仙剑5前传之璇光殿
- 编程之美2013初赛——竞价
- 编程之美2013 初赛一 A - 竞价 学习大牛的思路
- 2013 编程之美资格赛
- 2013 编程之美挑战赛 管道系统
- 2013编程之美全国挑战赛---相似字符串
- 编程之美2013资格赛 水结
- 相似字符串(编程之美2013初赛题2)
- 2013 编程之美挑战赛 仙剑5前传之软星包子
- 微软编程之美2013全国挑战赛 复赛 第2题
- 2013编程之美全国挑战赛 长方形
- 编程之美2013全国挑战赛之传话游戏