诚心请教一道ACM问题(简单题)!请各位好人帮忙看看
zoj 1926题
题目是:
Guessing Game
Stan and Ollie are playing a guessing game. Stan thinks of a number between 1 and 10 and Ollie guesses what the number might be. After each guess, Stan indicates whether Ollie 's guess is too high, too low, or right on.
After playing several rounds, Ollie has become suspicious that Stan cheats; that is, that he changes the number between Ollie 's guesses. To prepare his case against Stan, Ollie has recorded a transcript of several games. You are to determine whether or not each transcript proves that Stan is cheating.
Input
Standard input consists of several transcripts. Each transcript consists of a number of paired guesses and responses. A guess is a line containing single integer between 1 and 10, and a response is a line containing "too high ", "too low ", or "right on ". Each game ends with "right on ". A line containing 0 follows the last transcript.
Output
For each game, output a line "Stan is dishonest " if Stan 's responses are inconsistent with the final guess and response. Otherwise, print "Stan may be honest ".
Sample Input
10
too high
3
too low
4
too high
2
right on
5
too low
7
too high
6
right on
0
Sample Output
Stan is dishonest
Stan may be honest
我的代码是:
#include <stdio.h>
#include <memory.h>
int main()
{
short an[10],hi[10],lo[10],ri;
bool ju;
char ra[9];
while(1)
{ memset(an,0,sizeof(an));
memset(ra, '* ',sizeof(ra));
int i=0,j=0,n=0,m=0,t=0;
ju=1;
while(1)
{
scanf( "%d ",&an[i]);if(an[0]==0) break;
char c;
c=getchar();
gets(ra);
if(ra[4]== 'h ') {hi[m]=an[t];m++;t++;}
else if(ra[4]== 'l ') {lo[n]=an[t];n++;t++;}
else if(ra[0]== 'r ') {ri=an[t];}
if(ra[0]== 'r ') break;
i++;}
for(i=0;i <m;i++)
{
for(j=0;j <n;j++)
{
if(ri> lo[j]&&ri <hi[i]) ju=1;
else ju=0;
if(ju==0) break;
}
if(ju==0) break;
}
if(an[i]==0) break;
if(ju||t==0) printf( "Stan may be honest\n ");
else printf( "Stan is dishonest\n ");
}
return 0;
}
在自己电脑上运行后已经跟题目要求的答案一样了,可是提交后仍是“wrong answer”,实在搞不清了,请大家帮帮我~~
另外,代码的各各方面有哪些不足之处也请多指教!
[解决办法]
代码风格怎么感觉这么难读~
[解决办法]
你的思路太凌乱了~~应该改进你这种算法,才能做得更好,过多的变量会使自己迷失方向的
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector <int> GetArray()
{return vector <int> (10,1); }
int main()
{
string StansJudge;
int OlliesGuess;
int StepDirection; // 1 to right, -1 to left
vector <int> NumberArray = GetArray();
while(true)
{
cin> > OlliesGuess;
if ( OlliesGuess==0 )
break;
cin> > StansJudge> > StansJudge;
if ( StansJudge== "high " )
StepDirection = 1;
else if ( StansJudge == "low " )
StepDirection = -1;
else if ( StansJudge == "on " )
{
if ( NumberArray[OlliesGuess-1]!=1 )
cout < < "Stan is dishonest " < <endl;
else
cout < < "Stan may be honest " < <endl;
NumberArray = GetArray();
continue;
}
for ( int i=OlliesGuess; i> 0 && i <11 ; i += StepDirection )
NumberArray[i-1] = 0;
}
}
上面这个算法用了一个数组来对结果进行记录,思想是很简单的
[解决办法]
想要做好ACM,最好把STL给学学,能省却你非常多的时间
因为你不可能需要对一个数组进行排序的时候还自己再写一个吧,那样浪费的时间太多了
而STL无论在数据结构还是算法上都有极好的效率及表现。
