1060. Are They Equal (25)
Posted 给杰瑞一块奶酪~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.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.9Sample Output 1:
YES 0.123*10^5Sample Input 2:
3 120 128Sample Output 2:
NO 0.120*10^3 0.128*10^3
一道让人心烦的题,需要注意这么几点,题目输出的有效数字位数不超过n;要确定第一个不为0的数的位置,以及小数点的位置,从而判断是几次方;再就是如果都是0,不存在不为0的数字,那么要记得补0,直到够了n位数。
小数点的位置减去第一个不为0的数的位置就是要求的指数,如果指数为负需要加1,比如a为0.002,在字符串中point_a=1,non0_a=4,1-4=-3,a可以表示为0.2*10^-2。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; void print(char *p,int k)///输出格式 { printf("0.%s*10^%d",p,k); } int main() { int n,flag; char a[100],b[100],aa[100],bb[100]; cin>>n>>a>>b; int non0_a = 0,non0_b = 0,point_a = strlen(a),point_b = strlen(b),count_a = 0,count_b = 0; int size_a = strlen(a),size_b = strlen(b); int index_a,index_b; flag=0; for(int i = 0;i < size_a;i++) { if(a[i] == ‘.‘)point_a = i; else if(a[i] != ‘0‘ || flag) { if(!flag)non0_a = i,flag = 1; if(count_a < n)aa[count_a++] = a[i]; } } if(!count_a) { while(count_a<n)aa[count_a++]=‘0‘; point_a = 0; } aa[count_a] = ‘\0‘; index_a = point_a - non0_a; if(index_a < 0)index_a += 1; flag = 0; for(int i = 0;i < size_b;i++) { if(b[i] == ‘.‘)point_b = i; else if(b[i] != ‘0‘ || flag) { if(!flag)non0_b = i,flag = 1; if(count_b < n)bb[count_b++] = b[i]; } } if(!count_b) { while(count_b<n)bb[count_b++]=‘0‘; point_b = 0; } bb[count_b] = ‘\0‘; index_b = point_b - non0_b; if(index_b < 0)index_b += 1; if(index_a == index_b && strcmp(aa,bb)==0) { cout<<"YES "; print(aa,index_a); } else { cout<<"NO "; print(aa,index_a); cout<<‘ ‘; print(bb,index_b); } }
以上是关于1060. Are They Equal (25)的主要内容,如果未能解决你的问题,请参考以下文章