Old Calculator
时间限制:1000 ms  |  内存限制:65535 KB
难度:1
描述
szhhck have an old calculator bought 5 years ago.he find the old machine can just calculate expressions like this  :

A-B、A+B、A*B、A/B、A%B.

because it is too old and long time not use,the old machine maybe conclude a wrong answer sometime.

Your task is to write a program to check the answer the old calculator calculates is correct or not.

输入
First input is a single line,it's N and stands for there are N test cases.then there are N lines for N cases,each line contain an equation like A op B = C(A,B and C are all integers,and op can only be + , - , * , / or % ).
More details in the Sample Input.
输出
For each test case,if the equation is illegal(divided or mod by zero),you should Output "Input Error".and if the equation is correct,Output "Accept";if not Output "Wrong Answer",and print the right answer after a blank line.
样例输入
5
1+2=32
2-3=-1
4*5=20
6/0=122
8%9=0
样例输出
Wrong Answer
3
Accept
Accept
Input Error
Wrong Answer
8

#include <stdio.h>
int main()
{int n;
 scanf("%d",&n);
  while(n--)
  {   int a,b,c;
      char s,t;
   scanf("%d%c%d%c%d",&a,&s,&b,&t,&c);
   if(s=='/')
   if(b==0)
   printf("Input Error\n");
   else if(a/b==c)
   printf("Accept\n");
   else
   {
   printf("Wrong Answer\n");
   printf("%d\n",a/b);
   }
   if(s=='+')
   if(a+b==c)
   printf("Accept\n");
   else
   {
   printf("Wrong Answer\n");
   printf("%d\n",a+b);
   }
   if(s=='-')
   if(a-b==c)
   printf("Accept\n");
   else
   {
   printf("Wrong Answer\n");
   printf("%d\n",a-b);
   }
   if(s=='*')
   if(a*b==c)
   printf("Accept\n");
   else
   {
   printf("Wrong Answer\n");
   printf("%d\n",a*b);
   }
   if(s=='%')
   if(a%b==c)
   printf("Accept\n");
   else
   {
   printf("Wrong Answer\n");
   printf("%d\n",a%b);
   }
  }
 return 0;  
}

关于C的取余运算‘%’  http://hi.baidu.com/d_life/item/6c05f60fb2bf62143b53ee24

关于C的取余运算‘%’我们对C的%运算知多少呢?
当是正整数时,可能大家都知道。例如:5%3等于2, 3%5等于3。
当存在负数时呢?先看看例子:
例一:
int main()
{

int x;
     x = -6%5; printf("%2d\n",x);  
     x = 6%-5; printf("%2d\n",x);  
     x = 1%-5; printf("%2d\n",x);  
     x = -1%-5; printf("%2d\n",x);
     x = -6%-5; printf("%2d\n",x);  
}
运行结果为:
-1
1
1
-1
-1
例二:
#include <stdio.h>
int main()

{

int x;
     x = 5%-6; printf("%2d\n",x);  
     x = -5%6; printf("%2d\n",x);  
     x = 4%5;   printf("%2d\n",x);  
     x = -4%-5; printf("%2d\n",x);
     x = -5%-6; printf("%2d\n",x);  
}
运行结果为:
5
-5
4
-4
-5

你看出规律了吗?我帮你总结一下:
余数的定义:当被除数不够整除时余下的数。

当都是正整数时:
除法实际可转化为减数,不够减时剩下的就是余数。
例如:12%5
           12-5-5
            2
当存在负数时: x%y
   i. 当异号时:
                 if |x|>|y|
                    result: x+y
                 else
                    result: x
             例:
                -6% 5等于-1
                 6%-5等于 1
                 5%-6等于 5
                -5% 6等于 -5
   ii. 当同号时:
                if |x|>|y|
                    result: x-y
                 else
                    result: x
             例:
               -1%-5等于-1
                -6%-5等于-1
                -4%-5等于-4
                -5%-6等于-5
   相信当你记住这个规律后,再遇到这种问题,你不用思考就可以回答出来。
