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

题意

  比较两个数的科学计数法是否相同,相同输出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的更多相关文章

  1. 学习Java 以及对几大基本排序算法(对算法笔记书的研究)的一些学习总结(Java对算法的实现持续更新中)

    Java排序一,冒泡排序! 刚刚开始学习Java,但是比较有兴趣研究算法.最近看了一本算法笔记,刚开始只是打算随便看看,但是发现这本书非常不错,尤其是对排序算法,以及哈希函数的一些解释,让我非常的感兴 ...

  2. 算法笔记--数位dp

    算法笔记 这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392 数位dp的精髓是不同情况下sta变量的设置. 模板: ]; ...

  3. 算法笔记--lca倍增算法

    算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...

  4. 算法笔记--STL中的各种遍历及查找(待增)

    算法笔记 map: map<string,int> m; map<string,int>::iterator it;//auto it it = m.begin(); whil ...

  5. 算法笔记--priority_queue

    算法笔记 priority_queue<int>que;//默认大顶堆 或者写作:priority_queue<int,vector<int>,less<int&g ...

  6. 算法笔记--sg函数详解及其模板

    算法笔记 参考资料:https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html sg函数大神详解:http://blog.csdn.net/l ...

  7. 算法笔记——C/C++语言基础篇(已完结)

    开始系统学习算法,希望自己能够坚持下去,期间会把常用到的算法写进此博客,便于以后复习,同时希望能够给初学者提供一定的帮助,手敲难免存在错误,欢迎评论指正,共同学习.博客也可能会引用别人写的代码,如有引 ...

  8. 算法笔记_067:蓝桥杯练习 算法训练 安慰奶牛(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是 ...

  9. 算法笔记(c++)--回文

    算法笔记(c++)--回文 #include<iostream> #include<algorithm> #include<vector> using namesp ...

  10. 算法笔记(c++)--完全背包问题

    算法笔记(c++)--完全背包和多重背包问题 完全背包 完全背包不同于01背包-完全背包里面的东西数量无限 假设现在有5种物品重量为5,4,3,2,1  价值为1,2,3,4,5  背包容量为10 # ...

随机推荐

  1. PHP 中 Traits 的简单使用

    PHP 5.4中的traits,是新引入的特性,中文还真不知道如何准确翻译好.其实际的目的,是为了有的场合想用多继承,但PHP又没多继承,于是就发明了这样的一个东西.       Traits可以理解 ...

  2. UX术语详解:任务流,用户流,流程图以及其它全新术语

    以下内容由Mockplus(摹客)团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 用户体验拥有一长串专业的术语和可交付内容.当在线查看UX相关职位描述时,所罗列的这类术语更是 ...

  3. 静态HTML服务器

    主要代码 #pragma once#include "pre.h"#include <thread> NAMESPACE(DEF) class Socket {publ ...

  4. background image

    http://www.ajaxblender.com/bgstretcher-2-jquery-stretch-background-plugin-updated.html http://blog.d ...

  5. .NET基础 (21)ASP NET应用开发

    ASP.NET中的WebForm相关的内容其实有点儿过时了,但在很多的老项目中还是WebForm的,这些都是遗留问题,新上的项目基本上都用MVC了,在微软最新的 ASP.NET 的版本中已经默认使用M ...

  6. Linux 基础教程 35-软件包管理-YUM

    YUM基础     使用RPM在Linux中安装.卸载软件或服务进会经常碰到RPM包的依赖,而我们在安装软件A时,提示依赖于软件B,安装软件B时又会出现提示依赖于软件C等一系列的依赖关系.这时大家会提 ...

  7. CYUSB3014固件部分低版本工程在Eclipse中编译得到img文件时无效的解决方案

    最近在做基于我们AC6102开发板的UVC图像视频方案,下载了官方的an75779应用工程,但是倒入到FX3—SDK自带的Eclipse中后,却无法编译生成img文件,经过比对后确认是生成该文件的命令 ...

  8. PipelineDB On Kafka

    PipelineDB 安装yum install https://s3-us-west-2.amazonaws.com/download.pipelinedb.com/pipelinedb-0.9.8 ...

  9. [Elixir003] Mix Archives

    在[Elixir001]中使用 mix escript.build 生成一个lifelog 的escript启动脚本. 今天我们尝试一下另一种方式:生成Archives. 我们先添加一个Task 1. ...

  10. solr特点七:Plugins(扩展点)

    http://wiki.apache.org/solr/SolrPlugins 在 Solr 1.3 中,扩展 Solr 以及配置和重新整理扩展变得十分简单.以前,您需要编写一个 SolrReques ...