时间限制: 2000ms
单点时限: 200ms
内存限制: 256MB

描述

Given N arithmetic expressions, can you tell whose result is closest to 9?

输入

Line 1: N (1 <= N <= 50000).
Line 2..N+1: Each line contains an expression in the format of "a op b" where a, b are integers (-10000 <= a, b <= 10000) and op is one of addition (+), subtraction (-), multiplication (*) and division (/). There is no "divided by zero" expression.

输出

The index of expression whose result is closest to 9. If there are more than one such expressions, output the smallest index.

样例输入

4
901 / 100
3 * 3
2 + 6
8 - -1
样例输出
2
我的代码,仅供参考:
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int count = input.nextInt();
        if (count < 1 || count > 50000)
            return;
        double[] result = new double[count];
        int i = 0;
        // System.out.print("count:"+count);
        while (input.hasNextLine()) {
            String line = input.nextLine();
            System.out.println(line);
            if (line.contains("*")) {
                String num[] = line.split("\\*");
                int a = Integer.parseInt(num[0]);
                int b = Integer.parseInt(num[1]);
                if (a < -10000 || a > 10000)
                    return;
                if (b < -10000 || b > 10000)
                    return;
                double result_temp = a * b;
                result[i - 1] = Math.abs(result_temp - 9.0);
            } else if (line.contains("/")) {
                String num[] = line.split("\\/");
                int a = Integer.parseInt(num[0]);
                int b = Integer.parseInt(num[1]);
                if (a < -10000 || a > 10000)
                    return;
                if (b < -10000 || b > 10000)
                    return;
                double result_temp = a / b;
                result[i - 1] = Math.abs(result_temp - 9.0);
            } else if (line.contains("+")) {
                String num[] = line.split("\\+");
                int a = Integer.parseInt(num[0]);
                int b = Integer.parseInt(num[1]);
                if (a < -10000 || a > 10000)
                    return;
                if (b < -10000 || b > 10000)
                    return;
                double result_temp = a + b;
                result[i - 1] = Math.abs(result_temp - 9.0);
            } else if (line.contains("-")) {
                int p = line.indexOf("-");
                if (p == 0) {
                    String temp_str = line;
                    temp_str = temp_str.substring(1);
                    // System.out.println(temp_str);
                    p = temp_str.indexOf("-") + 1;
                    String a_s = line.substring(0, p);
                    String b_s = line.substring(p + 1);
                    int a = Integer.parseInt(a_s);
                    int b = Integer.parseInt(b_s);
                    if (a < -10000 || a > 10000)
                        return;
                    if (b < -10000 || b > 10000)
                        return;
                    double result_temp = a - b;
                    result[i - 1] = Math.abs(result_temp - 9.0);
                } else {
                    String a_s = line.substring(0, p);
                    String b_s = line.substring(p + 1);
                    int a = Integer.parseInt(a_s);
                    int b = Integer.parseInt(b_s);
                    if (a < -10000 || a > 10000)
                        return;
                    if (b < -10000 || b > 10000)
                        return;
                    double result_temp = a - b;
                    result[i - 1] = Math.abs(result_temp - 9.0);
                }
            }
            if (i == count) {
                break;
            }
            i++;
        }
        int minIndex = 0;
        double min;
        min = result[0];
        for (int index = 1; index < result.length; index++) {
            if (result[index] < min) {
                min = result[index];
                minIndex = index;
            }
        }
        System.out.println(minIndex + 1);
    }
}

Arithmetic Expression相关推荐

  1. 微软编程一小时比赛--题目1 : Arithmetic Expression

    题目1 : Arithmetic Expression 时间限制:2000ms 单点时限:200ms 内存限制:256MB 描述 Given N arithmetic expressions, can ...

  2. C++11中nullptr的使用

    在C语言中,NULL实际上是一个void* 的指针,然后把void* 指针赋值给其它类型的指针的时候,会隐式转换成相应的类型.而如果用一个C++编译器来编译的时候是要出错的,因为C++是强类型的,vo ...

  3. LeetCode实战:逆波兰表达式求值

    题目英文 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  4. leetcode - 150. Evaluate Reverse Polish Notation

    前言:记录一下leetcode上的一道题目: 题目网址:https://leetcode.com/problems/evaluate-reverse-polish-notation/descripti ...

  5. ZOJ 3822 Known Notation(2014牡丹江Regional K题)

    题目大意:给你一个不完整的后缀表达式和两种操作,一是插入一个数或者"*"运算号,另一个是把数字和"*"交换. 一个*至少对应两个数字,而一个表达式又等于一个数字 ...

  6. NVelocity:语法

    User Guide - Contents About this Guide 什么是NVelocity? NVelocity能为我们做什么? The Mud Store example Velocit ...

  7. Python常用语句及流程控制

    1. 赋值语句常用技巧 通过序列解包(sequence unpacking)可以将多个值的序列解开,让后一一放置到变量的序列中.解包的序列中的元素必须和等号左边变量数量一致.如下: 1 values ...

  8. 用YACC/LEX 设计计算机语言

    用YACC/LEX 设计计算机语言 前言: YACC (Yet Another Compiler Compiler) 是1974年在 Unix 下设计出来的一个优秀的计算机语法分析工具.LEX 是相应 ...

  9. 12C OCP 1ZO-071 题库(8月以前)

    071-67q 1 choose the best answer 选择最好的答案 exanine the desciption of the employees table: 员工表的填写: whic ...

最新文章

  1. 64 DLL动态链接库换一台电脑就不能加载
  2. android 判断按钮是否已经有onclicklinstener,通过点击事件监听setOnClickListener彻底理解回调...
  3. 如何将外部邮箱添加至Exchange邮件组中
  4. .Net Core 2.2升级3.1的避坑指南
  5. Java 类加载总结
  6. sp | sp格式的空间矢量对象及其函数方法简介
  7. vs2013代码模板设置
  8. 基于机器学习的GitHub敏感信息泄露监控
  9. 相分离在聚集多价信号蛋白过程中的作用Phase transitions in the assembly of multivalent signalling proteins
  10. 开启MyBatis(三)工作原理
  11. python 和vba在财务上_Excel函数和VBA技术在财务工作中的应用
  12. dart语言(dart语言菜鸟教程)
  13. win+shift+s截图保存在哪_用惯了QQ/微信截图,你不知道的截图神器还有这个
  14. 我该如何拯救你,我的考研?
  15. c语言中矩形法求定积分
  16. Android Room,编译成功,倒是 build APK 失败提示: 无法访问:ActivityCompatApi23
  17. WordPress快速增加百度收录的方法
  18. informix数据库unload导出数据问题
  19. 不能管家的AI机器狗不是好宠物?
  20. api接口 php_PHP关于API接口实例分享

热门文章

  1. Logoist 4 for Mac(图标制作软件)
  2. 浅谈数据治理、数据管理、数据资源与数据资产管理内涵及差异点(建议收藏)...
  3. 不同游戏的不同反作弊引擎
  4. MEMS麦克风具备哪些优势?MEMS麦克风发展前景如何?
  5. 理解线性回归中的常数项
  6. 【STM32Cube】学习笔记(一):光敏传感器
  7. C语言 根据经纬度如何获取日落日出时间
  8. 微信公布文章阅读量 淘宝随即开发“新生意”
  9. 【Prompt】7 个向 chatGPT 高效提问的方法
  10. 龙芯处理器发展的思考