USASO Section1.1Your Ride Is Here[其他]

2017.5.18

题目:

输入两个由大写字母组成的字符串,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
then calculate the product mod 47:
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值