1060 Are They Equal (25 分)
 

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 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 (<) 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 1, 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

题意:

给出两个数,问:将他们写成保留N (<100)位小数的科学计数法 0.d[1]...d[N]*10^k (d[1]>0 unless the number is 0); 后,是否相同。若相等,输出YES,并转换结果; 如果不相等,输出NO,并给出两个数的转换结果。

题解:

本题的思路难想而且情况判断非常复杂。

题目要求科学计数法时,两个数是否相等。因此只要判断科学技术法时的本体部分以及指数部分是否相等即可。

对于数据来说,要分为大于 1 与小于 1 来判断,要考虑各种情况下的数字,比如:0000, 000.00, 00123.4, 0.012, 0.00 等

一开始没有考虑太多的异常情况,拿了19分,第二天看了题解的注意点重做,还是只有21分,

第三天理了理思路,其实,只要关注,格式化好后的小数点要点在哪里,次数是多少就可以了,在此基础上,特判0,去掉前导零。比较的时候注意不要超限,输出不够0补齐。

自己编的测试样例:

 0.0015 0000.001520000
YES 0.15*^-
 010.25 0010.23
YES 0.102*^
 0.1 00.100
YES 0.10000*^
 000.0012 0000000.0012000000000
YES 0.1200000000*^-
  0.000
YES 0.000*^

AC代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
char a[];
char b[];
int main(){
cin>>n>>a>>b;
int la=strlen(a);
int lb=strlen(b);
int sta=-,stb=-;
int isa0=,isb0=;//特判是不是0 //从第一个不是0的数开始
for(int i=;i<la;i++){
if(a[i]!='.'&&a[i]!=''){
sta=i;
break;
}
}
for(int i=;i<lb;i++){
if(b[i]!='.'&&b[i]!=''){
stb=i;
break;
}
}
if(sta==-) isa0=;
if(stb==-) isb0=; //小数点前有几位
int ia=,ib=;
int f=;
for(int i=;i<la;i++){
if(a[i]=='.') break;
if(a[i]!='') f=;
if(f){
ia++;
}
}
f=;
for(int i=;i<lb;i++){
if(b[i]=='.') break;
if(b[i]!='') f=;
if(f){
ib++;
}
}
//cout<<"sta:"<<sta<<" ia:"<<ia<<" ha:"<<ha<<endl;
//cout<<"stb:"<<stb<<" ib:"<<ib<<" hb:"<<hb<<endl; //比较
f=;
int pa=sta,pb=stb;
if(isa0!=isb0 || ia!=ib) f=;
for(int i=;i<n;i++){
if(pa+i>=la||pb+i>=lb) break;
if(a[pa]=='.') pa++;
if(b[pb]=='.') pb++;
//cout<<"pa "<<pa+i<<" a[pa] "<<a[pa+i]<<endl;
//cout<<"pb "<<pa+i<<" b[pb] "<<b[pa+i]<<endl;
if(a[pa+i]!=b[pb+i]){
f=;
break;
}
}
if(isa0==&&isa0==isb0) f=;//如果两个都是0 //输出
if(f){
cout<<"YES 0.";
if(isa0==){//0的特判
for(int i=;i<=n;i++) cout<<"";
cout<<"*10^0";
}else{
pa=sta;
for(int i=;i<n;i++){
if(pa+i>=la) {
cout<<"";
continue;
}
if(a[pa+i]=='.') pa++;
if(pa+i>=la) cout<<"";
else cout<<a[pa+i];
}
cout<<"*10^";
if(ia!=) cout<<ia;//次数>=于0
else{//次数<0
int k=-;
for(int i=;i<la;i++){
if(a[i]=='.'){
k=i;
break;
}
}
cout<<k-sta+;//.的位置-开始的位置+1
}
}
}else{//同上
cout<<"NO 0.";
if(isa0==){
for(int i=;i<=n;i++) cout<<"";
cout<<"*10^0 ";
}else{
for(int i=;i<n;i++){
if(pa+i>=la) {
cout<<"";
continue;
}
if(a[pa+i]=='.') pa++;
if(pa+i>=la) cout<<"";
else cout<<a[pa+i];
}
cout<<"*10^";
if(ia!=) cout<<ia<<" 0.";
else{
int k=-;
for(int i=;i<la;i++){
if(a[i]=='.'){
k=i;
break;
}
}
cout<<k-sta+<<" 0.";
}
}
if(isb0==){
for(int i=;i<=n;i++) cout<<"";
cout<<"*10^0";
}else{
for(int i=;i<n;i++){
if(pb+i>=lb) {
cout<<"";
continue;
}
if(b[pb+i]=='.') pb++;
if(pb+i>=lb) cout<<"";
else cout<<b[pb+i];
}
cout<<"*10^";
if(ib!=) cout<<ib;
else{
int k=-;
for(int i=;i<lb;i++){
if(b[i]=='.'){
k=i;
break;
}
}
cout<<k-stb+;
}
}
}
return ;
}

