题目如下:

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

这个题目的难点在于要处理的数不一定都是大于1的,还可能出现0.015这样的数字,我之前的代码只能处理大于1的情况,后来参考了小5555的代码,发现可以用两个游标指示小数点和第一个非0数的位置,然后判断二者的位置关系来判断数的值。

首先定义一个结构体,来存储基数和次方:

struct result{
char d[MAX]; // 0.xxx部分
int k; // 10的k次方
};

然后设计一个函数,对于输入的数字(存储在char*内)数组进行遍历。

遍历时定义firstPos记录第一个非0数,pointPos记录小数点位置。

需要注意以下问题:

①题目的case似乎出现了00123.45这种坑爹的情况,因此要处理无效的0,也就说开头出现的0是不能要的,只有碰到第一个非0才记录firstPos。

②对于题目给定的精度,如果输入的数字位数不够,要补0,在结尾处还要补\0使得字符串可靠结束。

③处理完毕后,对于firstPos和pointPos做比较,如果前者小,说明是大于1的数,pointPos - firstPos即为10的几次方;如果后者小,说明是小数,应该用firstPos - pointPos + 1,+1是因为firstPos越过了小数点,而小数点不能算作一位,注意的是这个次方是负值。

④比较相等时,如果基数部分一致、次方也一致,才算相等。

代码如下:

#include <stdio.h>
#include <string.h>
#define MAX 110
struct result{
char d[MAX]; // 0.xxx部分
int k; // 10的k次方
}; result getResult(char *a, int n){
result r;
int firstPos = -1;
int pointPos = -1;
int index = 0;
int i;
for (i = 0; a[i]; i++){
if (a[i] == '.'){
pointPos = i;
continue;
}
else if (a[i] == '0' && firstPos == -1) // 不能以0开头,否则忽略
continue;
else{
if (firstPos == -1)
firstPos = i; // 第一个非0数字的位置
if (index < n)
{
if (index < strlen(a))
r.d[index++] = a[i]; // 对于特定的精度,有数字则填入相应数字,没有则补0
else
r.d[index++] = '0';
}
}
}
r.d[index] = 0; // 在数字结尾加\0,防止越界
if (pointPos == -1)
pointPos = i; // 如果没有找到小数点,则小数点在最后,这是个纯整数
if (pointPos - firstPos < 0) // 判断小数点与第一个非0数字的位置关系,计算10的几次方
r.k = - (firstPos - pointPos - 1); // 负次方,例如0.015,pointPos = 1, firstPos = 3, 3 - 1 - 1 = 1, -1是因为多算了小数点进去,0.15*10^-1
else
r.k = pointPos - firstPos; // 正次方,例如21.25,pointPos = 2,firstPos = 0,2-0=2,0.2125*10^2
if (index == 0){ // 如果index = 0,代表值为0,则每一位都写0,再加\0
int i;
for (i = 0; i != n; i++)
r.d[i] = '0';
r.d[i] = 0;
r.k = 0;
}
return r;
} int main(){
int n;
char a[MAX], b[MAX];
scanf("%d%s%s", &n, a, b);
result r1 = getResult(a, n);
result r2 = getResult(b, n);
if (strcmp(r1.d, r2.d) == 0 && r1.k == r2.k)
printf("YES 0.%s*10^%d\n", r1.d, r1.k);
else
printf("NO 0.%s*10^%d 0.%s*10^%d\n", r1.d, r1.k, r2.d, r2.k);
return 0;
}

1060. Are They Equal (25)的更多相关文章

  1. 【PAT】1060 Are They Equal (25)(25 分)

    1060 Are They Equal (25)(25 分) If a machine can save only 3 significant digits, the float numbers 12 ...

  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. 1060 Are They Equal (25 分)

    1060 Are They Equal (25 分)   If a machine can save only 3 significant digits, the float numbers 1230 ...

  4. 1060 Are They Equal (25分)

    1060 Are They Equal (25分) 题目 思路 定义结构体 struct fraction{ string f; int index; } 把输入的两个数先都转换为科学计数法,统一标准 ...

  5. pat 1060. Are They Equal (25)

    题目意思直接,要求将两个数转为科学计数法表示,然后比较是否相同  不过有精度要求 /* test 6 3 0.00 00.00 test 3 3 0.1 0.001 0.001=0.1*10^-2 p ...

  6. PAT (Advanced Level) 1060. Are They Equal (25)

    模拟题.坑点较多. #include<iostream> #include<cstring> #include<cmath> #include<algorit ...

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

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

  8. 【PAT甲级】1060 Are They Equal (25 分)(需注意细节的模拟)

    题意: 输入一个正整数N(<=100),接着输入两个浮点数(可能包含前导零,对于PAT已经习惯以string输入了,这点未知),在保留N位有效数字的同时判断两个数是否相等,并以科学计数法输出. ...

  9. PAT 甲级 1060 Are They Equal

    1060. Are They Equal (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue If a ma ...

随机推荐

  1. C语言程序设计第五次作业——循环结构1

    (一)改错题 输出华氏摄氏温度转换表:输入两个整数lower和upper,输出一张华氏摄氏温度转换表,华氏温度的取值范围是{lower,upper},每次增加2℉.计算公式如下: c = 5×(f-3 ...

  2. TensorFlow 中文资源全集,官方网站,安装教程,入门教程,实战项目,学习路径。

    Awesome-TensorFlow-Chinese TensorFlow 中文资源全集,学习路径推荐: 官方网站,初步了解. 安装教程,安装之后跑起来. 入门教程,简单的模型学习和运行. 实战项目, ...

  3. jieba库分词统计

    代码在github网站,https://github.com/chaigee/chaigee,中的z3.py文件 py.txt为团队中文简介文件 代码运行后词频统计使用xlwt库将数据发送到excel ...

  4. [原创]手把手教你写网络爬虫(7):URL去重

    手把手教你写网络爬虫(7) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 本期我们来聊聊URL去重那些事儿.以前我们曾使用Python的字典来保存抓取过的URL,目的是将重复抓取的UR ...

  5. 欢迎大家来到DevLegal的博客

    申明:以下博文欢迎转载,转载请注明出处和作者 若非作者原创,则会标记出处与原作者,其余则为原创. 本人在校大学僧一枚,软件工程专业,希望通过将自己的学习心得与体会写出来,勉励自己学习,同时与大家一起分 ...

  6. 底部导航栏实现一 Fragment-replace

    [效果](这里下载的软件收费的试用有水印) [推荐]这里推荐一个图标网http://iconfont.cn/.以上图标来自此图标网 [项目结构] [步骤] ①创建布局文件,写底部导航栏 <?xm ...

  7. MyBatis批量新增和更新

    之前有开发任务一个接口里面有大量的数据新增和更新操作,导致十分缓慢.使用了批量操作之后速度有明显提升,几乎百倍千倍的速度提升. 博主之前统计过,通过普通接口一次数据库插入大概需要200ms,对于大量新 ...

  8. 对 /dev/shm 认识

    一./dev/shm理论 /dev/shm/是linux下一个非常有用的目录,因为这个目录不在硬盘上,而是在内存里.因此在linux下,就不需要大费周折去建 ramdisk,直接使用/dev/shm/ ...

  9. 网络爬虫框架Scrapy简介

    作者: 黄进(QQ:7149101) 一. 网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本:它是一个自动提取网页的程序,它为搜索引擎从万维 ...

  10. Windows编译ejabberd

    Windows编译ejabberd(金庆的专栏)安装 erlang OTP. 添加路径到 PATH, 使 erl 可以运行.git clone ejabberd安装 rebar:git clone g ...