1060. Are They Equal (25)
来源:互联网 发布:线性时间选择算法分析 编辑:程序博客网 时间:2024/05/17 17:14
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
#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX 110int getK(char *a){ int DotPos,FirstValid; int i; DotPos = -1;FirstValid = -1; for(i=0;a[i];i++) { if(a[i] == '.') { DotPos = i; }else if(a[i] != '0' && FirstValid == -1){FirstValid = i;} }if(DotPos == -1){DotPos = i;}if(FirstValid == -1){return 0;}if(DotPos < FirstValid){ return DotPos - FirstValid+1;}else{return DotPos - FirstValid;}}char *convert(char *a,int N){ char *c; int lenth,i,flag,count; c = (char *)malloc(MAX*sizeof(char)); c[0] = '0'; c[1] = '.'; lenth = strlen(a); flag = 1; count = 0; for(i=0;i<lenth;i++) { if(a[i] != '.' && a[i] !='0') { flag = 0; }if(a[i] == '.'){continue;} if(flag) { continue; } c[count+2] = a[i]; count ++; if(count >= N) { break; } } while(count < N) { c[count+2] = '0'; count++; } c[count+2] = 0; return c;}int main(){ int N; char a[MAX],b[MAX]; char *c,*d; //freopen("d://input.txt","r",stdin); scanf("%d%s%s",&N,a,b); c = convert(a,N); d = convert(b,N);if(getK(a) == getK(b) && strcmp(c,d)==0){ printf("YES %s*10^%d\n",c,getK(a),d,getK(b));}else{printf("NO %s*10^%d %s*10^%d\n",c,getK(a),d,getK(b));} return 0;}
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- 1060. Are They Equal (25)
- js之bom_技术
- 网狐 "由于网络问题,您已经与服务器断开连接,请重新连接"
- 45. Jump Game II
- android studio for android learning (二十六 )自定义控件理解与浅析(1)
- dup and dup2的剖析
- 1060. Are They Equal (25)
- Java:异常、断言
- eclipse for java ee +tomcat +jsp环境搭建
- CSDN博客中画出一道华丽的水平分割线
- 非线性最小二乘法之Gauss Newton、L-M、Dog-Leg
- 刚学习Tcp通信遇到的一些问题
- JavaFX 程序退出时结束子线程
- 网狐框架分析九--TCPSocket.cpp
- Python——字典与字典方法