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
常用特例 0 0.00 1.0 等用来特判
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
using namespace std;
/*
char数组存放满足给定要求的科学计数法表示:
eg 0.[ ] * 10^k,关键:找到首个非零元素,确定小数点的位置([].len 代表有效位数)
*/
struct result{
char d[105];
int k;
};
result getres(char *a,int n){
result r;
int index=0;//r.d[]使用
int i,firstpos=-1,pointpos=-1;
//注意考虑给定数是整数或者小数
for(i=0;a[i]!=0;i++){
if(a[i]=='.') pointpos=i;
else if(a[i]=='0' && firstpos==-1) ;
else{//录入的不超过最大精度,,开始出bug因为这里写成了else if,这样找到第一个非零点的同时就不能进行录入,故会丢失数据的第一位
if(firstpos==-1) firstpos=i;
if(index<n) r.d[index++]=a[i];
}
}
if(index!=0) while(index<n) r.d[index++]='0';//非零时才补齐,不然0.00这样会使r.k=2
r.d[index]=0;
//用转化后小数点和首个非零数之间距离来确定要计算的指数
if(pointpos==-1)
pointpos=i;//i=strlen(a),即原来数的最后一位的后一位,不能用n(用0补齐后的)
if(pointpos<firstpos)//小数
r.k=pointpos-firstpos+1;//题目要求,如0.01234->0.[1234]*10^[1]
else
r.k=pointpos-firstpos;//123.4->0.[1234]*10^[3]
if(index==0){
int j;
for(j=0;j<n;j++)
r.d[j]='0';
r.d[j]=0;
r.k=0;
}
return r;
};
int main()
{
//freopen("E:\\learn\\inputs.txt","r",stdin);
int n;
char num1[120],num2[120];
scanf("%d%s%s",&n,num1,num2);
result r1=getres(num1,n);
result r2=getres(num2,n);
if(strcmp(r1.d,r2.d)==0 && r1.k==r2.k){
printf("YES 0.%s*10^%d",r1.d,r1.k);
}
else printf("NO 0.%s*10^%d 0.%s*10^%d",r1.d,r1.k,r2.d,r2.k);
return 0;
}