Ternary Calculation

Time Limit : /2000ms (Java/Other)   Memory Limit : /65536K (Java/Other)
Total Submission(s) : Accepted Submission(s) :
Problem Description
Complete the ternary calculation.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
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 [, ].
Output
For each test case, output the answer.
Sample Input + *
- /
+ -
* /
- %
Sample Output - Note
The calculation "A % B" means taking the remainder of A divided by B, and "A / B" means taking the quotient.
package ACM1;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner; public class nyojw2
{
public static void main(String[]args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i=0;i<n;i++)
{
int sum=0;
int sum1 =0;
int a = scanner.nextInt();
String b=scanner.next(); // str=scanner.next();
int c =scanner.nextInt();
String d = scanner.next();
int e = scanner.nextInt();
if((b.equals("/"))||(b.equals("*"))||(b.equals("%")))
{
// sum1 = cal(a,b,c);
sum = cal(cal(a,b,c),d,e);
}
else if((d.equals("/"))||(d.equals("*"))||(d.equals("%")))
{
// sum1 = cal(c,d,e);
sum = cal(a,b,cal(c,d,e));
}
else
{
// sum1 = cal(a,b,c);
sum=cal(cal(a,b,c),d,e);
}
// System.out.println(sum1);
System.out.println(sum); } }
public static int cal(int x,String y,int z)
{ // String y11 = new String();
if(y.equals("/"))
return (x/z);
else if(y.equals("%"))
return (x%z);
else if(y.equals("*"))
return (x*z);
else if(y.equals("+"))
return (x+z);
else
return (x-z); }
}

注意:在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. JavaScript框架——jquery

    1.jQuery编程常识   ————————如何进行jQuery插件开发   2.五星评分——jQuery Raty 一个很棒的jQuery评分插件—jQuery Raty         3.能感 ...

  2. Notepad++集成Subversion SVN插件

    点击Plugin –> Plugin Manager –> Show Plugin Manager 打开后,在“Available”页找到“Subversion”,然后点击“Install ...

  3. shell 获取当前svn代码目录版本号

    在当前svn代码目录下执行以下命令: svn info | grep "Last Changed Rev:" | awk -F ': ' '{print $2}' > svn ...

  4. T-SQL简单查询语句(模糊查询)

    T-SQL简单查询语句 简单查询: 1.最简单查询(查所有数据) select * from 表名: 注:* 代表所有列 select * from info 2.查询指定列 select code, ...

  5. PHP数据类型转换和运算符表达式

    一:数据类型的转换 获取类型: gettype($a); 判断是否是某种类型的数据: is_类型名($a); 1.(int)$a; 2.settype($a,int); 二:运算符表达式 1.数学运算 ...

  6. HDU 1853 Cyclic Tour[有向环最小权值覆盖]

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  7. 基于live555实现的RTSPServer对底层进行性能优化的方法

    在博客<EasyIPCamera高性能摄像机RTSP服务器RTSPServer解决方案>我介绍了基于live555实现的一套RTSPServer功能组件,当时开发者经过几个月的调试,已经将 ...

  8. 九度OJ 1006:ZOJ问题 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:18621 解决:3197 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下: 1. ...

  9. php异步任务处理: gearman

    Gearman是一个用来把工作委派给其他机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡 准备软件包 gearmand-1.1.12.tar.gz gearman-1. ...

  10. HTML 学习笔记 JQuery(事件)

    加载DOM 以浏览器加载文档为例,在页面加载完毕后,浏览器会通过JavaScript为DOM元素添加事件.在常规的JavaScript中,通常使用window.onload方法,在JQuery中通常使 ...