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 ...
随机推荐
- 关于POI相关通用方法源码
设置宽度,1个汉字的宽度 导入excel用,返回行数 sheetName是sheet,显示名 导出excel 导出excel 获得excel数据 写输出,最后用 重新单元格指定位置 移到下一行,列开头 ...
- 学习笔记 Hadoop的job提交过程,shuffle过程以及HA机制的实现
一,在hadoop中的mapreduce的job提交过程比较繁琐,但掌握job的提交过程是我们进入深入学习的必要. 二,mapreduce的shuffle机制 三,Hadoop的HA机制.
- typedef void (*sighandler_t)(int);
typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); ---------- ...
- Kubernetes --(k8s)Job、CronJob
Job https://www.kubernetes.org.cn/job https://www.kubernetes.org.cn/cronjob Job负责批量处理短暂的一次性任务 (short ...
- 飞塔创建IPSec
5.2和5.4版本飞塔建立IPSec VPN时,必须在两端添加完策略.路由后IPSec才会起来.
- 关于base64编码Encode和Decode编码的几种方式--Java
Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便.在实际应用上,Base64除了能将Binary资料可视化之外 ...
- 【洛谷 p3383】模板-线性筛素数(数论)
题目:给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内).(N<=10000000,M<=100000) 解法:1.欧拉筛O(n),数组近乎100KB:2.( ...
- Codeforces Round #676 (Div. 2) XORwice、Putting Bricks in the Wall、Palindromifier
题目链接:XORwice 题意:给你两个数a.b.求一个数x,使得((a异或x)+(b异或x))这个值最小,输出最小那个x 题解: 输出(a|b)-(a&b)就行(猜了一手 代码: #incl ...
- P1091 合唱队形(LIS)
题目描述 NNN位同学站成一排,音乐老师要请其中的(N−KN-KN−K)位同学出列,使得剩下的KKK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,-,K1,2, ...
- hdu1313 Round and Round We Go (大数乘法)
Problem Description A cyclic number is an integer n digits in length which, when multiplied by any i ...