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 科学计数法是科学家用来表示很 ...
随机推荐
- 【JAVA基础】08 面向对象3
1. 多态 多态polymorhic概述 事物存在的多种形态. 多态前提 要有继承关系 要有方法重写 要有父类引用指向子类对象 案例演示 代码体现多态 class Demo1_Polymorphic{ ...
- wampserver安装之后无法打开localhost
2019独角兽企业重金招聘Python工程师标准>>> 如上图 是服务未成功启动 服务成功启动标志 修改服务器监听端口 在安装完wampserver,打开,如果在状态栏上的图标不是绿 ...
- 从Webpack源码探究打包流程,萌新也能看懂~
简介 上一篇讲述了如何理解tapable这个钩子机制,因为这个是webpack程序的灵魂.虽然钩子机制很灵活,而然却变成了我们读懂webpack道路上的阻碍.每当webpack运行起来的时候,我的心态 ...
- Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法
2019独角兽企业重金招聘Python工程师标准>>> Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法 博客分类: java 搜索引擎 ...
- 积性函数初步(欧拉$\varphi$函数)
updata on 2020.4.3 添加了欧拉\(\varphi\)函数为积性函数的证明和它的计算方式 1.积性函数 设\(f(n)\)为定义在正整数上的函数,若\(f(1)=1\),且对于任意正整 ...
- 题目分享k
题意:开关问题,有n只奶牛朝前或朝后,要使这n只奶牛全部朝前,每次能且必须翻转k只奶牛,求在最少翻转次数下的最小的k值,n≤5000 分析:n²暴力直接水过......枚举k值,对于每个k值因为最左边 ...
- django项目的uwsgi方式启停脚本
#!/bin/sh NAME="fushentang" if [ ! -n "$NAME" ];then echo "no arguments&quo ...
- 《Docker从入门到跑路》之镜像和容器的基本操作
一.获取镜像 官方提供了一个公共镜像仓库Docker Hub,默认是从这上面获取镜像的. 搜素镜像使用docker search 命令: # docker search --help Usage: d ...
- matlab-均值滤波
均值滤波 主要思想为邻域平均法,即用几个像素灰度的平均值来代替每个像素的灰度.有效抑制加性噪声.缺点:容易引起图像模糊,可以对其进行改进,主要避开对景物边缘的平滑处理. 均值滤波器的缺点是存在着边缘模 ...
- 带"反悔"的贪心-超市
题面:https://www.acwing.com/problem/content/description/147/ 超市里有N件商品,每个商品都有利润pi和过期时间di,每天只能卖一件商品,过期商品 ...