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. HashMap源码解析JDK8

    一.HashMap基础 1.1 HashMap的定义 我们先看一下HashMap的定义: public class HashMap<K,V> extends AbstractMap< ...

  2. Java多线程并发系列之闭锁(Latch)和栅栏(CyclicBarrier)

    JAVA并发包中有三个类用于同步一批线程的行为,分别是闭锁(Latch),信号灯(Semaphore)和栅栏(CyclicBarrier).本贴主要说明闭锁(Latch)和栅栏(CyclicBarri ...

  3. LaTex中文article模板(支持代码、数学、TikZ)

    代码 请使用XeLatex编译 main.tex \documentclass{article} \usepackage{ctex} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...

  4. 深入实践Spring Boot1.4 运行与发布

    1.4 运行与发布 本章实例工程的完整代码可以使用IDEA直接从GitHub的https://github.com/chen-fromsz/spring-boot-hello.git中检出,如图1-1 ...

  5. C++--浅谈开发系统的经验

    最近写了不少类了,从垃圾代码爬坑,虽然还是很垃圾,但是照葫芦画瓢,有几分神韵.在这里总结一下,写类的经验教训. 第一步 分析: 当拿到一个要求时,要先去考虑怎样一个类到底该实现什么样的功能,有什么样的 ...

  6. Codeforce 263D Cycle in Graph 搜索 图论 哈密尔顿环

    You've got a undirected graph G, consisting of n nodes. We will consider the nodes of the graph inde ...

  7. poj1679 The Unique MST(最小生成树唯一性)

    最小生成树的唯一性,部分参考了oi-wiki 如果一条不在最小生成树边集内的边,它可以替换一条在最小生成树边集内,且权值相等的边,那么最小生成树不是唯一的 同过kruskal来判断 考虑权值相等的边, ...

  8. Android 开发技术周报 Issue#278

    新闻 Pixel 4a渲染图曝光:或能成新款iPhone SE有力竞争者 Google Play商店为预注册的游戏和应用提供自动安装功能 Android最强单摄Pixel 4a样张曝光:1200万像素 ...

  9. 模块_os模块

    import os print(os.getcwd()) # 获取当前工作目录 print(os.listdir()) # 列表列出当前目录下的目录名和文件名 os.mkdir("tempd ...

  10. 系统通配符号、系统正则符号,grep

    系统通配符号.系统正则符号,grep 1 系统通配符号 系统通配符号:借助通配符号 匹配文件名称信息 1.1 *: 匹配所有(任意)字符信息 找寻以old开头的文件 find /oldboy -typ ...