P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here (Java)
发布日期:2021-05-08 22:12:20 浏览次数:18 分类:精选文章

本文共 981 字,大约阅读时间需要 3 分钟。

题目要求我们编写一个程序,判断两个字符串是否能根据特定规则搭配。我们需要将每个字符串中的字母转换为数字,计算乘积并进行模运算,最后判断结果是否一致。

1. 字符转数字

将每个字母转换为对应的数字,A=1,B=2,...,Z=26。即字符的ASCII码减去'Z'的ASCII码再加1。

2. 乘积计算

计算每个字符串转换后的数字之积。

3. 模运算

对乘积分别取47的模,比较结果是否相同。

4. 条件判断

如果模运算结果相同,输出“GO”,否则输出“STAY”。

以下是代码:

import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        String comet = sc.nextLine().toUpperCase();        String group = sc.nextLine().toUpperCase();                long productComet = 1;        for (int i = 0; i < comet.length(); i++) {            char c = comet.charAt(i);            productComet *= (c - 'A' + 1);        }        long productGroup = 1;        for (int i = 0; i < group.length(); i++) {            char c = group.charAt(i);            productGroup *= (c - 'A' + 1);        }        if ((productComet % 47) == (productGroup % 47)) {            System.out.println("GO");        } else {            System.out.println("STAY");        }    }}
上一篇:HDU1559(二维前缀和模板 Java&C++)
下一篇:P1271 【深基9.例1】选举学生会 (Java & C++)

发表评论

最新留言

不错!
[***.144.177.141]2025年04月05日 11时02分39秒