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. springboot中扩展ModelAndView实现net mvc的ActionResult效果

    最近在写spring boot项目,写起来感觉有点繁琐,为了简化spring boot中的Controller开发,对ModelAndView进行简单的扩展,实现net mvc中ActionResul ...

  2. CF-1328 E. Tree Queries

    E. Tree Queries 题目链接 题意 给定一个树,每次询问一组点,问是否存在一条从根到某点的路径,使得该组点到该路径的最短距离不超过1 分析 从根到达某点的路径,如果覆盖到了某个点,那么一定 ...

  3. Codeforces Round #628 (Div. 2) D. Ehab the Xorcist(异或,思维题)

    题意: 寻找异或后值为 u,相加后和为 v 的最短数组. 思路: 异或得 u ,则 v 至少应大于等于 u ,且多出来的部分可以等分为两份相消. 即初始数组为 u , (v-u)/2 , (v-u)/ ...

  4. BZOJ3238 [Ahoi2013]差异 【SAM or SA】

    BZOJ3238 [Ahoi2013]差异 给定一个串,问其任意两个后缀的最长公共前缀长度的和 1.又是后缀,又是\(lcp\),很显然直接拿\(SA\)的\(height\)数组搞就好了,配合一下单 ...

  5. Codeforces Round #345 (Div. 1) C. Table Compression (并查集)

    Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorith ...

  6. Codeforces Round #641 (Div. 2) D. Orac and Medians (贪心)

    题意:有一个长度为\(n\)的数组,问能否通过多次使某个区间的所有元素变成这个区间的中位数,来使整个数组变成题目所给定的\(k\). 题解:首先这个\(k\)一定要在数组中存在,然后我们对中位数进行考 ...

  7. Codeforces Round #515 (Div. 3) B. Heaters (贪心)

    题意:有\(n\)个桩子,\(1\)表示该位置有一个火炉,可以使两边距离为\(r\)的范围照亮,问最少使用多少炉子使得所有范围都被照亮. 题解:贪心,首先我们从\(r\)位置开始向左找,如果找到了就记 ...

  8. LEETCODE - 1181【前后拼接】

    class Solution { public: string gethead(string str){//获取头单词 string ret = ""; int strlen = ...

  9. Linux CentOS7.x 升级内核的方法

    一.概述 在数据中心基础环境中,Linux系统使用很普遍,但是有时候会遇到应用程序需要运行在高版本的内核上或者有时候系统自身要求需要升级内核,我们要综合考虑升级内核的风险. 二.升级内核的方法 1.查 ...

  10. 牛客多校第九场 && ZOJ3774 The power of Fibonacci(二次剩余定理+斐波那契数列通项/循环节)题解

    题意1.1: 求\(\sum_{i=1}^n Fib^m\mod 1e9+9\),\(n\in[1, 1e9], m\in[1, 1e4]\) 思路1.1 我们首先需要知道斐波那契数列的通项是:\(F ...