立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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


科学计数法计算逻辑:
1、找小数点位置。
2、找某数第一个不为0数字的位置。
3、将第一个不为0数字后的数字加入temp(有效数字)
4、第二点减第一点为指数值
5、判断指数和有效数字是否相等即可。

小傻瓜们还在苦苦写模拟, 而大lao们早就用上了模板~


#include<bits/stdc++.h>
using namespace std;int f(const string&s, string&temp, int N) {int point = s.size(), index = -1;  //小数点位置、第一个非0数字位置for(int i = 0; i < s.size(); i++) {if(s[i] == '.')   //找小数点 point = i;//若第一个不为0的位置找到,则将其后的数字加入temp else if(index != -1 && temp.size() < N)    temp += s[i];else if(index == -1 && s[i] != '0') { //找第一个不为0的位置 index = i;temp += s[i];}} while(temp.size() < N)  //如果temp长度小于N,那么+0temp += "0";if(index == -1) //没有找到非零数字,说明字符串s表示的数是0return 0; point -= index;        //小数点减去第一排非零数字位置得到指数 return point < 0 ? point+1 : point;    //如果为负数,返回point+1,否则返回point
}int main() {int N;string A, B, Atemp="", Btemp="";cin >> N >> A >> B;               //读取数据int Aexp = f(A, Atemp, N), Bexp = f(B, Btemp, N); if(Aexp == Bexp && Atemp == Btemp)   //若有效数字和指数皆相同cout << "YES 0." << Atemp << "*10^" << Aexp;else cout << "NO 0." << Atemp << "*10^" << Aexp << " 0." << Btemp << "*10^" << Bexp; return 0;
}

耗时:


求赞哦~ (✪ω✪)

【科学计数法模板讲解】1060 Are They Equal (25 分)相关推荐

  1. PAT 1060 Are They Equal (25 分)

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

  2. 1060 Are They Equal (25 分)【难度: 一般 / 知识点: 模拟 字符串处理】

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

  3. 科学数字_Excel分列时拒绝让超过15位的数字变成科学计数法

    分列时让超过15位的数字不变成科学计数法 Excel情报局 生产搬运分享Excel基础技能 Excel知识青年 用1%的Excel基础搞定99%的日常工作 做一个有文艺范的Excel公众号 Excel ...

  4. 二进制的科学计数法?白话谈谈计算机如何存储与理解小数:IEEE 754

    浮点数的计算机表示(IEEE 754),由 UCB 数学教授 William Kahan 主要起草.后者也因其卓越贡献于1989年获得图灵奖.计算机组成原理与汇编语言这两门课均对该内容有所讲解.与课程 ...

  5. Oracle ERP 报表:类数字格式字符串数据的前置0被截取和被科学计数法表示

    在开发报表的时候,遇到了以下情况: A.问题描述 在PLSQL中编写程序输出HTML/XML格式的报表,在提交请求之后查看输出的时候,如果选择使用Excel打开,类似数字格式的字符串数据会被Excel ...

  6. 指数(乘方、幂、科学计数法)、对数、等比数列、阶乘基础知识

    指数 指数.幂:乘方.次方.a的n次方,a的n次幂,a^n.数学计算为n自乘m次.其中n称作底数,m称作指数. n^0= 1;     n^1=1*n;     n^2=1*n*n 科学计数法:a*( ...

  7. 科学记数法怎么做iPhone计算机上运用,怎么把科学计数法直接打开

    1. Excel中,如何把科学记数法转换成数字 准备工具/材料:装有windows 10的电脑一台,Microsoft Office 家庭和学生版 2016 excel软件. 1.让我们先看看2016 ...

  8. python科学计数法转换_对比Python学习Go 基本数据结构

    公众号文章不方便更新,可关注底部「阅读原文」博客,文章随时更新. 本篇是「对比 Python 学习 Go」[1] 系列的第三篇,本篇文章我们来看下 Go 的基本数据结构.Go 的环境搭建,可参考之前的 ...

  9. 【PAT (Basic Level) 】1024 科学计数法 (20 分)

    科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [±][1-9].[0-9]+E[±][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指数部 ...

最新文章

  1. python画饼图-从零开始学Python--matplotlib(饼图)
  2. 数据结构和算法设计专题之---判断单链表中是否有环,环的长度,环的入口节点...
  3. (GO_GTD_1)基于OpenCV和QT,建立Android图像处理程序
  4. war包部署-排除内嵌的tomcat
  5. linux之通过htop操作进程使用总结
  6. python历史波动率_历史波动率计算(旧文)
  7. 训练caffe:registry.count(type) == 0 (1 vs. 0) Solver type Nesterov already registered
  8. 苹果正开发更轻薄MacBook Air 且配备MagSafe
  9. mysql 容灾 灾备 备份
  10. [转载] Python判断分数等级if...elif...else
  11. Eclipse 使用和问题总结
  12. 中国工业企业数据库stata处理
  13. 短链接生成接口、长链接转换短链接,可根据ip归属地个性化跳转、随机跳转
  14. blockUI弹出层
  15. Mugeda(木疙瘩)H5案例课—教你玩转密室逃脱类H5-岑远科-专题视频课程
  16. 虚拟机安装与双系统(win10+ubuntu)安装及其他
  17. Nature综述:一文揭秘土壤微生物的生死过程如何影响生物地球化学
  18. 【三星篇】三星手机实用功能软件推荐
  19. 我的世界无限贪婪服务器,我的世界无尽贪婪mod
  20. 帮你快速拿Offer!Android攒了一个月的面试题及解答,含BATJM大厂

热门文章

  1. SurfaceView 和 GLSurfaceView
  2. Python 的AES加密与解密
  3. 用Python实现二叉树的遍历
  4. MySql数据库中的子查询使用
  5. 字节、阿里等大厂的技术如何?看看这些Java程序员的自学笔记
  6. Python中的匿名函数和函数式编程
  7. 腾讯高性能分布式路由技术,亮相亚太网络研讨会APNet
  8. srs代码学习(4)-怎么转发流
  9. C语言文件操作 fopen, fclose, mkdir(打开关闭文件,建文件夹,判断文件是否存在可读或可写)
  10. java异常 The origin server did not find a current representation for the target resource or is not