【PAT】【Advanced Level】1060. Are They Equal (25)
来源:互联网 发布:php开发实战视频教程 编辑:程序博客网 时间:2024/05/21 10:26
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.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.9Sample Output 1:
YES 0.123*10^5Sample Input 2:
3 120 128Sample Output 2:
NO 0.120*10^3 0.128*10^3
https://www.patest.cn/contests/pat-a-practise/1060
https://www.nowcoder.com/pat/5/problem/4113
思路:
先处理小数点,再处理0,然后对齐,最后比较
花式if else
坑点:
0的处理
CODE:
#include<iostream>#include<cstring>#include<string>using namespace std;int main(){int n;string a,b;cin>>n>>a>>b;int w1=-1,w2=-1;for (int i=0;i<a.length();i++){if (a[i]=='.'){w1=i;break;}}for (int i=0;i<b.length();i++){if (b[i]=='.'){w2=i;break;}}if (w1!=-1)a=a.substr(0,w1)+a.substr(w1+1,a.length()-w1+1);elsew1=a.length();if (w2!=-1)b=b.substr(0,w2)+b.substr(w2+1,b.length()-w2+1);elsew2=b.length();int c1=-1,c2=-1;for(int i=0;i<a.length();i++){if (a[i]!='0') {c1=i;break;}w1--;}for(int i=0;i<b.length();i++){if (b[i]!='0') {c2=i;break;}w2--;}if (c1==-1){a="0";w1=0;}elsea=a.substr(c1,a.length()-c1+1);if (c2==-1){b="0";w2=0;} elseb=b.substr(c2,b.length()-c2+1);if (a.length()<n)for (int i=a.length();i<n;i++)a=a+'0';elsea=a.substr(0,n);if (b.length()<n)for (int i=b.length();i<n;i++)b=b+'0';elseb=b.substr(0,n);if (w1==w2 && a==b)cout<<"YES 0."<<a<<"*10^"<<w1;elsecout<<"NO 0."<<a<<"*10^"<<w1<<" 0."<<b<<"*10^"<<w2;return 0;}
- 【C++】PAT (advanced level)1060. Are They Equal (25)
- 【PAT】【Advanced Level】1060. Are They Equal (25)
- PAT (Advanced Level) Practise 1060 Are They Equal (25)
- PAT (Advanced Level) Practise 1060 Are They Equal (25)
- PAT (Advanced Level) 1060. Are They Equal (25) 字符串转科学计数法
- 1060. Are They Equal (25)【字符串处理】——PAT (Advanced Level) Practise
- Pat(Advanced Level)Practice--1060(Are They Equal)
- 1060. Are They Equal (25)-PAT
- pat 1060. Are They Equal (25)
- PAT A 1060. Are They Equal (25)
- PAT 1060. Are They Equal (25)
- PAT 1060. Are They Equal (25)
- PAT 1060. Are They Equal (25)
- PAT(A) - 1060. Are They Equal (25)
- 【PAT甲级】1060. Are They Equal (25)
- 1060. Are They Equal (25)PAT甲级
- PAT甲级1060. Are They Equal (25)
- PAT 1060. Are They Equal (25)
- kmp算法学习(未完成)
- C/S模型-TCP
- 设计模式学习笔记十一:适配器模式(Adapter Pattern)
- Android ijkplayer 编译的 bilibili 开源编译版本上,去掉失败的依赖,增加对 https 支持
- 用SQL命令手工创建CDB
- 【PAT】【Advanced Level】1060. Are They Equal (25)
- XMLHttpRequest对象
- Koa框架教程,Koa框架开发指南,Koa框架中文使用手册,Koa框架中文文档
- Pandas DataFrame 行列操作
- 传球游戏
- LeetCode 89 Gray Code (Python详解及实现)
- 系统编程之进程控制
- Android 数据库升级 数据迁移 数据不丢失
- 设计模式学习笔记十二:桥接模式(Bridge Pattern)