Analysis: Your Ride Is Here
This is probably the easiest problem in the entire set of lessons. An `ad hoc' problem, no particular algorithms or tricks are needed save one: one must be careful to get all 72 characters of input without processing the newline on the end!
Here is a prototype solution:
Here is a prototype solution:
#include < stdio.h >
#include < ctype.h >
int hash(char *s)
{
int i, h;
h = 1;
for(i=0; s[i] && isalpha(s[i]); i++)
h = ((s[i]-'A'+1)*h) % 47;
return h;
}
void
main(void)
{
FILE *in, *out;
char comet[100], group[100]; /* bigger than necessary, room for newline */
in = fopen("input.txt", "r");
out = fopen("output.txt", "w");
fgets(comet, sizeof comet, in);
fgets(group, sizeof group, in);
if(hash(comet) == hash(group))
fprintf(out, "GO\n");
else
fprintf(out, "STAY\n");
exit (0);
}
Solutions generally run in under 0.01 seconds.
This entry was posted on 2009/12/11 at 下午5:26. You can follow any responses to this entry through the RSS 2.0. You can leave a response.
- No comments yet.