【算法笔记】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 # ...
随机推荐
- code1167 树网的核
floyd+枚举 看点: 1.floyd同时用数组p记录转移节点k,这样知道线段的端点u v就可以得到整条线段 2.任意一点c到线段a b的距离=(d[a][c]+d[c][b]-d[a][b])/2 ...
- Mybatis的map用法
当Mybatis传过来的值是map类型的时候,有两种处理方法 1.将数值装入类封装起来 public interface IStudentDao { // 根据姓名和年龄查询 List<Stud ...
- Log4Net配置使用简记
1,引用Log4Net.dll .当前为2.0.8.0版,可添加Nuget包.我的办法是从下载的包中直接引用相应.net版本的dll以减小项目体积 2,在App.config中增加<sectio ...
- Swift实现UIKit Dynamic动画
iOS7引入了UIKit Dynamics,可以帮助开发者开发出更接近真实世界的动画效果.之前,用户如果要做出这样的效果,需要话很多的时间在物理计算和Core Animation上.现在,所有的一切都 ...
- Android-自定义控件-继承View与ViewGroup的初步理解
继承View需要走的流程是: 1.构造实例化, public ChildView(Context context, @Nullable AttributeSet attrs) 2.测量自身的高和宽on ...
- iperf命令 +speedtest-cli
iperf工具可以用来测量TCP或者是UDP的网络吞吐量,即bandwidth(带宽)测试. iPerf的主要目标是帮助调整特定路径上的TCP连接. 众所周知,TCP最基本的调整问题是调整TCP窗口 ...
- Docker Warning : the backing xfs filesystem is formatted without d_type support
CentOS7 下安装配置 Docker,遇到如下的WARNING, WARNING: overlay: the backing xfs filesystem is formatted without ...
- JS三个编码函数和net编码System.Web.HttpUtility.UrlEncode比较
JS三个编码函数和net编码比较 总结 1.escape.encodeUri.encodeUriComponent均不会对数字.字母进行编码.2.escape:对某些字符(如中文)进行unicode编 ...
- C#中==操作符存在的缺陷
==操作符因为语法简洁而备受欢迎,但它本身也存在着局限性,比如继承或泛型问题.下面让我们依次来看看吧. 1.==和继承性问题 关于==运算符在继承时存在的问题,我们以String类型为例进行说明. s ...
- django drf unique_together和UniqueTogetherValidator
联合唯一可以使用django中的unique_together,和DRF中的UniqueTogetherValidator->https://www.django-rest-framework. ...