题目地址:https://train.usaco.org/usacoprob2?a=g6aKKOrpotS&S=ride。估计这个网页需要登录才能看到。
我自己的OJ上题目链接是http://47.110.135.197/problem.php?id=4362。
题意
就是输入两个不超过 6 长度的全部由大写字符构成字符串,然后按照某种规定(A变1,B变2,...,Z变26),计算两个字符串的数值(乘积)。如果两个数值相等,输出 GO;如果数值不等,输出 STAY。
题解
本题是一道水题。
数学
好吧。水题没什么大数学,就是:
1、大写字符如何变数字。假设 ch 为对应字符,哪么对应数字就是 ch - 'A' + 1。注意从 1 开始。
2、如何求累乘。数据初始值必须设置为 1,而不能为 0。
结果范围
不超过 6 个长度,全部大写。按照要求,进行乘积。哪么可知,最大的字符串结果应该为 ZZZZZZ,计算一下,也就是 26 * 26 * 26 * 26 * 26 * 26 = 308,915,776。如果不会笔算,写个简单程序。也就是说这个乘积结果不会超过 int 范围。
坑点
本提唯一的坑点就是:长度不超过6个。注意是不超过,也就是说可能小于或者等于6个。
AC代码
/*对于第一次在USACO刷题,难度在于USACO规定的提交格式。具体题目格式要求参考下面。*/
/* Use the slash-star style comments or the system won't see your
identification information */
/*
ID: your_id_here
PROG: ride
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
FILE *fin = fopen ("ride.in", "r");
FILE *fout = fopen ("ride.out", "w");
char str1[8] = {};
unsigned long long ans1 = 1;
char str2[8] = {};
unsigned long long ans2 = 1;
fscanf(fin, "%s %s", &str1, &str2);
//fprintf (fout, "%s\n%s\n", str1, str2);
int i;
for (i=0; str1[i]!=0; i++) {
ans1 *= (str1[i]-'A'+1);
}
for (i=0; str2[i]!=0; i++) {
ans2 *= (str2[i]-'A'+1);
}
if (ans1%47 == ans2%47) {
fprintf (fout, "GO\n");
} else {
fprintf (fout, "STAY\n");
}
return 0;
}