PAT-1060 Are They Equal (科学计数法)
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.120*10^3 0.128*10^3
题目大意:给出两个数,要求判断两数在保留小数点后n位并以科学计数法表示时是否相等。
主要思想:该题的核心步骤主要是找出一个数的小数点位置(没有则设置为字符串长度),第一个有效数位置(非0和非小数点),然后根据这两个值计算出指数值。在比较的时候应分两部分,底数和指数,都相等时才相等,其中底数的小数点后n位部分存于另一目标数组。(注意:诸如0.000的为特殊形式,有效位置等于字符串长度,此时应把指数设为0,防止出现0.00与0.000不等的错误)
#include <cstdio>
#include <string.h>
void print_str(char * s, int n, int count);
int main(void) {
int n, i;
char s1[105], s2[105], res1[105], res2[105]; scanf("%d %s %s", &n, s1, s2);
int count1 = strlen(s1), count2 = strlen(s2); //小数点位置
int p = 0, q = 0; //第一个有效数字的位置 //分别计算两个数的 小数点位置,首个有效数位置,幂指数
for (i = 0; i < strlen(s1); i++) {
if (s1[i] == '.') {
count1 = i;
break;
}
}
for (i = 0; i < strlen(s2); i++) {
if (s2[i] == '.') {
count2 = i;
break;
}
}
while (s1[p] == '0' || s1[p] == '.') p++;
while (s2[q] == '0' || s2[q] == '.') q++;
int k1 = count1 - p; //k1, k2 表示指数
if (k1 < 0) k1++;
int k2 = count2 - q;
if (k2 < 0) k2++;
if (p == strlen(s1)) k1 = 0; //0.000.. 的情况
if (q == strlen(s2)) k2 = 0; //将两字符串的n位 底数部分 复制到结果数组
int index1 = 0, index2 = 0;
while (index1 < n) {
if (p < strlen(s1) && s1[p] != '.')
res1[index1++] = s1[p];
else if (p >= strlen(s1))
res1[index1++] = '0';
p++;
}
res1[index1] = '\0';
while (index2 < n) {
if (q < strlen(s2) && s2[q] != '.')
res2[index2++] = s2[q];
else if (q >= strlen(s2))
res2[index2++] = '0';
q++;
}
res2[index2] = '\0';
if (!strcmp(res1, res2) && k1 == k2)
printf("YES 0.%s*10^%d\n", res1, k1);
else
printf("NO 0.%s*10^%d 0.%s*10^%d\n", res1, k1, res2, k2); return 0;
}
PAT-1060 Are They Equal (科学计数法)的更多相关文章
- PAT (Basic Level) Practice 1024 科学计数法 分数 20
科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [+-][1-9].[0-9]+E[+-][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指 ...
- 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 ...
- PAT甲题题解-1060. Are They Equal (25)-字符串处理(科学计数法)
又是一道字符串处理的题目... 题意:给出两个浮点数,询问它们保留n位小数的科学计数法(0.xxx*10^x)是否相等.根据是和否输出相应答案. 思路:先分别将两个浮点数转换成相应的科学计数法的格式1 ...
- pat 1060 比较科学计数法
trick: 1.前导0 如:000001,000.000001 2.出现0时也要按照科学计数法输出 e.g. 4 00000.00000 0001 NO 0.0000*10^0 0.1*10^1 3 ...
- C#版 - PAT乙级(Basic Level)真题 之 1024.科学计数法转化为普通数字 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. PAT Bas ...
- PAT 1024 科学计数法 (20)(精简版代码+思路+推荐测试样例)
1024 科学计数法 (20)(20 分) 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+, ...
- PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)
PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...
- PAT 1024. 科学计数法 (20)
科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分只有1位,小数部分至少有1位 ...
- PAT乙级 1024. 科学计数法 (20)
1024. 科学计数法 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HOU, Qiming 科学计数法是科学家用来表示很 ...
随机推荐
- Spring Boot JPA的查询语句
文章目录 准备工作 Containing, Contains, IsContaining 和 Like StartsWith EndsWith 大小写不敏感 Not @Query Spring Boo ...
- (转)mysql数据库表名批量修改大小写
由于不用服务器对mysql的表名的大小写敏感要求不一致,经常在出现线上的数据库down到了本地不能运行的情况,贴出一段代码用来批量修改数据库表名大小写. DELIMITER // DROP PROCE ...
- nodeJS中express框架设置全局跨域请求头
//设置跨域请求头 router.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin&qu ...
- codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)
ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...
- python(If 判断)
一.if判断 如果 条件满足,才能做某件事情, 如果 条件不满足,就做另外一件事情,或者什么也不做 注意: 代码的缩进为一个 tab 键,或者 4 个空格 在 Python 开发中,Tab 和空格不要 ...
- 【Redis】跳跃表原理分析与基本代码实现(java)
最近开始看Redis设计原理,碰到一个从未遇见的数据结构:跳跃表(skiplist).于是花时间学习了跳表的原理,并用java对其实现. 主要参考以下两本书: <Redis设计与实现>跳表 ...
- Spring官网阅读(十八)Spring中的AOP
文章目录 什么是AOP AOP中的核心概念 切面 连接点 通知 切点 引入 目标对象 代理对象 织入 Spring中如何使用AOP 1.开启AOP 2.申明切面 3.申明切点 切点表达式 excecu ...
- 【Hadoop离线基础总结】Hue与Mysql集成
Hue与Mysql集成 1.修改hue.ini配置文件 这里要去掉#,打开mysql注释,大概在1547行 [[[mysql]]] nice_name="My SQL DB" en ...
- 基于情感词典的python情感分析
近期老师给我们安排了一个大作业,要求根据情感词典对微博语料进行情感分析.于是在网上狂找资料,看相关书籍,终于搞出了这个任务.现在做做笔记,总结一下本次的任务,同时也给遇到有同样需求的人,提供一点帮助. ...
- webpack4使用出现ERROR in Template execution failed: ReferenceError: HtmlwebpackPlugin is not defined
问题描述 博主在使用webpack4的时候,使用了ejs文件,先附上ejs中的代码: <!doctype html> <html lang="zh-CN"> ...