P1200 [USACO1.1] Your Ride Is Here (Java)

Your Ride Is Here Your Ride Is Here

Topic link: https://www.luogu.com.cn/problem/P1200

Title description

As we all know, there is a UFO behind every comet. These UFOs often come to collect loyal supporters on the earth. Unfortunately, their UFO can only bring one group of supporters on each trip. Therefore, they have to use a clever plan to let these groups know in advance who will be taken away by the comet. They gave a name to each comet and used these names to determine whether the group was taken away from the specific group (who do you think gave these comets the name?). The details of how to match will be told to you below; your task is to write a program to determine whether the group can be taken away by the UFO behind the comet by the name of the group and the name of the comet.

Both the group name and the comet name are converted into a number in the following way: the final number is the product of all the letters in the name, where A is 1, and ZZ is 26. For example, the USACO team is 21×19×1×3×15=17955. If the group's digital mod47 is equal to the comet's digital mod47, you have to tell the group that it needs to be ready to be taken away! (Remember that "a mod b" is the remainder of dividing a by b; 34 mod 10 equals 4)

Write a program to read in the name of the comet and the group and figure out whether the two names can be matched with the above scheme. If it can be matched, output "GO", otherwise output "STAY". Both the group name and the comet name are a string of capital letters (not more than 6 letters) without spaces or punctuation.

Input format

Line 1: A string of capital letters with a length of 1 to 6, indicating the name of the comet.

Line 2: A string of capital letters with a length of 1 to 6, representing the name of the team.

Output format

no

Sample input and output

Enter #1

COMETQ
Hvngar

Output #1

GO

Input #2

ABSTAR
USACO

Output #2

STAY

Problem-solving ideas:

Convert the characters in the string object into a character array through toCharArray(), and then subtract 64 (ie -'A'+1) to convert English letters into corresponding serial numbers.

code show as below:

import java.util.Scanner;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		String a = sc.nextLine();
		char c[] = a.toCharArray();
		String b = sc.nextLine();
		char d[] = b.toCharArray();
		long ans = 1;
		for (char x : c) {
    
    
			ans *= x - 64;
		}
		long count = 1;
		for (char x : d) {
    
    
			count *= x - 64;
		}
		if (ans % 47 == count % 47)
			System.out.println("GO");
		else
			System.out.println("STAY");
	}
}

Guess you like

Origin blog.csdn.net/weixin_45894701/article/details/114680826