A-B Problem

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述

A+B问题早已经被大家所熟知了,是不是很无聊呢?现在大家来做一下A-B吧。

现在有两个实数A和B,聪明的你,能不能判断出A-B的值是否等于0呢?

 
输入
有多组测试数据。每组数据包括两行,分别代表A和B。
它们的位数小于100,且每个数字前中可能包含+,- 号。
每个数字前面和后面都可能有多余的0。
每组测试数据后有一空行。
输出
对于每组数据,输出一行。
如果A-B=0,输出YES,否则输出NO。
样例输入
1
1

1.0
2.0
样例输出
YES
NO

#include <iostream>
#include <cstdlib>
using namespace std;

int delete_Start_Zero(const string s,const int i)//去除前置0
{
  int index=i;
  while(index<s.length()&&s[index]=='0')
  ++index;
  if(index==s.length()||s[index]=='.')//保留小数点前面的一个0
  --index;
  return index;
}

int delete_End_Zero(const string s,const int i)//去除后置0
{
  int j=i-1;
  for(;j>=0;--j)
    if(s[j]!='0')
      break;
  if(s[j]=='.')//保留小数点后的一个0
    return j+1;
  else
    return j;
}

bool substraction(const string s1,const string s2)
{
  int start_1=delete_Start_Zero(s1,1);//s1有效数字的起始位置
  int end_1=delete_End_Zero(s1,s1.length());//s1有效数字的结束位置
  int start_2=delete_Start_Zero(s2,1);//s2有效数字的起始位置
  int end_2=delete_End_Zero(s2,s2.length());//s2有效数字的结束位置

  if((s1[0]=='+'&&s2[0]=='-')||(s1[0]=='-'&&s2[0]=='+'))//符号相反只有都是0结果才可能为0
    if(s1.substr(1,3)=="0.0"&&s2.substr(1,3)=="0.0")
      return true;
    else
      return false;
  else if((s1[0]=='+'&&s2[0]=='+')||(s1[0]=='-'&&s2[0]=='-'))//符号相同数字一样才为0
    if(s1.substr(start_1,end_1-start_1+1)==s2.substr(start_2,end_2-start_2+1))
      return true;
    else
      return false;
}

int main()
{
  string s1,s2;
  while(cin>>s1>>s2)
  {
    if(isdigit(s1[0]))//如果第一位没有符号且是数字,则为正数
      s1="+"+s1;
    if(isdigit(s2[0]))
      s2="+"+s2;
    if(s1.find('.')==-1)//把正数也当做小数来处理,
      s1.append(".0");
    if(s2.find('.')==-1)
      s2.append(".0");
    if(substraction(s1,s2))
      cout<<"YES"<<endl;
    else
      cout<<"NO"<<endl;
  }
  return 0;
}

A-B Problem nyoj的更多相关文章

  1. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

    Prime Ring Problem Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  2. nyoj 473 A^B Problem

    A^B Problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Give you two numbers a and b,how to know the a^ ...

  3. nyoj 623 A*B Problem II(矩阵)

    A*B Problem II 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 ACM的C++同学有好多作业要做,最头痛莫过于线性代数了,因为每次做到矩阵相乘的时候,大 ...

  4. NYOJ:题目524 A-B Problem

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=860 My思路: 先用两个字符串储存这两个实数,然后再用另外两个字符串储存去掉符号和前后多 ...

  5. nyoj 103 A + B problem II

    点击打开链接 A+B Problem II 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 I have a very simple problem for you. G ...

  6. NYoj The partial sum problem(简单深搜+优化)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: #include <stdio.h> #include & ...

  7. nyoj A+B Problem IV

    A+B Problem IV 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 acmj最近发现在使用计算器计算高精度的大数加法时很不方便,于是他想着能不能写个程序把这 ...

  8. 【志银】NYOJ《题目524》A-B Problem

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=860 My思路: 先用两个字符串储存这两个实数,然后再用另外两个字符串储存去掉符号和前后多 ...

  9. NYOJ 219 An problem about date

    An problem about date 时间限制:2000 ms  |  内存限制:65535 KB 难度:2   描述 acm的iphxer经常忘记某天是星期几,但是他记那天的具体日期,他希望你 ...

随机推荐

  1. surface知识点

    SurfaceView和TextureView 在学习直播的过程遇到一个问题:连麦场景下能够支持大小窗口切换(即小窗口变大,大窗口变小),大窗口是TextView(用于拉流显示),而小窗口是Surfa ...

  2. POI之Excel导入

    1,maven配置 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-oox ...

  3. Java编程学习之JDBC连接MySQL

    JDBC连接MySQL 一.对JDBC连接数据库的步骤1.加载数据库驱动//加载驱动Class.forName(driverClass)-------------------------------- ...

  4. 【javascript】浏览器用户代理检测脚本实现

    以下是完整的用户代理字符串检测脚本,包括检测呈现引擎.平台.Windows操作系统.移动设备和游戏系统. var client = function(){ // 呈现引擎 var engine = { ...

  5. git如何自动打补丁

    答:git am --reject jello.patch  (如果打补丁失败,会自动生成rej文件)

  6. Android -- 在一个Activity开启另一个Activity 并 获取他的返回值。

    1. 视图示例, 按选择弹出 2界面, 选择选项  回显到1    2. 示例代码 MainActivity.java, 第一个activity public class MainActivity e ...

  7. class []的用法

    span[class='test']    =>匹配所有带有class类名test的span标签 span[class *='test']  =>匹配所有包含了test字符串的class类 ...

  8. 插入10W数据的两个程序比较

    程序1 添加10W数据 $count = 0; for ($i = 1;$i <= 100000 ;$i++) { $add_data = [ 'id' => $i, 'username' ...

  9. Qt5发布错误01

    1.Qt5+VS2013兼容XP方法 (http://www.qtcn.org/bbs/apps.php?q=diary&a=detail&did=2047&uid=15538 ...

  10. QWebEngine_C++_交互

    参考网址:http://blog.csdn.net/liuyez123/article/details/50509788 ZC: 该文章里面的: “ <ahref="javascrip ...