【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 12300 and 12358.9 are considered equal since they are both saved as 0.123*10^5^ 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 10^100^, 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
科学计数法,注意以下几种情况:
1.数字前有 0 的,比如:00056;0000.32 等,需先去除无效的0;
2.指数 e 的正负计算,0.1 = 0.1*10^0;
3.不同情况下有效位的计算,比如00056,0.00032;
4.按题目要求输出有效位数,不足的后面补0;
C++代码如下:
#include<iostream>
#include<string>
#include<iomanip>
using namespace std; void deal(string &s, int &e) {
while (s.length() > && s[] == '') s.erase(s.begin());
int i;
if (s[] == '.') {
s.erase(s.begin());
while (s.length()>&&s[] == '') {
e--;
s.erase(s.begin());
}
if (s.length() == ) e = ;
}
else {
for ( i = ; i < s.length(); i++) {
if (s[i] != '.') e++;
else break;
}
if(i<s.length()) s.erase(s.begin() + i);
}
}
void signdigit(string &s,int n) {
while (s.length() < n) s += '';
s.resize(n);
}
int main() {
string str1, str2;
int n,e1=,e2=;
cin >> n >> str1 >> str2;
deal(str1, e1);
deal(str2, e2);
signdigit(str1, n);
signdigit(str2, n);
if (str1 == str2 && e1 == e2)
cout << "YES 0." << str1 << "*10^" << e1 << endl;
else
cout << "NO 0." << str1 << "*10^" << e1 << " 0." << str2 << "*10^" << e2 << endl;
return ;
}
【PAT】1060 Are They Equal (25)(25 分)的更多相关文章
- PAT 1060 Are They Equal[难][科学记数法]
1060 Are They Equal(25 分) If a machine can save only 3 significant digits, the float numbers 12300 a ...
- 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 1060. Are They Equal
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...
- 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 分) 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乙级:1090危险品装箱(25分)
PAT乙级:1090危险品装箱(25分) 题干 集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里.比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸. 本题给定一张不相容物品的清 ...
- PAT乙级:1070 结绳 (25分)
PAT乙级:1070 结绳 (25分) 题干 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟 ...
- PAT 甲级 1060 Are They Equal
1060. Are They Equal (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue If a ma ...
随机推荐
- hdu 6315 Naive Operations (2018 Multi-University Training Contest 2 1007)
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- 99种用Racket说I love you的方式
99种用Racket说I love you的方式 来源 https://www.tuicool.com/articles/qiyeAz 原文 http://www.soimort.org/posts ...
- MT【88】抽象函数
分析:此类题一般有两种做法,第一种按解答题做法, 第二种作为填空题找对应的特殊函数,比如这里可以根据三角里和差化积得出$f(x)=\frac{1}{2}cos(\frac{\pi}{3}x)$
- 【刷题】LOJ 6011 「网络流 24 题」运输问题
题目描述 W 公司有 \(m\) 个仓库和 \(n\) 个零售商店.第 \(i\) 个仓库有 \(a_i\) 个单位的货物:第 \(j\) 个零售商店需要 \(b_j\) 个单位的货物.货物供需平衡, ...
- Apache Storm从一端读取实时数据的原始流
Apache Storm从一端读取实时数据的原始流,并将其传递通过一系列小处理单元,并在另一端输出处理/有用的信息. 下图描述了Apache Storm的核心概念. 640?wx_fmt=png&am ...
- 【bzoj1044】木棍分割
Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...
- [Java] I/O底层原理之二:网络IO及网络编程
首先我们来看一下当访问一个域名时它的过程 查找 DNS 首先,浏览器检查缓存中有没有 浏览器缓存中没有,则查找操作系统中有没有配置这个对应关系 如果操作系统中也没有,则去 DNS 查找,即发送DNS报 ...
- Java SSM框架之MyBatis3(二)MyBatis之Mapper代理的开发方式
Mapper代理的开发规范 1. mapper接口的全限定名要和mapper映射文件的namespace值一致. 2. mapper接口的方法名称要和mapper映射文件的statement的id一致 ...
- shell 检测安装包
检测 wget 是否存在 rpm -q wget >/dev/null ];then echo "install wget,Please wait..." yum -y in ...
- Anaconda+django写出第一个web app(九)
今天来学习外键的使用,用外键来连接数据库中的两个表. 当我们的tutorials非常多的时候,目前的显示方式就会使得页面非常凌乱.我们可以考虑把这些教程分为不同的系列,页面只显示标题以及概要等信息,进 ...