# Thread: I wanna solve this problem

1. ## I wanna solve this problem

Seriously, I'm having an exam after 2 hours and 20 minutes so I'm training. I'm trying to solve this problem for days but I can't

It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do, however, let the groups know ahead of time which will be picked up for each comet by a clever scheme: they pick a name for the comet which, along with the name of the group, can be used to determine if it is a particular group's turn to go (who do you think names the comets?). The details of the matching scheme are given below; your job is to write a program which takes the names of a group and a comet and then determines whether the group should go with the UFO behind that comet.
Both the name of the group and the name of the comet are converted into a number in the following manner: the final number is just the product of all the letters in the name, where "A" is 1 and "Z" is 26. For instance, the group "USACO" would be 21 * 19 * 1 * 3 * 15 = 17955. If the group's number mod 47 is the same as the comet's number mod 47, then you need to tell the group to get ready! (Remember that "a mod b" is the remainder left over after dividing a by b; 34 mod 10 is 4.)
Write a program which reads in the name of the comet and the name of the group and figures out whether according to the above scheme the names are a match, printing "GO" if they match and "STAY" if not. The names of the groups and the comets will be a string of capital letters with no spaces or punctuation, up to 6 characters long.
Examples:

 Input Output COMETQ HVNGAT GO ABSTAR USACO STAY
PROGRAM NAME: ride

PROG: ride INPUT FORMAT

 Line 1: An upper case character string of length 1..6 that is the name of the comet. Line 2: An upper case character string of length 1..6 that is the name of the group.
NOTE: The input file has a newline at the end of each line but does not have a "return". Sometimes, programmers code for the Windows paradigm of "return" followed by "newline"; don't do that! Use simple input routines like "readln" (for Pascal) and, for C/C++, "fscanf" and "fid>>string".
SAMPLE INPUT (file ride.in)

COMETQ HVNGAT OUTPUT FORMAT

A single line containing either the word "GO" or the word "STAY". SAMPLE OUTPUT (file ride.out)

GO

Submission file Name: USACO Gateway | Comment or Question
Here's my code

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *in = fopen("ride.in","r");
FILE *out = fopen("ride.out","w");
int i,flag=0,fin1[6],fin2[6],fin2f=0;
char fin[12];
fscanf(in,"%s",&fin);
for (i=0;i<12;i++)
{
printf("Hello");
if(flag!=1)
{
if(fin[i]!='\n')
{
fin1[i]=(fin[i]-46);
}
else
{
flag=1;
}
}
else
{
fin2[fin2f]=(fin[i]-46);
fin2f++;
}
}
printf("\n%d",fin1);
printf("\n%d",fin2);
return 0;
}
It isn't finished yet

But even that doesn't work

2. What part can't you get to work? You don't need the & when you are scanning into a character array as a whole:
Code:
char fin[12];
fscanf(in,"%s",&fin);
This is only going to give you 5 useable characters. %s isn't going to stop after 5 characters on its own though. You need to limit it, or hope you've got some white space after the fifth character in your file.

Quzah.

3. Do you know own functions in C?
You should calculate the product with a function like
Code:
unsigned long myproduct(const char *s)
{
unsigned long p=1;
while( s[0] ) { p = p * (s[0]-'A'+1); ++s; };
return p;
}
Then you can read your names with one fscanf upto end of file like
Code:
char comet[7],group[7]; /* note 7 chars needed ; terminating '\0' */
...
while( 2==fscanf(in,"%6s%6s",comet,group) )
if( (myproduct(comet)%47) == (myproduct(group)%47) )
puts("GO");
else
puts("STAY");
Now its easy or?

4. @quzah, strlen? o.o

@BillyTKid, I can't understand anything of what you've written