博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT-1060 Are They Equal (科学计数法)
阅读量:4639 次
发布时间:2019-06-09

本文共 3127 字,大约阅读时间需要 10 分钟。

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 
#include
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;}

转载于:https://www.cnblogs.com/zhayujie/p/7534848.html

你可能感兴趣的文章
Linux epoll 笔记(高并发事件处理机制)
查看>>
shell脚本练习01
查看>>
WPF图标拾取器
查看>>
通过取父级for循环的i来理解闭包,iife,匿名函数
查看>>
HDU 3374 String Problem
查看>>
数据集
查看>>
打印python包含汉字报SyntaxError: Non-ASCII character '\xe4' in file
查看>>
[Leetcode] unique paths ii 独特路径
查看>>
HDU 1217 Arbitrage (Floyd + SPFA判环)
查看>>
IntelliJ idea学习资源
查看>>
Django Rest Framework -解析器
查看>>
ExtJs 分组表格控件----监听
查看>>
Hibernate二级缓存配置
查看>>
LoadRunner常用术语
查看>>
关于jedis2.4以上版本的连接池配置,及工具类
查看>>
记忆讲师石伟华微信公众号2017所有文章汇总(待更新)
查看>>
mechanize (1)
查看>>
FactoryBean
查看>>
Coolite动态加载CheckboxGroup,无法在后台中获取
查看>>
如何在我们项目中利用开源的图表(js chart)
查看>>