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.d[1]...d[N]*10^k (d[1]>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<bits/stdc++.h>
using namespace std; int n;
string a,b; const int maxn=; struct bign{
string d;
int cnt;
}; bign change(string str,int k){ bool flag=false; bign ans; int len=(int)str.size(); int i=; while(i<len&&str[i]=='')
{
i++;
} int pos1=-,pos2=-; while(i<len){
if(str[i]=='.')
pos2=i;
else if(str[i]!=''||flag){
ans.d+=str[i];
if(pos1==-)
pos1=i; flag=true; } i++;
} if(i>=len&&pos2==-)
pos2=len; if(pos2>pos1)
ans.cnt=pos2-pos1;
else
ans.cnt=pos2-pos1+; //cout<<pos1<<endl<<pos2<<endl; int anslen=(int)ans.d.size(); if(anslen>=k)
ans.d=ans.d.substr(,k);
else{
int h=k-anslen;
while(h--)
ans.d+='';
} if(flag==false)
ans.cnt=; return ans; } bool judge(bign a,bign b){
if(a.d==b.d&&a.cnt==b.cnt)
return true;
else
return false;
} void print(bign a){
cout<<" 0."<<a.d<<"*10^"<<a.cnt;
} int main(){
cin>>n;
cin>>a>>b; bign ansa,ansb; ansa=change(a,n);
ansb=change(b,n); bool flag = judge(ansa,ansb); if(flag){
cout<<"YES";
print(ansa);
} else{
cout<<"NO";
print(ansa);
print(ansb); } return ; }
1060 Are They Equal (25 分)的更多相关文章
- 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 ...
- 1060 Are They Equal (25分)
1060 Are They Equal (25分) 题目 思路 定义结构体 struct fraction{ string f; int index; } 把输入的两个数先都转换为科学计数法,统一标准 ...
- 【PAT甲级】1060 Are They Equal (25 分)(需注意细节的模拟)
题意: 输入一个正整数N(<=100),接着输入两个浮点数(可能包含前导零,对于PAT已经习惯以string输入了,这点未知),在保留N位有效数字的同时判断两个数是否相等,并以科学计数法输出. ...
- 【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 ...
- 1060. Are They Equal (25)
题目如下: If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are consi ...
- pat 1060. Are They Equal (25)
题目意思直接,要求将两个数转为科学计数法表示,然后比较是否相同 不过有精度要求 /* test 6 3 0.00 00.00 test 3 3 0.1 0.001 0.001=0.1*10^-2 p ...
- PAT (Advanced Level) 1060. Are They Equal (25)
模拟题.坑点较多. #include<iostream> #include<cstring> #include<cmath> #include<algorit ...
- PAT甲题题解-1060. Are They Equal (25)-字符串处理(科学计数法)
又是一道字符串处理的题目... 题意:给出两个浮点数,询问它们保留n位小数的科学计数法(0.xxx*10^x)是否相等.根据是和否输出相应答案. 思路:先分别将两个浮点数转换成相应的科学计数法的格式1 ...
- A1060 Are They Equal (25 分)
一.技术总结 cnta.cntb用于记录小数点出现的位置下标,初始化为strlen(字符串)长度. q.p用于记录第一个非0(非小数点)出现的下标,可以用于计算次方和方便统计输出的字符串,考虑到前面可 ...
随机推荐
- 为什么重写equals还要重写hashcode
参考回答: HashMap中,如果要比较key是否相等,要同时使用这两个函数!因为自定义的类的hashcode()方法继承于Object类,其hashcode码为默认的内存地址,这样即便有相同含义的两 ...
- vue-cesium中经纬度写反了,报错
vue-cesium中经纬度写反了,报错 [Vue warn]: Invalid prop: custom validator check failed for prop "position ...
- activiti7配置文件activiti.cfg.xml,日志文件log4j.properties及pom文件
activiti.cfg.xml:<?xml version="1.0" encoding="UTF-8"?><beans xmlns=&qu ...
- shell编程:find命令
写在前面 在linux的日常管理中,find的使用频率很高,熟练掌握对提高工作效率很有帮助. find的语法比较简单,常用参数的就那么几个,比如-name.-type.-ctime等.初学的同学直接看 ...
- SpringBoot扫描不到类,注入失败A component required a bean of type 'XXService' that could...
SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! “Application类”是指SpringBoot项目入口类.这个类的位置很关键: 如果App ...
- leetcode.数组.769最多能完成排序的块-Java
1. 具体题目 数组arr是[0, 1, ..., arr.length - 1]的一种排列,我们将这个数组分割成几个“块”,并将这些块分别进行排序.之后再连接起来,使得连接的结果和按升序排序后的原数 ...
- C#5.0 异步编程 Async和Await--介绍
C#5.0引入async和await关键字实现方法的异步调用. 直接进入正题. async只是一个标识符,并没有实际的用途,只是用于表明某个方法是异步方法,在方法前面加上async 表示该方法为一个异 ...
- texi2dvi - 打印 Texinfo 文档
SYNOPSIS 总览 texi2dvi [OPTION]... FILE... DESCRIPTION 描述 依次从 Tex 系统中运行每个 Texinfo 或者 LaTex 文件 FILE,直到解 ...
- 负载均衡实现故障vip自动漂移
环境说明演示vip自动漂移 192.168.237.50 192.168.237.51 vip: 192.168.237.5 keepalived开源软件实现 keepalived可以实现当vip挂的 ...
- Centos7搭建SkyWalking分布式追踪,以mysql为存储
Skywalking专门为微服务架构和云原生架构系统而设计并且支持分布式链路追踪的APM系统,即应用性能监控系统,为微服务架构和云原生架构系统设计.它通过探针自动收集所需的指标,并进行分布式追踪.通过 ...