PAT 甲级 1060 Are They Equal
1060. Are They Equal (25)
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123*105 with simple chopping. Now given the number of significant digits on a machine and
two float numbers, you are supposed to tell if they are treated equal in that machine.
Input Specification:
Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10100,
and that its total digit number is less than 100.
Output Specification:
For each test case, print in a line "YES" if the two numbers are treated equal, and then the number in the standard form "0.d1...dN*10^k" (d1>0 unless the number
is 0); or "NO" if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.
Note: Simple chopping is assumed without rounding.
Sample Input 1:
3 12300 12358.9
Sample Output 1:
YES 0.123*10^5
Sample Input 2:
3 120 128
Sample Output 2:
NO 0.120*10^3 0.128*10^3
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string> using namespace std;
pair<string,int> m;
string a,b;
string aa,bb;
int n;
pair<string,int> fun(string a)
{
int len=a.length();
int begin,end;
int tag1=0,tag2=0;
for(int i=0;i<len;i++)//去除前缀的0和后缀的0
{
if(!tag1&&(a[i]!='0'||(a[i]=='0'&&a[i+1]=='.')))
begin=i,tag1=1;
if(tag2==1&&a[i]!='0')
end=i;
if(tag2==0)
end=i;
if(a[i]=='.')
tag2=1;
}
string c=a.substr(begin,end-begin+1);
int i;
for(i=0;c[i];i++)//找到小数点的位置 if(c[i]=='.')
break;
int j;
for(j=0;c[j];j++)//找到第一个不是0的数字位置,即科学技术法中小数点应该放在哪个位置之前,
{
if(c[j]!='0'&&c[j]!='.')
break;
}
string d="0.";int num=0;
for(int ii=j;c[ii];ii++)
{
if(c[ii]=='.') continue;
d+=c[ii];
num++;
if(num>=n)
break;
}
for(int i=0;i<n-num;i++)//补0
d+='0'; int k=i-j;//小数点位置的变化,就是指数
if(k<0) k++;
m.first=d;
m.second=k;
return m;
}
int main()
{
cin>>n>>a>>b;
pair<string,int> mm1,mm2;
mm1=fun(a);
mm2=fun(b);
if(mm1==mm2)
cout<<"YES "<<mm1.first<<"*10^"<<mm1.second<<endl;
else
cout<<"NO "<<mm1.first<<"*10^"<<mm1.second<<" "<<mm2.first<<"*10^"<<mm2.second<<endl;
return 0;
}
PAT 甲级 1060 Are They Equal的更多相关文章
- PAT 甲级 1060 Are They Equal (25 分)(科学计数法,接连做了2天,考虑要全面,坑点多,真麻烦)
1060 Are They Equal (25 分) If a machine can save only 3 significant digits, the float numbers 1230 ...
- PAT甲级1060 Are They Equal【模拟】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805413520719872 题意: 给定两个数,表示成0.xxxx ...
- 【PAT】1060 Are They Equal (25)(25 分)
1060 Are They Equal (25)(25 分) If a machine can save only 3 significant digits, the float numbers 12 ...
- pat 甲级 1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)
1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight Wi assigne ...
- PAT甲级——A1060 Are They Equal
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...
- PAT甲级——A1053 Path of Equal Weight
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weig ...
- 1060 Are They Equal——PAT甲级真题
1060 Are They Equal If a machine can save only 3 significant digits, the float numbers 12300 and 123 ...
- PAT 1060 Are They Equal[难][科学记数法]
1060 Are They Equal(25 分) If a machine can save only 3 significant digits, the float numbers 12300 a ...
随机推荐
- Atitit. Java script 多重多重catch语句的实现and Javascript js 异常机制
Atitit. Java script 多重多重catch语句的实现and Javascript js 异常机制 1. 语法错误(ERROR)和运行期错误(Exception) 1 2. 错误类型判断 ...
- 浅谈I2C总线
I2C总线概述 I2C(Inter-Integrated Circuit)总线是一种由PHILIPS公司在80年代开发的两线式串行总线,用于连接微控制器及其外围设备.I2C总线最主要的优点是其简单性和 ...
- [svc]nginx优化25条
参考:
- jmap 查看内存使用直方图
jps -- 查看进程号 jmap -histo pid 查看堆内存中的对象数目.大小统计直方图, 如果带上live则表示先进行一次fullgc 再统计内存使用情况,如下: jmap -hist ...
- 如何只利用NMAKE+CL+LINK写WIN32程序
关键是1.包含<Windows.h>及其他的相关头文件2.在LINK指令中最起码要加上KERNEL32.LIB USER32.LIB GDI32.LIB(不需要制定其路径,因为NMAKE, ...
- Linux之目录的操作(创建、移动、改名、删除、复制)
.创建 mkdir [dirname] //创建单个目录 mkdir -p newdir1/newdir2/newdir3 //递归创建多级目录 mkdir dir1/dir2/newdir3 //在 ...
- 基于html5和css3响应式全屏滚动页面切换效果
分享一款全屏响应式的HTML5和CSS3页面切换效果.这个页面布局效果对于那些页面要求固定100%高度和宽度的网站和APP来说是十分有用的.效果图如下: 在线预览 源码下载 HTML wrappe ...
- [转]SOA接口的两种常用实现比较:SOAP vs REST
原文链接:http://blog.csdn.net/zhaohuabing/article/details/39643127 SOA架构用于异构系统的协作,因此需要一种跨操作系统.跨语言的通用的消息交 ...
- C#调用Oracle存储过程
C#调用Oracle存储过程的代码如下所示: using System; using System.Collections.Generic; using System.Collections.Obje ...
- Spring Boot 属性配置&自定义属性配置
在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...