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 (科学计数法)的更多相关文章

  1. PAT (Basic Level) Practice 1024 科学计数法 分数 20

    科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [+-][1-9].[0-9]+E[+-][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指 ...

  2. 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 ...

  3. PAT甲题题解-1060. Are They Equal (25)-字符串处理(科学计数法)

    又是一道字符串处理的题目... 题意:给出两个浮点数,询问它们保留n位小数的科学计数法(0.xxx*10^x)是否相等.根据是和否输出相应答案. 思路:先分别将两个浮点数转换成相应的科学计数法的格式1 ...

  4. 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 ...

  5. C#版 - PAT乙级(Basic Level)真题 之 1024.科学计数法转化为普通数字 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. PAT Bas ...

  6. PAT 1024 科学计数法 (20)(精简版代码+思路+推荐测试样例)

    1024 科学计数法 (20)(20 分) 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+, ...

  7. PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)

    PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...

  8. PAT 1024. 科学计数法 (20)

    科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分只有1位,小数部分至少有1位 ...

  9. PAT乙级 1024. 科学计数法 (20)

    1024. 科学计数法 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HOU, Qiming 科学计数法是科学家用来表示很 ...

随机推荐

  1. material UI中withStyles和makeStyles的区别

      在material UI中,withStyles和makeStyles是经常使用的两个用于封装样式的函数.对于刚使用material UI的开发者而言,可能不太清楚这两者的区别.   本文简要探究 ...

  2. MySQL根据业务场景归纳常用SQL语句

    素材表数据:user[{"id":1,"name":"x"},{"id":2,"name":&quo ...

  3. TypeScript 2.0 正式发布

    9 月 22 日,TypeScript 2.0 正式发布了. TypeScript 是微软开发的开源的编程语言,主要负责人是 C# 之父 Anders Hejlsberg. TypeScript 成功 ...

  4. python的unittest框架中的assert断言

    unittest框架自带断言,如果想用assert断言,一定要引入unittest.TestCase框架才行,不然不会自动识别assert断言

  5. python——import日常学习记录

    import为导入包,有两种方法,一个是import,一个是from ** import  ** import后紧跟着的是个模块,一般是一个.py文件下的类名: from *** import *** ...

  6. uiautomatorviewer 出现安卓8.0级以上无法打开的解决方法

    一..本人在使用Android自带的uiautomatorviewer工具来进行app元素定位时,出现了Android 9.0打开不了.出现了如下图错误提示: 经过网上的查阅,总结了几个解决的方法. ...

  7. uniapp中引入less文件

    uniapp入门遇到的问题记录 在uniapp中从外部import less文件的话,首先需要在 工具>插件安装 中安装支持less语法的插件,然后在.vue文件中引入  @import url ...

  8. 【Java8新特性】Lambda表达式基础语法,都在这儿了!!

    写在前面 前面积极响应读者的需求,写了两篇Java新特性的文章.有小伙伴留言说:感觉Lambda表达式很强大啊!一行代码就能够搞定那么多功能!我想学习下Lambda表达式的语法,可以吗?我的回答是:没 ...

  9. Dynamics 9.0 安装好后 公告出现 提示:出现错误。 请稍等片刻,然后重试。 如果问题仍然存在,请与管理员联系。

    此问题为系统的Bug,示例图如下: 解决方案为修改存储过程 p_RetrievePosts,将startDate参数的默认值改成 1900-01-01,endDate参数的默认值改成 9999-12- ...

  10. js函数传递参数的方式------传值与传递指针

    原则: 1. 基本类型:传值 2. 对象:传递指针 应用场景之一: 用jq选择器获取某个div后(例如:element),准备进行某些修改,之后添加到页面中去. 采取例一的方式,append后发现修改 ...