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×1050.123\times 10^50.123×10​5​​ 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 NNN, AAA and BBB, where NNN (<100<100<100) is the number of significant digits, and AAA and BBB are the two float numbers to be compared. Each float number is non-negative, no greater than 1010010^{100}10​100​​, 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.d[1]...d[N]*10^k (d[1]>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

我的解答:

 #include<cstdio>
#include<cstring>
const int maxn=;
int n; struct bign{
int d[maxn]; //记录 0.xxxx 记录maxn次就会
int len; // 表示幂级次数
bign(){
len=;
memset(d,,sizeof(d));
}
}; bign change(char str1[]){
bign a;
int len1 = strlen(str1);
bool flag = false; // 标识是否找到了首位位置
int i=,k=; // i反馈目前str1的执行位置;最终停在flag==true的时候
int dot_p = len1; //记录小数点dot的位置,默认他的值在最后。
int first_p = len1; // 默认第一个数值是在最后,也就是此数值为空。这样可以减少逻辑判断
while(i < len1){
// 确定小数点位置,只有一个,如果到最后都没有找到,那就默认是在尾部
if(str1[i] == '.'){
dot_p = i;
}
// 确定首个非零非dot位置
if(flag==false && str1[i] - '' >= && str1[i] - '' <= ){
flag = true;
first_p = i;
}
// 保存有效数字,尾部不需要处理,默认就是0;
if(flag == true && str1[i] != '.'){
a.d[k++] = str1[i] - '';
}
i++;
}
// 利用小数点和首位有效数字位置,确定a.len的大小;分成三类情况
if(first_p == len1) a.len=; // 用于解决0,0.0这样的特殊情况;
else if(dot_p > first_p) a.len = dot_p - first_p; //对应大于1的情况
else if(dot_p < first_p) a.len = dot_p - first_p + ; //对应小于1的情况
return a;
} bool isequal(bign a, bign b){
bool flag = true;
if(a.len == b.len){
for(int i=;i<n;i++){
if(a.d[i] != b.d[i]){
flag = false;
break;
}
}
}else{
flag = false;
}
return flag;
} void print_a(bign a){
printf("0.");
for(int i=;i<n;i++){
printf("%d",a.d[i]);
}
printf("*10^%d",a.len);
} int main(){
char str1[maxn],str2[maxn];
scanf("%d%s%s",&n,str1,str2);
bign a = change(str1);
bign b = change(str2);
bool res = isequal(a, b);
if(res){
printf("YES ");
print_a(a);
}else{
printf("NO ");
print_a(a);
printf(" ");
print_a(b);
}
return ;
}

总结:

1 教材中推荐的方法是使用string,上学的时候我特别喜欢用string。本题刚开始只是想学以致用,希望利用自己构造的结构体解决问题。并且认为自己构造的结构体只需要遍历一次就可以获取到所有计算必要的元素。从而得到结果。

2 从使用的结果可见,在打印结果和比较结果的过程中,比较麻烦。但是基本不需要动脑,按照惯例抒写即可。

3 而课本说的方法,选择逐步完成,第一次遍历需要去除string左边连续是0的部分,直到出现正整数 or dot;(实际上这次遍历复杂度只是1而已)

第二次遍历是直接计算出len,依然需要借助dot的位置,分情况讨论。并且获得无dot的有效数字部分;

第三次遍历是不足右边有效数字部分;同样,对于0,0.0的情况一定要特殊处理。

4 另一方面,应该特别重视s.erase(s.begin());的用法;也是非常理想的容器。经过时间测算,方法一确实要比string快很多。

PAT A1060 (Advanced Level) Practice的更多相关文章

  1. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  2. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  3. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  4. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  5. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

  6. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  7. PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...

  8. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

  9. PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...

随机推荐

  1. M-wordL-图

    典型的需要使用图模型 将start 和 end 以及字典一同构建成图,然后探究从start到end的最短路径

  2. 二叉树的二叉链表存储结构及C++实现

    前言:存储二叉树的关键是如何表示结点之间的逻辑关系,也就是双亲和孩子之间的关系.在具体应用中,可能要求从任一结点能直接访问到它的孩子. 一.二叉链表 二叉树一般多采用二叉链表(binary linke ...

  3. WIN7与WIN10 安装

    ---恢复内容开始--- 开始的操作系统是黑白屏的DOS,随着光标的一闪一闪并逐渐后移,一条条指令输入电脑,并执行相关指令完成任务.慢慢的,视窗操作系统最初是基于DOS的windows 9X内核WIN ...

  4. photoshopcs6破解补丁用来干嘛的

    photoshopcs6破解补丁为 Adobe CS6 系列软件通用破解补丁,亲测可用,终于能用了不再出现那个烦人的购买页面了,cs6破解补丁解压后得到32和64两个文件夹,根据自己的系统类型选择,6 ...

  5. 32位 64位 获得进程peb的方法

    基于上一篇文章,大概了解了peb的获取方法,但是那个方法只能获得当前进程的PEB,不能获得其他的进程的PEB.根据那个思想,获得其他进程PEB则需要注入,得到进程信息,然后进程间通信,将信息返回来,经 ...

  6. Sqlserver2008+搜索型注入技术

    简单的判断搜索型注入漏洞存在不存在的办法是先搜索',如果出错,说明90%存在这个漏洞.然后搜索%,如果正常返回,说明95%有洞了. 然后再搜索一个关键字,比如2006吧,正常返回所有2006相关的信息 ...

  7. slf4j-log4j12-1.5.8.jar有什么用

    slf4j是hibernate的日志接口,通常我们用log4j.jar来实现hibernate的记录日志功能,slf4j-log4j.jar可以看成是用来把slf4j的接口转换成适合log4j的接口的 ...

  8. 简要总结 数据仓库VS数据库

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wl101yjx/article/details/31015367 本文简要总结以下两个问题,旨在高速 ...

  9. 二·安装Subversion(基于Centos7)

    1.在网站http://archive.apache.org/dist/subversion/中下载对应的版本,我的操作系统是centos, 所以我下载了Linux generic版本subversi ...

  10. Kali Linux重设root密码

    许久不用的Kali,某天打开竟忘了密码! 网上的方法颇为简单,遂准备亲自试一下. #光标移动到第二行的“恢复模式”,按E进入[编辑模式]                                 ...