【英文原題】api
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.ide
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.)this
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.lua
Examples:spa
Input | Output |
COMETQ HVNGAT |
GO |
ABSTAR USACO |
STAY |
This means that you fill in your header with:
PROG: ride
WARNING: You must have 'ride' in this field or the wrong test data (or no test data) will be used.翻譯
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". code
NOTE 2: Because of the extra characters, be sure to leave enough room for a 'newline' (also notated as '\n') and an end of string character ('\0') if your language uses it (as C and C++ do). This means you need eight characters of room instead of six. blog
COMETQ HVNGAT
A single line containing either the word "GO" or the word "STAY".ip
GO
Converting the letters to numbers:rem
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'.
【中文翻譯】
衆所周知,在每個彗星後都有一隻UFO。這些UFO時常來收集地球上的忠誠支持者。不幸的是,他們的飛碟每次出行都只能帶上一組支持者。所以,他們要用一種聰明的方案讓這些小組提早知道誰會被彗星帶走。他們爲每一個彗星起了一個名字,經過這些名字來決定這個小組是否是被帶走的那個特定的小組(你認爲是誰給這些彗星取的名字呢?)。關於如何搭配的細節會在下面告訴你;你的任務是寫一個程序,經過小組名和彗星名來決定這個小組是否能被那顆彗星後面的UFO帶走。
小組名和彗星名都如下列方式轉換成一個數字:最終的數字就是名字中全部字母的積,其中「A」是1,「Z」是26。例如,「USACO」小組就是21*19*1*3*15=17955。若是小組的數字 mod 47等於彗星的數字mod 47,你就得告訴這個小組須要準備好被帶走!(記住「a mod b」是a除以b的餘數;34 mod 10等於4)
寫出一個程序,讀入彗星名和小組名並算出用上面的方案可否將兩個名字搭配起來,若是能搭配,就輸出「GO」,不然輸出「STAY」。小組名和彗星名均是沒有空格或標點的一串大寫字母(不超過6個字母)。
輸入格式:
第1行:一個長度爲1到6的大寫字母串,表示彗星的名字。
第2行:一個長度爲1到6的大寫字母串,表示隊伍的名字。
輸出格式:
COMETQ HVNGAT
GO
ABSTAR USACO
STAY
思路:爲了知足題意,首先要將兩個字符串轉換爲數字,而後兩個字符串分別對47取餘,判斷取餘後兩數是否相等,相等輸出GO,不相等輸出STAY。
代碼以下:
1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 char a[3000],s[3000]; 6 int a1,s1,a2=1,s2=1; 7 int i; 8 scanf("%s%s",a,s); 9 a1=strlen(a);//測字符串a長度 10 s1=strlen(s);//測字符串s長度 11 for(i=0;i<a1;i++) a2*=a[i]-'A'+1;//轉換成數字a2 12 for(i=0;i<s1;i++) s2*=s[i]-'A'+1;//轉換成數字s2 13 if(a2%47==s2%47)//取餘後相等,輸出GO 14 printf("GO\n"); 15 else printf("STAY\n");//不然輸出STAY 16 return 0; 17 }