【算法笔记】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 # ...
随机推荐
- Java 设计模式系列(八)装饰者模式
Java 设计模式系列(八)装饰者模式 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案.Decorator 或 Wrapper 一.装饰模 ...
- oracle存储过程和游标参考
oracle open cursor forhttp://www.itpub.net/thread-1874683-1-1.html
- python int函数转换浮点型字符串的坑???
python中的int函数可以将数字或字符串转换为整型数字类型,具体功能就不提了 最近发现一个问题,对于字符串'1.1'之类的,int转换的时候会报异常,这是为什么,个人感觉直接转换成1不就行了,干嘛 ...
- java中null转换成其它类型
对null进行强转会不会抛错.测试结果是,如果把null强转给对象,是不会抛异常的,因为本身对象是可以为null的.但是如果是基本类型,比如 int i = (Integer)obj的强转,其实内部会 ...
- Python + selenium + unittest装饰器 @classmethod
前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间. 于是就想是不是可以 ...
- 9、Dockerfile语法
在Dockerfile中定义了很多关键字,通过关键字来完成Dockerfile的编写. Dockerfile官方文档 9.1 FROM 在Dockerfile中FROM主要是指定这个Doc ...
- Android-自定义侧滑菜单
效果图: 需要继承ViewGroup,因为包含了子控件,菜单子控件 与 主页面子控件 Activity Xml布局相关: <!-- 自定义侧滑菜单 SlideMenu --> <Li ...
- DateUtils常用方法
一.DateUtils常用方法 1.1.常用的日期判断 isSameDay(final Date date1, final Date date2):判断两个时间是否是同一天: isSameInst ...
- Spring学习(七)——开发Web Service的几种方式
本文作者在学习使用Java开发Web Service(不包括Restful)时,由于不知道Java有这么多框架支持开发Web Service一度陷入迷惘,不知道这些框架各有 什么不同,各有什么优缺点. ...
- airport 抓包
链接airport命令: ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources ...