1060 Are They Equal——PAT甲级真题
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为有效数字位数。
大致思路:
- 先排除前导0的影响,删除当前字符串的所有前导0,然后在判断当前字符串是>1 还是<1.
- 如果>1,设置一个指针K遍历当前字符串,直到找到小数点或者遍历到字符串的最后一位。然后删除小数点,同时在遍历的时候指数部分e也要不断增加
- 如果<1, 设置一个指针k遍历当前字符串,不断删除小数点后面的前导0,同时指数部分e不断-1.
- 上述过程处理完之后,在判断当前字符串的长度和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甲级真题的更多相关文章
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
- 1080 Graduate Admission——PAT甲级真题
1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...
- 1053 Path of Equal Weight——PAT甲级真题
1053 Path of Equal Weight 给定一个非空的树,树根为 RR. 树中每个节点 TiTi 的权重为 WiWi. 从 RR 到 LL 的路径权重定义为从根节点 RR 到任何叶节点 L ...
- PAT甲级真题及训练集
正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- PAT 甲级真题
1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...
- PAT甲级真题 A1025 PAT Ranking
题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...
- Count PAT's (25) PAT甲级真题
题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...
随机推荐
- ipython和jupyter安装
ipython pip install ipython pip install --upgrade pip jupyter pip install jupyter 查看token: jupyter n ...
- mysql高级day2
Mysql高级-day02 1. Mysql的体系结构概览 整个MySQL Server由以下组成 Connection Pool : 连接池组件 Management Services & ...
- 关于.NET中迭代器的实现以及集合扩展方法的理解
在C#中所有的数据结构类型都实现IEnumerable或IEnumerable<T>接口(实现迭代器模式),可以实现对集合遍历(集合元素顺序访问).换句话可以这么说,只要实现上面这两个接口 ...
- Hive 中日志的存放位置
目前hive启动无法成功,想查看下hive的日志定位问题,但发现hive的安装目录下并没有hive的日志,后来经过在网上谷歌发现: Hive中的日志分为两种 系统日志,记录了hive的运行情况,错误状 ...
- zjnu1762 U (线段树)
Description Mirko is hungry as a bear, scratch that, programmer and has stumbled upon a local restau ...
- 郁闷的出纳员 HYSBZ - 1503
OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的 工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资.如果他 ...
- Codeforces Round #531 (Div. 3) B. Array K-Coloring (结构体排序)
题意:给你\(n\)个数字,用\(k\)种颜色给他们涂色,要求每个数字都要涂,每种颜色都要用,相同的数字不能涂一样的颜色. 题解:用结构体读入每个数字和它的位置,然后用桶记录每个数字出现的次数,判断是 ...
- Codeforces Round #663 (Div. 2) C. Cyclic Permutations (构造,图?)
题意:对于某个序列,若\(1\le i\le n\),\(1\le j\le i\)且\(p_j>p_i\),或者\(1\le i\le n\),\(i<j \le n\)且\(p_j&g ...
- 牛客编程巅峰赛S1第6场 - 黄金&钻石&王者 B.牛牛摆放花 (贪心)
题意;将一组数重新排序,使得相邻两个数之间的最大差值最小. 题解:贪心,现将所有数sort一下,然后正向遍历,将数分配到新数组的两端,然后再遍历一次维护一个最大值即可. 代码: class Solut ...
- Go中的Socket编程
在很多底层网络应用开发者的眼里一切编程都是Socket,话虽然有点夸张,但却也几乎如此了,现在的网络编程几乎都是用Socket来编程.你想过这些情景么?我们每天打开浏览器浏览网页时,浏览器进程怎么和W ...