【华为OD机试真题 JAVA】火星文计算【2022 Q4 | 100分】

题目描述

【火星文计算】

已知火星人使用的运算符为#、$,其与地球人的等价公式如下:

x#y = 2*x+3*y+4

x$y = 3*x+y+2

1、其中x、y是无符号整数

2、地球人公式按C语言规则计算

3、火星人公式中,$的优先级高于#,相同的运算符,按从左到右的顺序计算

现有一段火星人的字符串报文,请你来翻译并计算结果。

输入描述

火星人字符串表达式(结尾不带回车换行)

输入的字符串说明:字符串为仅由无符号整数和操作符(#、$)组成的计算表达式。

例如:123#4$5#67$78。

  1. 用例保证字符串中,操作数与操作符之间没有任何分隔符。
  2. 用例保证操作数取值范围为32位无符号整数。
  3. 保证输入以及计算结果不会出现整型溢出。
  4. 保证输入的字符串为合法的求值报文,例如:123#4$5#67$78
  5. 保证不会出现非法的求值报文,例如类似这样字符串:

#4$5 //缺少操作数

4$5# //缺少操作数

4#$5 //缺少操作数

4 $5 //有空格

3+4-5*6/7 //有其它操作符

12345678987654321$54321 //32位整数计算溢出

输出描述

根据输入的火星人字符串输出计算结果(结尾不带回车换行)。

示例1 输入输出示例仅供调试,后台判题数据一般不包含示例

输入

7#6$5#12

输出

226

说明

示例

7#6$5#12

=7#(3*6+5+2)#12

=7#25#12

=(2*7+3*25+4)#12

=93#12

=2*93+3*12+4

=226

Java代码实现1:

import java.util.Scanner;

public class MarsTextCalculation {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String input = sc.nextLine();
            System.out.println(func(input));
        }
        sc.close();
    }

    private static int func(String input) {
        int index = input.lastIndexOf("#");
        if (index != -1) {
            String left = input.substring(0, index);
            String right = input.substring(index + 1);
            return 2 * func(left) + 3 * func(right) + 4;
        }
        index = input.lastIndexOf("$");
        if (index != -1) {
            String left = input.substring(0, index);
            String right = input.substring(index + 1);
            return 3 * func(left) + func(right) + 2;
        }
        return Integer.parseInt(input);
    }
}

Java代码实现2:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        while (input.contains("$") || input.contains("#")) {
            if (input.contains("$")) {
                int idx = input.lastIndexOf("$");
                int leftStart = changeInput(input, idx, -1);
                int op1 = Integer.parseInt(input.substring(leftStart, idx));
                int rightEnd = changeInput(input, idx, +1);
                int op2 = Integer.parseInt(input.substring(idx + 1, rightEnd));
                int res = calcStar('$', op1, op2);
                input = input.substring(0, leftStart) + res + input.substring(rightEnd);
            } else {
                if (input.contains("#")) {
                    int idx = input.indexOf("#", 0);
                    int leftStart = changeInput(input, idx, -1);
                    int op1 = Integer.parseInt(input.substring(leftStart, idx));
                    int rightEnd = changeInput(input, idx, +1);
                    int op2 = Integer.parseInt(input.substring(idx + 1, rightEnd));
                    int res = calcStar('#', op1, op2);
                    input = input.substring(0, leftStart) + res + input.substring(rightEnd);
                }
            }
        }
        System.out.println(input);
    }

    private static int changeInput(String input, int idx, int opt) {
        char ch = input.charAt(idx + opt);
        while (ch >= '0' && ch <= '9') {
            if (opt > 0) {
                opt++;
            } else {
                opt--;
            }
            if (idx + opt >= 0 && idx + opt < input.length()) {
                ch = input.charAt(idx + opt);
            } else {
                break;
            }
        }
        if (opt < 0) {
            return opt + idx + 1;
        } else {
            return opt + idx;
        }
    }

    private static int calcStar(char ch, int op1, int op2) {
        if (ch == '#') {
            return op1 * 2 + op2 * 3 + 4;
        } else {
            return 3 * op1 + op2 + 2;
        }
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值