题目:
输入两个由大写字母组成的字符串,A~Z分别转换为1~26,然后将其累乘,得到的两个乘积如果mod 47后相等则输出GO,否则输出STAY
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
OUTPUT EXPLANATION
Converting the letters to numbers:C | O | M | E | T | Q | |
3 | 15 | 13 | 5 | 20 | 17 | |
H | V | N | G | A | T | |
8 | 22 | 14 | 7 | 1 | 20 |
3 * 15 * 13 * 5 * 20 * 17 = 994500 mod 47 = 27 8 * 22 * 14 * 7 * 1 * 20 = 344960 mod 47 = 27
Because both products evaluate to 27 (when modded by 47), the mission is 'GO'.
题解:
乘积的取余等于取余的乘积,由此可以避免数据溢出。注意ASCII码和字符之间的转换,表内字符可以直接作为整数来计算,减去64即可。
代码:
/*
ID: xcwhkh1
LANG: C
TASK: ride
*/
#include <stdio.h>
char a[100002];
char b[100002];
int main () {
FILE *fin = fopen ("ride.in", "r");
FILE *fout = fopen ("ride.out", "w");
fscanf (fin, "%s\n%s",a,b); /* the two input integers */
int sa=1,sb=1,i;
for(i=0;a[i]!='\0';i++)
sa=sa*(a[i]-64)%47;
for(i=0;b[i]!='\0';i++)
sb=sb*(b[i]-64)%47;
if(sa!=sb)
fprintf (fout,"STAY\n");
else
fprintf (fout,"GO\n");
return 0;
}
ID: xcwhkh1
LANG: C
TASK: ride
*/
#include <stdio.h>
char a[100002];
char b[100002];
int main () {
FILE *fin = fopen ("ride.in", "r");
FILE *fout = fopen ("ride.out", "w");
fscanf (fin, "%s\n%s",a,b); /* the two input integers */
int sa=1,sb=1,i;
for(i=0;a[i]!='\0';i++)
sa=sa*(a[i]-64)%47;
for(i=0;b[i]!='\0';i++)
sb=sb*(b[i]-64)%47;
if(sa!=sb)
fprintf (fout,"STAY\n");
else
fprintf (fout,"GO\n");
return 0;
}