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. 知识复习(LDT+TSS+GATE+INTERRUPT)

    [1]README 1.0)由于实现进程的切换任务,其功能涉及到 LDT + TSS +GATE + INTERRUPT:下面我们对这些内容进行复习: 1.1) source code from or ...

  2. 目标跟踪之camshift---opencv中meanshift和camshift例子的应用

    在这一节中,主要讲目标跟踪的一个重要的算法Camshift,因为它是连续自使用的meanShift,所以这2个函数opencv中都有,且都很重要.为了让大家先达到一个感性认识.这节主要是看懂和运行op ...

  3. 【Atheros】如何禁用载波侦听CSMA和BACKOFF

    无线网络上发包之前都要进行载波侦听,如果未收到ACK,就指数级退避之后重传. 有时候我们为了观察网卡在全力发送数据包时的性能,要禁用CSMA和ACK,这一篇文章先来讨论怎么禁用CSMA,这里提供两种思 ...

  4. IT痴汉的工作现状10-Sprint Planning

    这是我们的第四个Sprint了.因为上一个迭代周期的失利,Leader群发邮件这样描写叙述道:"对任务的乐观预计,导致Sprint 3没有如期完毕. 我们须要在这次Sprint计划中细致评估 ...

  5. 【BZOJ3696】化合物 树形DP+暴力

    [BZOJ3696]化合物 Description 首长NOI惨跪,于是去念文化课了.现在,他面对一道化学题.    这题的来源是因为在一个奇怪的学校两个化竞党在玩一个奇怪的博弈论游戏.这个游戏很蛋疼 ...

  6. 流畅python学习笔记:第二十章:属性描述符:

    在前面一章中介绍了@property的用法,但是存在一个问题,如果我有多个属性想转变成property特性,那不是针对每个都需要实现一个 @propery.setter 和 @property.get ...

  7. Java基础之I/O流

    一.数据流的基本概念 数据流是一串连续不断的数据的集合,就象水管里的水流,在水管的一端一点一点地供水,而在水管的另一端看到的是一股连续不断的水流.数据写入程序可以是一段.一段地向数据流管道中写入数据, ...

  8. PAT 天梯赛 L3-008. 喊山 【BFS】

    题目链接 https://www.patest.cn/contests/gplt/L3-008 思路 因为 每个山头 最多有两个 能听到它的 临近山头 那么 我们就可以 给每个 山头 都 分配 最多两 ...

  9. springboot 默认tomcat配置

    1. Spring Boot 能支持的最大并发量主要看其对Tomcat的设置,可以在配置文件中对其进行更改.当在配置文件中敲出max后提示值就是它的默认值. 我们可以看到默认设置中,Tomcat的最大 ...

  10. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...