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×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 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 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

解析:

然后设计一个函数,对于输入的数字(存储在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<bits/stdc++.h>
using namespace std;#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}const int 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]; // 对于特定的精度,有数字则填入相应数字,没有则补0elser.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^-1elser.k = pointPos - firstPos; // 正次方,例如21.25,pointPos = 2,firstPos = 0,2-0=2,0.2125*10^2if (index == 0){ // 如果index = 0,代表值为0,则每一位都写0,再加\0int 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);elseprintf("NO 0.%s*10^%d 0.%s*10^%d\n", r1.d, r1.k, r2.d, r2.k);return 0;
}

1060 Are They Equal相关推荐

  1. PAT甲级1060 Are They Equal:[C++题解]字符串处理、有效数字、代码简洁!!!

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:字符串处理 题意:把一个数变成0.xxxxx * 10^xxxxx 的形式(小数点后面第一个是大于零的数,除非输入的数本来就是0),位 ...

  2. PAT 1060 Are They Equal (25 分)

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

  3. 【科学计数法模板讲解】1060 Are They Equal (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 If a machine can save only 3 significant digits, the float number ...

  4. PAT-A 1060 Are They Equal (25 分)

    If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...

  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. 1060 Are They Equal (25 分)【难度: 一般 / 知识点: 模拟 字符串处理】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805413520719872 细节很多,需要注意一下几个情况: 12345 ...

  7. PAT甲级题目翻译+答案 AcWing(字符串处理)

    1001 A+B Format (20 分) 题意 :将整数转换成标准格式 思路 :从后往前遍历字符串进行模拟,每三个数字加一个逗号,但不能是在最前面加逗号,也不能是加在负号后面 #include & ...

  8. 【最新合集】PAT甲级最优题解(题解+解析+代码)

    以下每道题均是笔者多方对比后, 思考整理得到的最优代码,欢迎交流! 共同成长哇.可以和博主比拼一下谁刷的更快~ 欢迎收藏.欢迎来玩儿 PAT题解目录 题号 标题 题解 分类 使用算法 1001 A+B ...

  9. 【PAT甲级】字符串处理及进制转换专题

    目录 字符串处理 PAT甲级 1001 A+B Format (20 分) PAT甲级1005 Spell It Right (20 分) PAT甲级1035 Password (20 分) PAT甲 ...

最新文章

  1. buffer busy waits等待事件的原因:hot block [转]
  2. Windows10远程访问Jupyter notebook
  3. c盘所有的html文件全删,我将C盘文件夹全删了
  4. 2019年10月数据库流行度排行:国产数据库鲲鹏正举 PostgreSQL同比增幅第一
  5. html转换jquery,将html字符串转化为jquery对象
  6. 读《scikiit-learn机器学习》第七章_决策树
  7. 中国传统节日端午节网页HTML代码 学生网页课程设计期末作业下载 春节大学生网页设计制作成品下载 DW春节节日网页作业代码下载
  8. 你知道硬齿面减速机价格为什么比齿轮减速机,蜗轮蜗杆减速机高?
  9. 神经元模型和BP网络
  10. 李飞飞、吴恩达、Bengio等人的15大顶级深度学习课程(转)
  11. uniapp拍照上传照片流程笔记
  12. mac升级系统mysql无法启动解决
  13. 【多图】二进制的起源,从01到创造天地万物
  14. 高光谱图像 空间分辨率
  15. java:数学运算的取最大、最小、绝对值的函数方法
  16. 交互工具 Framer 中文网全面更新,你可以分享灵感啦
  17. 新版傻妞升级之后 登录 查询无反应
  18. GPL协议允许你怎么卖?
  19. Flink入门视频教程(菜鸟窝出品)
  20. 自动登录武汉理工大学鉴湖宿舍校园网的方式-苹果/ipad端

热门文章

  1. spring mvc + freemarker 整合
  2. leetcode523 Continuous Subarray Sum
  3. ubuntu 中 iptables 和 ufw 的关系
  4. SEO是企业发展的永恒主题
  5. Node.js入门经典 读书笔记(3)
  6. PHP的isset()和empty()比较
  7. Git bash:初步入门(1)
  8. Linux0.11内核--系统中断处理程序int 0x80实现原理
  9. MATLAB函数记录
  10. 【Python】判断列表 list 是否为空