USACO Training Section 1.1——Your Ride Is Here

原题:
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.

翻译:

众所周知,在每一个彗星后都有一只 UFO。这些 UFO 时常来收集地球上的忠诚支持者。不幸的是,他们的飞碟每次出行都只能带上一组支持者。因此,他们要用一种聪明的方案让这些小组提前知道谁会被彗星带走。他们为每个彗星起了一个名字,通过这些名字来决定这个小组是不是被带走的那个特定的小组(你认为是谁给这些彗星取的名字呢?)。关于如何搭配的细节会在下面告诉你;你的任务是写一个程序,通过小组名和彗星名来决定这个小组是否能被那颗彗星后面的 UFO 带走。
小组名和彗星名都以下列方式转换成一个数字:最终的数字就是名字中所有字母的积,其中 A 是 1,Z 是 26。例如,USACO 小组就是 21×19×1×3×15=17955。如果小组的数字 mod47 等于彗星的数字 mod47,你就得告诉这个小组需要准备好被带走!(记住“amodb”是 a 除以 b 的余数,例如 34mod10 等于 4)
写出一个程序,读入彗星名和小组名并算出用上面的方案能否将两个名字搭配起来,如果能搭配,就输出 GO,否则输出 STAY。小组名和彗星名均是没有空格或标点的一串大写字母(不超过 6 个字母)。

输入格式

第1行:一个长度为 1 到 6 的大写字母串,表示彗星的名字。

第2行:一个长度为 1 到 6 的大写字母串,表示队伍的名字。

输出格式

输入输出样例

in:
COMETQ
HVNGAT
out:
GO

in:
ABSTAR
USACO
out:
STAY

比较适合萌新的代码。

个人认为这题主要考察强制类型转换吧。

值得注意的是,定义两个字符串后用getline输入会方便许多。

上代码解释!

#include<bits/stdc++.h>//万能头
using namespace std;
int main()
{
    string a,b;//STL中的字符串
    int ans=1,num=1;//ans代表第一个字符串所对应的数字
               //num代表第二个字符串所对应的数字
    //使用getline对a、b进行输入,getline在读取的时候读到回车会结束。
    getline(cin,a);
    getline(cin,b);
    //下面用两个循环进行统计字符串所对应的数字。
    for(int i=0;i<a.size();i++)//"a.size()"是对应字符串a的长度
        ans*=(a[i]-64);//大写字母-64,自动转化为整型,并且大写字母-64就是其对应的字母的编号。
    for(int i=0;i<b.size();i++)
		num*=(b[i]-64);
    if(ans%47==num%47)cout<<"GO";//按照题意输出。
    else cout<<"STAY";
    return 0;
}

给个赞或打赏一下吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值