【算法笔记】A1060 Are They Equal
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
题意
比较两个数的科学计数法是否相同,相同输出YES,否则输出NO。本题不需要考虑四舍五入。
思路
把接收到的两个数删除掉前导的0。然后分为两种情况:一种是.00XXXX形式;另一种是XXXX.XXXX形式。
如果是.00XXXX形式,删除小数点和小数点后面的0,直到碰到不为0的数,每删一个0,指数减1。
如果是XXXX.XXXX形式,遍历字符串直到碰到小数点,把小数点删除。每遍历一个数,指数加1。
最后比较改变后的字符串和指数,输出结果。注意输入 000.0 时输出 YES 0.000*^
code:
#include<bits/stdc++.h>
using namespace std;
string num1, num2;
int n;
static string turn(string a, int &e){//e前面加&,值才会被修改
string result="0.";
while(a.size()>&&a[]==''){//删除前导0
a.erase(a.begin());
}
if(a[]=='.'){//0.XXXXXX形式
a.erase(a.begin());
while(a.size()> && a[]==''){
a.erase(a.begin());
e--;
}
}else{//XXXX.XXXX形式
int k = ;
while(k < a.size() && a[k]!='.'){
e++;
k++;
}
if(k<a.size()) a.erase(a.begin()+k);
}
if(a.size()==) e = ;//删除0和小数点后长度为0
int num = , k = ;
while(num < n){
if(k < a.size()) result += a[k++];
else result += '';
num++;
}
return result;
}
int main(){
int e1 = , e2 = ;
cin>>n>>num1>>num2;
string s1 = turn(num1,e1);
string s2 = turn(num2,e2);
if(s1 == s2 && e1 == e2)
cout<<"YES "<<s1<<"*10^"<<e1;
else
cout<<"NO "<<s1<<"*10^"<<e1<<" "<<s2<<"*10^"<<e2;
return ;
}
【算法笔记】A1060 Are They Equal的更多相关文章
- 学习Java 以及对几大基本排序算法(对算法笔记书的研究)的一些学习总结(Java对算法的实现持续更新中)
Java排序一,冒泡排序! 刚刚开始学习Java,但是比较有兴趣研究算法.最近看了一本算法笔记,刚开始只是打算随便看看,但是发现这本书非常不错,尤其是对排序算法,以及哈希函数的一些解释,让我非常的感兴 ...
- 算法笔记--数位dp
算法笔记 这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392 数位dp的精髓是不同情况下sta变量的设置. 模板: ]; ...
- 算法笔记--lca倍增算法
算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...
- 算法笔记--STL中的各种遍历及查找(待增)
算法笔记 map: map<string,int> m; map<string,int>::iterator it;//auto it it = m.begin(); whil ...
- 算法笔记--priority_queue
算法笔记 priority_queue<int>que;//默认大顶堆 或者写作:priority_queue<int,vector<int>,less<int&g ...
- 算法笔记--sg函数详解及其模板
算法笔记 参考资料:https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html sg函数大神详解:http://blog.csdn.net/l ...
- 算法笔记——C/C++语言基础篇(已完结)
开始系统学习算法,希望自己能够坚持下去,期间会把常用到的算法写进此博客,便于以后复习,同时希望能够给初学者提供一定的帮助,手敲难免存在错误,欢迎评论指正,共同学习.博客也可能会引用别人写的代码,如有引 ...
- 算法笔记_067:蓝桥杯练习 算法训练 安慰奶牛(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是 ...
- 算法笔记(c++)--回文
算法笔记(c++)--回文 #include<iostream> #include<algorithm> #include<vector> using namesp ...
- 算法笔记(c++)--完全背包问题
算法笔记(c++)--完全背包和多重背包问题 完全背包 完全背包不同于01背包-完全背包里面的东西数量无限 假设现在有5种物品重量为5,4,3,2,1 价值为1,2,3,4,5 背包容量为10 # ...
随机推荐
- dedecms导出csv文件
1.mshd_orderlist.tpl <form id="frm" method="GET" action="mshd_orderlist. ...
- icp算法
https://github.com/tttamaki/ICP-test https://github.com/tttamaki/SICP-test
- Windows sql语句正则匹配导出数据到本地 The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
尝试使用 into outfile导出数据的时候出现错误: The MySQL server is running with the --secure-file-priv option so it c ...
- Navicet Mysql数据库电脑本地备份
Navicet Mysql数据库电脑本地备份 1.打开navicat客户端,连上mysql后,双击左边你想要备份的数据库.点击"计划",再点击"新建批处理作业" ...
- HDU1232 畅通工程 2017-04-12 19:20 53人阅读 评论(0) 收藏
畅通工程 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submissi ...
- [label][Apache] VirtualHost
<VirtualHost *:80> ServerName localhost DocumentRoot "D:\www"</VirtualHos ...
- EBS请求查找运行详细信息
--查找运行请求时间,参数等(可以是某用户的,某个报表) select c.user_name, papf.full_name, b.user_concurrent_progr ...
- How to Baskup and Restore a MySQL database
If you're storing anything in MySQL databases that you do not want to lose, it is very important to ...
- mysql查看数据库性能常用命令
mysql> show global status; 可以列出MySQL服务器运行各种状态值,另外,查询MySQL服务器配置信息语句: mysql> show variables; 一.慢 ...
- mysql导入数据load data infile用法(转)
们常常导入数据!mysql有一个高效导入方法,那就是load data infile 下面来看案例说明 基本语法: load data [low_priority] [local] infile ' ...