1. Ternary Calculation
  2.  
  3. Time Limit : /2000ms (Java/Other) Memory Limit : /65536K (Java/Other)
  4. Total Submission(s) : Accepted Submission(s) :
  5. Problem Description
  6. Complete the ternary calculation.
  7. Input
  8. There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
  9. There is a string in the form of "number1 operatora number2 operatorb number3". Each operator will be one of {'+', '-' , '*', '/', '%'}, and each number will be an integer in [, ].
  10. Output
  11. For each test case, output the answer.
  12. Sample Input
  13.  
  14. + *
  15. - /
  16. + -
  17. * /
  18. - %
  19. Sample Output
  20.  
  21. -
  22.  
  23. Note
  24. The calculation "A % B" means taking the remainder of A divided by B, and "A / B" means taking the quotient.
  1. package ACM1;
  2.  
  3. import java.text.DecimalFormat;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6.  
  7. public class nyojw2
  8. {
  9. public static void main(String[]args)
  10. {
  11. Scanner scanner = new Scanner(System.in);
  12. int n = scanner.nextInt();
  13. for(int i=0;i<n;i++)
  14. {
  15. int sum=0;
  16. int sum1 =0;
  17. int a = scanner.nextInt();
  18. String b=scanner.next();
  19.  
  20. // str=scanner.next();
  21. int c =scanner.nextInt();
  22. String d = scanner.next();
  23. int e = scanner.nextInt();
  24. if((b.equals("/"))||(b.equals("*"))||(b.equals("%")))
  25. {
  26. // sum1 = cal(a,b,c);
  27. sum = cal(cal(a,b,c),d,e);
  28. }
  29. else if((d.equals("/"))||(d.equals("*"))||(d.equals("%")))
  30. {
  31. // sum1 = cal(c,d,e);
  32. sum = cal(a,b,cal(c,d,e));
  33. }
  34. else
  35. {
  36. // sum1 = cal(a,b,c);
  37. sum=cal(cal(a,b,c),d,e);
  38. }
  39. // System.out.println(sum1);
  40. System.out.println(sum);
  41.  
  42. }
  43.  
  44. }
  45. public static int cal(int x,String y,int z)
  46. { // String y11 = new String();
  47. if(y.equals("/"))
  48. return (x/z);
  49. else if(y.equals("%"))
  50. return (x%z);
  51. else if(y.equals("*"))
  52. return (x*z);
  53. else if(y.equals("+"))
  54. return (x+z);
  55. else
  56. return (x-z);
  57.  
  58. }
  59. }

注意:在java里面要知道==和equals的区别:

需注意几点:

  1、string是一个特殊的引用类型。对于两个字符串的比较,不管是 == 和 Equals 这两者比较的都是字符串是否相同;

  2、当你创建两个string对象时,内存中的地址是不相同的,你可以赋相同的值。

    所以字符串的内容相同。引用地址不一定相同,(相同内容的对象地址不一定相同),但反过来却是肯定的;

  3、基本数据类型比较(string 除外) == 和 Equals 两者都是比较值;

Ternary Calculation的更多相关文章

  1. The 11th Zhejiang Provincial Collegiate Programming Contest->Problem G:G - Ternary Calculation

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3782 题意:把输入的三元运算用计算机运算出来. ;          ci ...

  2. ZOJ 3782 G - Ternary Calculation 水

    LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3782 题意:给出3个数和两个符号(+-*/%) 思路:拿到题目还 ...

  3. 2012-2014 三年浙江 acm 省赛 题目 分类

    The 9th Zhejiang Provincial Collegiate Programming Contest A    Taxi Fare    25.57% (166/649)     (水 ...

  4. [LeetCode] Ternary Expression Parser 三元表达式解析器

    Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...

  5. Ternary Expression Parser

    Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...

  6. OpenCASCADE Curve Length Calculation

    OpenCASCADE Curve Length Calculation eryar@163.com Abstract. The natural parametric equations of a c ...

  7. Leetcode: Ternary Expression Parser

    Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...

  8. 数据结构《17》---- 自动补齐之《二》----Ternary Search Tree

    一. 序言 上一篇文章中,给出了 trie 树的一个实现.可以看到,trie 树有一个巨大的弊病,内存占用过大. 本文给出另一种数据结构来解决上述问题---- Ternary Search Tree ...

  9. Ternary Search Trees 三分搜索树

    经常碰到要存一堆的string, 这个时候可以用hash tables, 虽然hash tables 查找很快,但是hash tables不能表现出字符串之间的联系.可以用binary search ...

随机推荐

  1. Cesium--气泡弹窗

    参考资料 首先感谢以下博主们的帮助,本人刚接触Cesium不久,无奈只能拾人牙慧了. 由于cesium没有自带的点击弹出气泡的功能,所以需要自己去开发一个这样的功能,网络上资源很多,看到基本思路都一致 ...

  2. scrollview gridview

    package com.fangdamai.salewinner.ui.customer; import android.content.Context;import android.content. ...

  3. eclipse tomcat maven

    jdk jre eclipse 略过 下载maven和tomcat 上apache官网下载maven:http://maven.apache.org/download.cgi. 上apache官网下载 ...

  4. pycharm连git和gitee

    http://www.cnblogs.com/feixuelove1009/p/5955332.html https://www.58jb.com/html/171.html

  5. x86 寻址学习

    x86 寻址方式众多,什么直接寻址.间接寻址.基址寻址.基址变址寻址等等让人眼花缭乱,而 AT&T 语法对内存寻址方式做了一个很好的统一,其格式为 section:displacement(b ...

  6. webpack热替换原理

    前期准备: const path = require('path'); const HtmlWebpackPlugin= require('html-webpack-plugin'); const C ...

  7. CodeForces - 540B School Marks —— 贪心

    题目链接:https://vjudge.net/contest/226823#problem/B Little Vova studies programming in an elite school. ...

  8. 1--单独使用jdbc开发问题总结

    1.数据库连接,使用时就创建,不使用立即释放,对数据库进行频繁连接开启和关闭,造成数据库资源浪费,影响 数据库性能. 设想:使用数据库连接池管理数据库连接. 2.将sql语句硬编码到java代码中,如 ...

  9. freemaker开发

    推荐书籍 百度云盘 密码: c3m9 1. 前言 本书为<FreeMarker 2.3.19 中文版手册>,包含了freemarker开发得方方面面,可以作为开发freemarker的字典 ...

  10. 检测UTF-8编码

    在PHP检测字符串是否是UTF-8编码的时候,很多人在使用mb_detect_encoding的时候,经常遇到检测不准的问题,下面的方法可以准确检测编码是否是UTF-8 function check_ ...