PAT 甲级 1060 Are They Equal (25 分)(科学计数法,接连做了2天,考虑要全面,坑点多,真麻烦)的更多相关文章

  1. 1060 Are They Equal (25 分)

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

  2. 1060 Are They Equal (25分)

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

  3. PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)

    PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...

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

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

  5. 【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 ...

  6. PAT 甲级 1060 Are They Equal

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

  7. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  8. PAT 甲级 1083 List Grades (25 分)

    1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...

  9. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

随机推荐

  1. <<回想>>

    算是一个简单的回忆录,文笔很差,愧对语文老师 突然发现上一篇回忆录,没错就是那个流水账,是去年今天写的...   这是2019年7月的一天,NOI2019刚刚落下帷幕,而小F,则百无聊赖地在高铁站等车 ...

  2. 大数据之路week06--day07(完全分布式Hadoop的搭建)

    前提工作: 克隆2台虚拟机完成后:新的2台虚拟机,请务必依次修改3台虚拟机的ip地址和主机名称[建议三台主机名称依次叫做:master.node1.node2 ] 上一篇博客 (三台虚拟机都要开机) ...

  3. Codeforces Round #609 (Div. 2) C. Long Beautiful Integer

    链接: https://codeforces.com/contest/1269/problem/C 题意: You are given an integer x of n digits a1,a2,- ...

  4. Selenium常用API的使用java语言之14-多窗口切换

    在页面操作过程中有时候点击某个链接会弹出新的窗口, 这时就需要主机切换到新打开的窗口上进行操作.WebDriver提供了switchTo().window()方法可以实现在不同的窗口之间切换. 以百度 ...

  5. Django --- 与数据库进行交互

    目录 1.静态文件配置 1.什么是静态文件 2.为什么用户在浏览器中输入的网址能够访问到响应的资源?有时候不能访问? 3.如果想要访问静态资源怎么做? 4.手动开设静态文件访问资源 5.关于两个sta ...

  6. jquery统计输入文字的个数并对其进行判断

    <textarea placeholder="该产品满足你的期待吗?说说你的使用心得,分享给 同样看中的他们吧"></textarea> <span ...

  7. [Cypress] install, configure, and script Cypress for JavaScript web applications -- part5

    Use the Most Robust Selector for Cypress Tests Which selectors your choose for your tests matter, a ...

  8. 011_GoldWave软件安装及使用

    (一)软件安装包: 链接:https://pan.baidu.com/s/15c5veooyA8bAYIAgLFOLjg提取码:jiis 复制这段内容后打开百度网盘手机App,操作更方便哦 (二)降低 ...

  9. 发布一个npm package

    1. 创建一个package.json文件 发布到npm registry的包必须包含一个packge.json文件. 1. 必需name字段 要求: 1. 只能是一个单词,但是可以包含-或_ 2. ...

  10. angular2-cli 安装

    1.如果你之前安装失败过,最好在安装angular-cli之前先卸载干净,用以下两句: npm uninstall -g angular-cli npm cache clean  2.设置淘宝镜像,国 ...