【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 ...
随机推荐
- sql bak还原到新数据库
1 创建新数据库 TestDB 2 使用语句 use master restore database [TestDB] from disk = 'D:\SqlDataBak\SanJu\SanJu ...
- CentOS安装git及使用Gitolite来管理版本库
首先吐槽一下网上的各种教程,大部分都扯蛋,估计都是些所谓的"编辑"在网上瞎抄来的-- 以下内容都是基于CentOS的服务器端,Mac OS X的客户端. 如果是使用的Windows ...
- 40+ 个非常有用的 Oracle 查询语句
40+ 个非常有用的 Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询.这些是所有 Oracle 开发者都必备的技能,所以快快收藏吧! 日期/时间 ...
- linux c 编程 ------ 程序入口参数,即 main 参数
#include <stdio.h> int main(int argc, char *argv[]) { printf(]); int i = argc; printf("th ...
- HTML5 快速学习一
关注HTML5有一段时间了,一直没系统的去学习过. 对于HTML5的理解,之前停留在一些新的标签,一些api可以完成部分js完成的事情,仅此而已. 前段时间HTML5定稿了,看了一些这方面的报道,进行 ...
- Python教你找到最心仪的对象
规则 单身妹妹到了适婚年龄,要选对象.候选男子100名,都是单身妹妹没有见过的.百人以随机顺序,从单身妹妹面前逐一经过.每当一位男子在单身妹妹面前经过时,单身妹妹要么选他为配偶,要么不选.如果选他,其 ...
- 鸟哥的Linux私房菜——第八章
参考鸟哥的Linux私房菜内容,我只是简单的记录比较重要的知识点,所以排版没怎么注意,如果写的太严肃小白也看不进去吧. 看视频!这篇文章只作为备忘录 视频链接:http://www.tudou.com ...
- [Spring] 学习Spring Boot之一:基本使用及简析
一.简介 使用 Spring Boot 目的主要是用来简化 Spring 应用的搭建及开发过程,因为使用 Spring 及 SpringMVC 框架时需要手动配置的地方非常多(各种包之间的依赖.各种配 ...
- bzoj千题计划215:bzoj1047: [HAOI2007]理想的正方形
http://www.lydsy.com/JudgeOnline/problem.php?id=1047 先用单调队列求出每横着n个最大值 再在里面用单调队列求出每竖着n个的最大值 这样一个位置就代表 ...
- HDU 5299 圆扫描线 + 树上删边
几何+博弈的简单组合技 给出n个圆,有包含关系,以这个关系做游戏,每次操作可以选择把一个圆及它内部的圆全部删除,不能操作者输. 圆的包含关系显然可以看做是树型结构,所以也就是树上删边的游戏. 而找圆的 ...