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.123*105 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 10100, 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.d1...dN*10^k" (d1>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
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std; // 0000.0012 0123.34
char str1[], str2[];
int process(char s[], int len){
int i,j, exp;
for(i = ; s[i] != '\0' && s[i] == ''; i++); //去除前导0
if(s[i] != '\0'){
for(j = ; s[j + i] != '\0'; j++)
s[j] = s[j + i];
s[j] = '\0';
}else{
for(int k = ; k < len; k++)
s[k] = '';
s[len] = '\0';
return ;
}
if(s[] == '.'){ //.0012
for(i = ; s[i] != '\0' && s[i] == ''; i++);
if(s[i] == '\0'){
exp = ;
s[] = '';
}else{
exp = i - ;
exp *= -;
for(j = ; s[i + j] != '\0'; j++)
s[j] = s[j + i];
s[j] = '\0';
}
}else{ //123.456
for(i = ; s[i] != '\0' && s[i] != '.'; i++);
if(s[i] == '\0'){
exp = i;
}else{
exp = i;
for(; s[i + ] != '\0'; i++)
s[i] = s[i + ];
s[i] = '\0';
}
}
for(j = ; s[j] != '\0'; j++);
while(j < len){
s[j++] = '';
}
s[len] = '\0';
return exp;
}
int main(){
int N, exp1, exp2;
scanf("%d %s %s", &N, str1, str2);
exp1 = process(str1, N);
exp2 = process(str2, N);
int tag = ;
for(int i = ; str1[i] != '\0'; i++)
if(str1[i] != str2[i]){
tag = ;
break;
}
if(tag == && exp1 == exp2){
printf("YES 0.%s*10^%d", str1, exp1);
}else{
printf("NO 0.%s*10^%d 0.%s*10^%d", str1, exp1, str2, exp2);
}
cin >> N;
return ;
}
总结:
1、题意:将给出的两个数字变成保留指定位小数的科学计数法的数字,之后看其是否相等。
2、主要分为两种数字,大于1和小于1(0.000123, 1234.5678),其次要注意有0000123的情况出现,要先去除前导0.
3、主要要做的就是想办法求出不带小数点的且符合要求的底数。
4、测试样例: 3 0.0 0
输出:YES 0.000*10^0
A1060. Are They Equal的更多相关文章
- 【算法笔记】A1060 Are They Equal
1060 Are They Equal (25 分) If a machine can save only 3 significant digits, the float numbers 1230 ...
- PAT甲级——A1060 Are They Equal
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...
- A1060 Are They Equal (25 分)
一.技术总结 cnta.cntb用于记录小数点出现的位置下标,初始化为strlen(字符串)长度. q.p用于记录第一个非0(非小数点)出现的下标,可以用于计算次方和方便统计输出的字符串,考虑到前面可 ...
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等
Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- Equal Sides Of An Array
参考:http://stackoverflow.com/questions/34584416/nested-loops-with-arrays You are going to be given an ...
- Int,Long比较重使用equal替换==
首先,==有很多限制,如Integer 类型的值在[-128,127] 期间,Integer 用 “==”是可以的(参考),超过范围则不行,那么使用equal则代替则完全ok public stati ...
- 无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS"
无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS" 之间 2011-0 ...
随机推荐
- RabbitMQ TroubleShooting
RabbitMQ是一款优秀的消息队列中间件,提供了稳定.监控完善的产品,但是软件就会有bug.为了前进路径可以畅通,我们必须了解出现的一些故障的快速处理方式,毕竟在生产环境,时间就是生命,尽快的处理是 ...
- Centos下DNS+NamedManager高可用部署方案完整记录
之前说到了NamedManager单机版的配置,下面说下DNS+NamedManager双机高可用的配置方案: 1)机器环境 主机名 ip地址 dns01.kevin.cn 192.168.10.20 ...
- C-数据结构-typedef的用法
.typedef的用法 # include <stdio.h> typedef int zhang; //为数据类为int从新取名为zhang 等价于int typedef struct ...
- 个人项目junit4测试
一.题目简介 用java编写一个程序,模拟ATM柜员机. 二.源码的github链接 www.github.com/liuxianchen/test 三.所设计的模块测试用例.测试结果截图 四 心得 ...
- Markdown页内跳转实现方法
目录 Markdown页内跳转实现方法 HTML锚点跳转 生成目录 Markdown页内跳转实现方法 [时间:2017-02] [状态:Open] [关键词:markdown,标记语言,页内跳转,ht ...
- throws和throw抛出异常的使用规则
一直对java中的throws和throw不太理解.最近一直在查这两个方面的资料,算是能明白一点吧.如果我下面的观点哪有不对,希望指出来,我加以改进. throw:(针对对象的做法) ...
- PAT L2-012 关于堆的判断
https://pintia.cn/problem-sets/994805046380707840/problems/994805064676261888 将一系列给定数字顺序插入一个初始为空的小顶堆 ...
- win8和win7下解决php5.3和5.4、5.5等不能加载php_curl.dll的终极解决办法 收藏
win8和win7下解决php5.3和5.4.5.5等不能加载php_curl.dll的终极解决办法 收藏2015年01月11日 最近分别在WIN7和Windows8 上分别安装php 高版本!都遇到 ...
- C++拷贝构造函数的调用时机
一.拷贝构造函数调用的时机 当以拷贝的方式初始化对象时会调用拷贝构造函数,这里需要注意两个关键点,分别是以拷贝的方式和初始化对象 1. 初始化对象 初始化对象是指,为对象分配内存后第一次向内存中填 ...
- centos7 搭建svn服务器
1.安装svn服务器: yum install subversion 2.配置svn服务器: 建立svn版本库根目录及相关目录即svndata及密码权限命令svnpasswd: mkdir -p /a ...