1060 Are They Equal

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.12010^3 0.12810^3

题目大意:让你用科学计数法表示两个数字,然后判断用科学计数法表示的两个数字是否相等,其中n为有效数字位数。

大致思路:

  1. 先排除前导0的影响,删除当前字符串的所有前导0,然后在判断当前字符串是>1 还是<1.
  2. 如果>1,设置一个指针K遍历当前字符串,直到找到小数点或者遍历到字符串的最后一位。然后删除小数点,同时在遍历的时候指数部分e也要不断增加
  3. 如果<1, 设置一个指针k遍历当前字符串,不断删除小数点后面的前导0,同时指数部分e不断-1.
  4. 上述过程处理完之后,在判断当前字符串的长度和n的大小,如果<n说明后面要补0

代码:

#include <bits/stdc++.h>

using namespace std;

int n;

string changeNum(string str, int& e) {
int k = 0; //str的下标
while(str.length() > 0 && str[0] == '0')
str.erase(str.begin()); //先去掉前导0
//如果是小数
if (str[0] == '.') {
str.erase(str.begin());
//去掉非0位前面的所有0
while(str.length() > 0 && str[0] == '0') {
str.erase(str.begin());
e--; //小数,指数依次递减
}
}
//如果是整数
else {
while(k < str.length() && str[k] != '.') {
k++;
e++;
}
if (k < str.length()) str.erase(str.begin() + k);
} //说明这个数为0
if (str.length() == 0) e = 0; //指数为0
int num = 0; k = 0;
string ans = "";
while(num < n) {
if (k < str.length()) ans += str[k++];
else ans += '0'; //如果超过表示范围后面要补0
num++;
}
return ans;
} int main() {
string str1, str2;
cin >> n >> str1 >> str2;
int e1 = 0, e2 = 0;
string ans1 = changeNum(str1, e1), ans2 = changeNum(str2, e2);
if (ans1 == ans2 && e1 == e2) cout << "YES 0." << ans1 << "*10^" << e1 << endl;
else cout << "NO 0." << ans1 << "*10^" << e1 << " " << "0." << ans2 << "*10^" << e2 << endl;
return 0;
}

1060 Are They Equal——PAT甲级真题的更多相关文章

  1. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  2. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  3. 1053 Path of Equal Weight——PAT甲级真题

    1053 Path of Equal Weight 给定一个非空的树,树根为 RR. 树中每个节点 TiTi 的权重为 WiWi. 从 RR 到 LL 的路径权重定义为从根节点 RR 到任何叶节点 L ...

  4. PAT甲级真题及训练集

    正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...

  5. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

  6. PAT 甲级真题

    1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...

  7. PAT甲级真题 A1025 PAT Ranking

    题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...

  8. Count PAT's (25) PAT甲级真题

    题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...

  9. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...

随机推荐

  1. IP路由__距离矢量路由选择协议

    矢量路由选择协议 1.距离矢量路由选择算法发送完整的路由选择表到相邻的路由器,然后,相邻的路由器会将接收到的路由表项与自己原有的路由表进行组合,以完善路由器的路由表. 由于路由器接收到的更新只是来自相 ...

  2. C++ 标准模板库(STL):map

    目录 4. map 4.1 map的定义 4.2 map容器内元素的访问 4.3 map常用函数实例解析 4.4 map的常见用途 4. map map翻译为映射,也是常用的STL容器. 在定义数组时 ...

  3. A - The Suspects (sars传染)

    题意:有m组,0为起点,有0的那一组全是嫌疑人,之后会不断传递到其它组去,问一共有多少人是嫌疑人. Severe acute respiratory syndrome (SARS), an atypi ...

  4. HDOJ 3398

    这个题坑了太久太久啊!!!!!贡献了得有30+WA才发现 原来是因为在乘法中有溢出导致一直TLE啊.... 但是到最后也不知道有个问题怎么解决的. 就是在getp()中的num值的诡异的改变! #in ...

  5. 【bzoj 2163】复杂的大门(算法效率--拆点+贪心)

    题目:你去找某bm玩,到了门口才发现要打开他家的大门不是一件容易的事-- 他家的大门外有n个站台,用1到n的正整数编号.你需要对每个站台访问一定次数以后大门才能开启.站台之间有m个单向的传送门,通过传 ...

  6. codeforces629C Famil Door and Brackets (dp)

    As Famil Door's birthday is coming, some of his friends (like Gabi) decided to buy a present for him ...

  7. 牛客53680 「金」点石成金 (dfs)

    题意:给你\(n\)组数,每组4个正整数\(a,b,c,d\),每组数有两个选择: ​ 1.增加\(a\)个财富,消耗\(b\)点魔法. ​ 2.回复\(c\)点魔法,减少\(a\)个财富. 求最后财 ...

  8. C#之Dispose

    前言 谈到Dispose,首先需要理解C#的资源 资源类型 托管资源:由CLR创建和释放 非托管资源:资源的创建和释放不由CLR管理.比如IO.网络连接.数据库连接等等.需要开发人员手动释放. 如何释 ...

  9. 鸟哥的linux私房菜——第十六章学习(程序管理与 SELinux 初探)

    第十六章.程序管理与 SE Linux 初探 在 Linux 系统当中:"触发任何一个事件时,系统都会将他定义成为一个程序,并且给予这个程序一个 ID ,称为 PID,同时依据启发这个程序的 ...

  10. 操作系统:Linux进程与线程

    这里是一部分内容,还会做修改. 一:目的及内容 学习fork(),exec,pthread库函数的使用,阅读源码,分析fork,exec,pthread_create函数的机理 代码实现: 进程A创建 ...