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 ...
随机推荐
- Docker容器学习梳理 - 容器间网络通信设置(Pipework和Open vSwitch)
自从Docker容器出现以来,容器的网络通信就一直是被关注的焦点,也是生产环境的迫切需求.容器的网络通信又可以分为两大方面:单主机容器上的相互通信,和跨主机的容器相互通信.下面将分别针对这两方面,对容 ...
- Nginx反向代理的简单实现
1)nginx的反向代理:proxy_pass2)nginx的负载均衡:upstream 下面是nginx的反向代理和负载均衡的实例: 负载机:A机器:103.110.186.8/192.168.1. ...
- Command Analyze failed with a nonzero exit code
在运行RN项目的时候,报 Command Analyze failed with a nonzero exit code ,试着将build System 修改下
- v-for v-if || v-else
<el-col> <div v-for="item in resultDetail" class="physical-content" v-i ...
- java感想
Java学起来很有趣,通过学习Java可以提高自己的逻辑能力.在学习Java期间我们做了一些程序,我们班的同学也都积极准备,完成的还不错!在做程序时,我遇到了一些难题,有时也会出现错误,时间长了弄得我 ...
- C程序设计教学小结(选择结构)
1. 函数使用的三个问题 函数声明语句 void add(); 或 int add(int x,int y); 函数调用 add(); c=add(a,b) 函 ...
- PHP 闭包获取外部变量和global关键字声明变量的区别
最近在学习workerman的时候比较频繁的接触到回调函数,使用中经常会因为worker的使用方式不同,会用这两种不同的方式去调用外部的worker变量,这里就整理一下PHP闭包获取外部变量和glob ...
- Docker for windows WIN版本,主板特性问题
WIN 10 Home版无法开启Hyper-V特性. Docker for windows有Hyper-V和VirtualBox两个版本: https://forums.docker.com/t/in ...
- Linux下运行Shell脚本或者可执行文件Executable方法
绝对路径 /xxx/xxx/something.sh /xxx/xxx/executable 相对路径 ./something.sh ./executable 注意:前边得加./,可不是像window ...
- 暂时刷完leetcode的一点小体会
两年前,在实习生笔试的时候,笔试百度,对试卷上很多问题感到不知所云,毫无悬念的挂了 读研两年,今年代笔百度,发现算法题都见过,或者有思路,但一时之间居然都想不到很好的解法,而且很少手写思路,手写代码, ...