【PAT】1060 Are They Equal (25)(25 分)

时间:2022-09-03 09:04:18
1060 Are They Equal (25)(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*10^5^ 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 10^100^, 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

科学计数法,注意以下几种情况:

  1.数字前有 0 的,比如:00056;0000.32 等,需先去除无效的0;

  2.指数 e 的正负计算,0.1 = 0.1*10^0;

  3.不同情况下有效位的计算,比如00056,0.00032;

  4.按题目要求输出有效位数,不足的后面补0;

C++代码如下:

 #include<iostream>
#include<string>
#include<iomanip>
using namespace std; void deal(string &s, int &e) {
while (s.length() > && s[] == '') s.erase(s.begin());
int i;
if (s[] == '.') {
s.erase(s.begin());
while (s.length()>&&s[] == '') {
e--;
s.erase(s.begin());
}
if (s.length() == ) e = ;
}
else {
for ( i = ; i < s.length(); i++) {
if (s[i] != '.') e++;
else break;
}
if(i<s.length()) s.erase(s.begin() + i);
}
}
void signdigit(string &s,int n) {
while (s.length() < n) s += '';
s.resize(n);
}
int main() {
string str1, str2;
int n,e1=,e2=;
cin >> n >> str1 >> str2;
deal(str1, e1);
deal(str2, e2);
signdigit(str1, n);
signdigit(str2, n);
if (str1 == str2 && e1 == e2)
cout << "YES 0." << str1 << "*10^" << e1 << endl;
else
cout << "NO 0." << str1 << "*10^" << e1 << " 0." << str2 << "*10^" << e2 << endl;
return ;
}

【PAT】1060 Are They Equal (25)(25 分)的更多相关文章

  1. PAT 1060 Are They Equal&lbrack;难&rsqb;&lbrack;科学记数法&rsqb;

    1060 Are They Equal(25 分) If a machine can save only 3 significant digits, the float numbers 12300 a ...

  2. pat 1060&period; Are They Equal &lpar;25&rpar;

    题目意思直接,要求将两个数转为科学计数法表示,然后比较是否相同  不过有精度要求 /* test 6 3 0.00 00.00 test 3 3 0.1 0.001 0.001=0.1*10^-2 p ...

  3. PAT 1060&period; Are They Equal

    If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...

  4. PAT 甲级 1060 Are They Equal &lpar;25 分&rpar;&lpar;科学计数法&comma;接连做了2天&comma;考虑要全面&comma;坑点多&comma;真麻烦&rpar;

    1060 Are They Equal (25 分)   If a machine can save only 3 significant digits, the float numbers 1230 ...

  5. 1060 Are They Equal &lpar;25 分&rpar;

    1060 Are They Equal (25 分)   If a machine can save only 3 significant digits, the float numbers 1230 ...

  6. 1060 Are They Equal &lpar;25分&rpar;

    1060 Are They Equal (25分) 题目 思路 定义结构体 struct fraction{ string f; int index; } 把输入的两个数先都转换为科学计数法,统一标准 ...

  7. PAT乙级:1090危险品装箱&lpar;25分&rpar;

    PAT乙级:1090危险品装箱(25分) 题干 集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里.比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸. 本题给定一张不相容物品的清 ...

  8. PAT乙级:1070 结绳 &lpar;25分&rpar;

    PAT乙级:1070 结绳 (25分) 题干 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟 ...

  9. PAT 甲级 1060 Are They Equal

    1060. Are They Equal (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue If a ma ...

随机推荐

  1. CentOS 7 修改主机名

    今天在阿里云上买了一个centos7的服务器,连接上以后,发现一个很长很长的主机名,看着让人很是不爽,就想着怎样将其改成一个有个性的名字. 这里我想说的是,在centos7 版本的linux系统上和c ...

  2. Android Sqlite 实例入门

    通过一个简单的例子来学习Sqlite,学生选课系统,一开始的需求是学生可以选课,选课完成后可以查询到已经选择的课. 首先设计三个表,学生,课程,选课.学生表存储学生的信息,课程表存储课程的信息,选课表 ...

  3. Unity3D研究院之Machine动画脚本自动生成AnimatorController(七十一)

    以前的项目一直不敢用Machine动画,因为当时立项的时候Machine动画还不成熟,最近项目做得差不多了我能有点时间学习,我就想在研究学习学习Machine.用Machine动画的时候需要创建一个A ...

  4. 你认为A和B所在方格颜色相同吗&quest;

    [你认为A和B所在方格颜色相同吗? ]据说全世界只有0.003%的人和photoshop能看出它们的颜色是相同的. 我属于那 99.9997% 的人...因为我不是神...

  5. git 技巧

    将某个文件回退到某个版本 git co d359624286d9c1f022b8b3b6f2d3fe3b6524188b build.sh 查看某个文件在某个版本时的内容 git show d3596 ...

  6. Ecsotre 参考

    2.dbschema dbschema 字段属性 ‘type’ => ‘int unsigned’, //字段类型 ‘extra’ => ‘auto_increment’,//定义自增 ‘ ...

  7. android客户端从服务器端获取json数据并解析的实现代码(重要)

    首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的URL中获取数组 * @param urlPath * @return * @throws Exc ...

  8. GET&lowbar;DDL提取目标元数据:ddl

    创建对象的语句就是了 提取表 set line 200 pages 50000 wrap on long 999999 serveroutput on SQL> select dbms_meta ...

  9. &lpar;办公&rpar;工作中的编码不良习惯Java&lpar;不定时更新&rpar;

    1.别瞎写,方法里能用封装好的类,就别自己写HashMap. 2.方法名,整的方法名都是啥?退出close,用out. 3.git提交版本,自己写的代码,注释,提交版本的时候,一定要清理掉.每个判断能 ...

  10. Spring Boot 2&period;x 编写 RESTful API &lpar;四&rpar; 使用 Mybatis

    用Spring Boot编写RESTful API 学习笔记 添加依赖 <dependency> <groupId>org.mybatis.spring.boot</gr ...