zoukankan      html  css  js  c++  java
  • PAT甲级 Are They Equal (25) (恶心模拟)

    Are They Equal (25)

    时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB

    题目描述

    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.

    输入描述:

    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.


    输出描述:

    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.

    输入例子:

    3 12300 12358.9

    输出例子:

    YES 0.123*10^5
    题解:
      这个题关键不在于各种分情况讨论,而是如何找到有效数位
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 int n;
     7 string s1,s2;
     8 struct node{
     9     string ss;
    10     int kk;
    11 }e;
    12 node judge(string s)
    13 {
    14     int flag=0;
    15     int len=s.length();
    16     int k=0;
    17     string ans="";
    18     for(int i=0;i<len;i++)
    19     {
    20         if(s[i]=='.')
    21             flag=1;
    22         else
    23         {
    24             if(!flag)
    25                 k++;
    26             if(ans==""&&s[i]=='0')
    27             {
    28                 k--;
    29             }
    30             else
    31             {
    32                 ans+=s[i];
    33             }
    34         }
    35         
    36     }
    37     while(ans.length()<n)
    38         ans+='0';
    39     if(ans.length()>n)
    40         ans=ans.substr(0,n);
    41 //    cout<<"ans="<<ans<<endl;
    42     int ok=0;
    43     for(int i=0;i<ans.length();i++)
    44     {
    45         if(ans[i]!='0')
    46         {
    47             ok=1;
    48             break;
    49         }
    50     }
    51     if(!ok)
    52         k=0;
    53     e.ss=ans;
    54     e.kk=k;
    55     return e; 
    56 }
    57 int main()
    58 {
    59     cin>>n>>s1>>s2;
    60     node ans1=judge(s1);
    61     node ans2=judge(s2);
    62     if(ans1.ss==ans2.ss&&ans1.kk==ans2.kk)
    63     {
    64         cout<<"YES "<<"0."<<ans1.ss<<"*10^"<<ans1.kk<<endl;
    65     }
    66     else
    67     {
    68         cout<<"NO "<<"0."<<ans1.ss<<"*10^"<<ans1.kk<<" "<<"0."<<ans2.ss<<"*10^"<<ans2.kk<<endl;
    69     }
    70     return 0;
    71 } 



  • 相关阅读:
    innobackupex备份命令输出
    Percona XtraBackup原理详解
    MongoDB性能分析工具mongostat
    MongoDB查看当前连接数
    事务、拦截器
    HttpServletResponse和HttpServletRequest的简单实用
    Maven环境配置
    SQL Server 时间戳与时间格式互相转换
    虚拟机、云主机、VPS 三者之间的区别
    Elasticsearch 空值过滤
  • 原文地址:https://www.cnblogs.com/1013star/p/11198204.html
Copyright © 2011-2022 走看看