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. JNI与NDK简析(一)

    1 JNI 简介 在Android Framework中,需要提供一种媒介或 桥梁,将Java层(上层)与C/C++层(下层)有机的联系起来,使得他们互相协调完成某些任务.而充当这种媒介的就是Java ...

  2. ubuntu(物理机)连接ARM开发板

    非虚拟机 ubuntu下连接开发板 首先安装超级终端minicom sudo apt-get install minicom 安装完minicom以后,需要将开发板和电脑进行物理连接.需要使用一条网线 ...

  3. 蚂蚁金服合作的RISE实验室到底有多牛?

    近日,蚂蚁金服与美国加州伯克利大学近期新成立的RISE实验室达成合作意向.RISE实验室的前身是著名伯克利AMP实验室,主导研发了当今大数据计算领域最前沿的开源系统:Apache Spark.Apac ...

  4. angular2相关

    脚手架安装一个项目 1.全局安装angular脚手架 npm install -g @angular/cli 2.初始化一个文件夹 ng new my-angular-demo 3.进入文件夹 cd ...

  5. NLP入门之语音模型原理

    这一篇文章其实是参考了很多篇文章之后写出的一篇对于语言模型的一篇科普文,目的是希望大家可以对于语言模型有着更好地理解,从而在接下来的NLP学习中可以更顺利的学习. 1:传统的语音识别方法: 这里我们讲 ...

  6. 监控之--Nagios如何监控本地主机及本地服务

    上一节内容介绍了Nagios监控服务在linux环境下的安装过程,本节内容将详细介绍如何使用已经安装的Nagios服务的一些配置文件的使用以及如何监控本地相关服务,如要完成对一台主机的监控Nagios ...

  7. NodeJs mysql 开启事务

    如题:node后台使用mysql数据库,并使用事务来管理数据库操作. 这里主要讲一个事务的封装并写了一个INSERT 插入操作. code: 基础code: db.config.js const my ...

  8. Leetcode 1. 两数之和 (Python版)

    有粉丝说我一个学算法的不去做Leetcode是不是浪费,于是今天闲来没事想尝试一下Leetcode,结果果断翻车,第一题没看懂,一直当我看到所有答案的开头都一样的时候,我意识到了我是个铁憨憨,人家是让 ...

  9. Centos7增加磁盘空间并挂载目录(VMware)

    1.前言 今天本机vmware在使用docker安装oracle11g时提示nospace空间不足,所以用这篇文章简介下虚拟机如何扩展硬盘并挂载 2.添加新硬盘 依次点击"虚拟机" ...

  10. STL之内存管理

    STL以泛型思维为基础,提供了6大组件:容器(containers).算法(algorithms).迭代器(iterators).仿函数(functors).适配器(adapters).分配器(all ...