zoukankan      html  css  js  c++  java
  • PAT甲级——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 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 (<) 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 1, 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.d[1]...d[N]*10^k (d[1]>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

    其实这道题的难度在于得到这个数的幂次
     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 string A, B;//注意,10^100超出double的范围,只能使用字符串来存取
     5 int N;
     6 int dealString(string &str)//数据预处理,返回数据位数
     7 {
     8     int k = str.find('.');//找到小数点
     9     if (k != -1)
    10     {
    11         str.erase(k, 1);//删除小数点
    12         if (str[0] == '0')
    13         {
    14             k = 0;
    15             str.erase(0, 1);//删除第一个0
    16         }
    17     }
    18     else//没有小数
    19     {
    20         if (str != "0")
    21             k = str.length();
    22         else
    23         {
    24             k = 0;
    25             str.erase(0, 1);//删除第一个0
    26         }
    27     }
    28     while (!str.empty() && str[0] == '0')
    29     {
    30         str.erase(0, 1);//输出前面的0
    31         k--;//比如0.000128 = 0.128*10^-3
    32     }
    33     if (str.empty())//这个数就是0
    34         k = 0;
    35     while (str.length() < N)
    36         str += "0";//位数不够0来凑
    37     return k;
    38 }
    39 
    40 int main()
    41 {
    42     cin >> N >> A >> B;
    43     //使用k1,k2来得到A,B的位数
    44     int k1, k2;
    45     k1 = dealString(A);
    46     k2 = dealString(B);
    47     A.assign(A.begin(), A.begin() + N);//取前N位
    48     B.assign(B.begin(), B.begin() + N);
    49     if (A == B && k1 == k2)
    50     {
    51         cout << "YES ";
    52         cout << "0." << A << "*10^" << k1 << endl;
    53     }
    54     else
    55     {
    56         cout << "NO ";
    57         cout << "0." << A << "*10^" << k1 << " ";
    58         cout << "0." << B << "*10^" << k2 << endl;
    59     }
    60     return 0;    
    61 }
  • 相关阅读:
    "Illegal group reference"异常的分析
    一个基于WEB的js时间控件的实现
    ASP.NET网站的网络安全性
    关于"parseInt"
    【C#算法】冒泡排序 选择排序 插入排序 希尔排序转
    【c#】web.config续
    【C#】GridView用法
    [C#]接口引
    【SQL】DBCC
    【C#】泛型
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11294212.html
Copyright © 2011-2022 走看看