但你一定不会满意,因为这不是你想要的结果,你一定觉得还有更深层的
原因。如果你感兴趣,请接着看:
例三:
#include <stdio.h>
int main()
{

int x;
     x = -6/5; printf("%2d\n",x);  
     x = 6/-5; printf("%2d\n",x);  
     x = 1/-5; printf("%2d\n",x);  
     x = -1/-5; printf("%2d\n",x);
     x = -6/-5; printf("%2d\n",x);  
}
运行结果:
-1
-1
0
0
1
例四:
#include <stdio.h>
int main()
{

int x;
     x = 5/-6; printf("%2d\n",x);  
     x = -5/6; printf("%2d\n",x);  
     x = 4/5;   printf("%2d\n",x);  
     x = -4/-5; printf("%2d\n",x);
     x = -5/-6; printf("%2d\n",x);  
}
运行结果:
0
0
0
0
0
   这两个例子我想大家都觉得很简单,但简单并不代表它没价值,
特别是它和其它事物联系其来时你才会注意到。
“/”在我们这些程序中代表整除,它符合除法法则,异号抵消。
再看看我们余数的定义:
                   整除“余”下的“数”。
则有:余数=被除数-商*除数
商就是我们整除的结果。
看例子:
eg1:
        (-6%5) = -6 - (-6/5)*5
        (-6%5) = -6 - (-1)*5
        (-6%5) = -6 - (-5)
        (-6%5) = -6+5
        (-6%5) = -1
eg2:
        (5%-6) =   5 - (5/-6)*(-6)
        (5%-6) =   5 - (0)*(-6)
        (5%-6) =   5 - 0
        (5%-6) =   5
eg3:
        (-5%-6)= -5 - (-5/-6)*(-6)
        (-5%-6)= -5 - (0)*(-6)
        (-5%-6)= -5 - 0
        (-5%-6)= -5
eg4:
        (6%-5) =   6 - (6/-5)*(-5)
        (6%-5) =   6 - (-1)*(-5)
        (6%-5) =   6 - 5
        (6%-5) =   1
到现在为止,你还有什么疑惑?
但我还是有点不明白,这是数学中的定义吗?
我查了一下《Concrete Mathematics》,请看原文:
摘之 P82
------------------
3.4 ‘MOD': THE BINARY OPERATION
     The quotient of n divided by m is [n/m],when m and n are positive
integers. It's handy to have a simple notation also for the remainder
of this division, and we call it 'n mod m', The basic formula
     n = m[n/m]+ n mod m
//NOTE:"m[n/m]" is quotient, "n mod m" is remainder
tells us that we can express n mod m as n-m[n/m] .We can generalize this
to megative integers, and in fact to arbitrary real numbers:
     x mod y = x - y[x/y], for y!=0.
--------------------
从文中可能看出,数学中的 余数(remainder) 其实就是 取模(mod),即:
     x mod y = x%y
     x%y     = x - y[x/y], for y!=0.
数学中的余数概念和我们的计算机中的余数概念一致,但实现却不一致。
其中 [x/y] 代表的是 x/y 的最小下界。
例:
     -3 mod 2         = -3 - 2*[-3/2]
                            = -3 - 2*[-1.5]
                            = -3 - 2*(-2)
                            = -3 + 4
                            = 1
而我们的计算机是怎么做的呢:
             -3%2        = -3 - 2*(-3/2)
                             = -3 - 2*(-1)
                              = -3 - (-2)
                              = -1
所以计算机中的取余实际上是:
       x%y = x - y(x/y), for y!=0.
   这就是二者的区别。这个区别,对于正数,二者计算出的结果是相等的,但是负数就不相等了。这就意味着,如果以后在使用数学中余数相关定理的时候,要注意计算机中余数的计算和数学定义不是完全一致的,所以在计算机上,对于负数,数学定理并不完全适用。当然,对于正数,二者是没有区别的。至于为什么计算机上要这么实现,我想恐怕还是历史原因,最早的计算机如果这样计算除法(取余是靠除法来完成的),那么就涉及到浮点数的计算以及取下界,这样,将比较大的降低效率,所以实现成了这样的方式,一直沿用至今。

【ACM】NYOJ_486_Old Calculator_20130725的更多相关文章

  1. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  2. 【ACM】HDU1008 Elevator 新手题前后不同的代码版本

    [前言] 很久没有纯粹的写写小代码,偶然想起要回炉再来,就去HDU随便选了个最基础的题,也不记得曾经AC过:最后吃惊的发现,思路完全不一样了,代码风格啥的也有不小的变化.希望是成长了一点点吧.后面定期 ...

  3. 【ACM】魔方十一题

    0. 前言打了两年的百度之星,都没进决赛.我最大的感受就是还是太弱,总结起来就是:人弱就要多做题,人傻就要多做题.题目还是按照分类做可能效果比较好,因此,就有了做几个系列的计划.这是系列中的第一个,解 ...

  4. 【ACM】那些年,我们挖(WA)过的最短路

    不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...

  5. 【ACM】不要62 (数位DP)

    题目:http://acm.acmcoder.com/showproblem.php?pid=2089 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新 ...

  6. 【Acm】八皇后问题

    八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题. 其解决办法和我以前发过的[算法之美—Fire Net:www.cnblogs.com/lcw/p/3159414.html]类似 题目:在8 ...

  7. 【ACM】hud1166 敌兵布阵(线段树)

    经验: cout 特别慢 如果要求速度 全部用 printf !!! 在学习线段树 内容来自:http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/24 ...

  8. 【acm】杀人游戏(hdu2211)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2211 杀人游戏 Time Limit: 3000/1000 MS (Java/Others)    M ...

  9. 【ACM】How many prime numbers

    http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=2 #inclu ...

随机推荐

  1. E20170626-gg

    occupy   vt. 占领; 使用,住在…; 使从事,使忙碌; 任职; stack   n. 垛,干草堆; (一排) 烟囱; 层积; 整个的藏书架排列;

  2. js返回上一层

    Javascript 返回上一页 1. Javascript 返回上一页 history.go(-1), 返回两个页面: history.go(-2); 2. history.back(). wind ...

  3. [Swift通天遁地]二、表格表单-(13)实时调整表单元素的显示和隐藏

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. python自动化测试学习笔记-4内置函数,处理json

    函数.全局变量 写代码时注意的几点事项: 1.一般写代码的时候尽量少用或不用全局变量,首先全局变量不安全,大家协作的情况下,代码公用容易被篡改,其次全局变量会一直占用系统内容. 2.函数里如果有多个r ...

  5. mybatis 中 foreach 的性能问题及调优

    1.mybatis中最初的sql语句 SELECT 参数1, 参数2, 参数3 FROM 表 WHERE 条件参数1 in <foreach item="item" inde ...

  6. “浪潮杯”第九届山东省ACM大学生程序设计竞赛重现赛 C-Cities

    题目描述:There are n cities in Byteland, and the ith city has a value ai. The cost of building a bidirec ...

  7. EF在应用程序配置文件中找不到名为“XXX”的连接字符串。

    现象: 在配置EF的时候需要如题所述的问题,仔细检查了在EF实体模型对应程序集下的APP.Config文件中的ConnectionString配置项有了XXX项的数据库字符串的配置: <conn ...

  8. [ SHOI 2012 ] 随机树

    \(\\\) \(Description\) 开始有一棵只有一个根节点的树.每次随机选择一个叶子节点,为他添上左右子节点,求: 生成一棵有\(N\)个叶节点的树,所有叶节点平均高度的期望. 生成一棵有 ...

  9. JSP中如何利用JS实现登录页面的跳转(JSP中如何利用JS实现跳转页面)

    <%!          <%                               url =              word =          }             ...

  10. [Windows Server 2003] IIS自带FTP安装及配置方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:IIS6.0自 ...