zoukankan      html  css  js  c++  java
  • 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 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 #define _CRT_SECURE_NO_WARNINGS
     2 #include <climits>
     3 #include<iostream>
     4 #include<vector>
     5 #include<queue>
     6 #include<map>
     7 #include<set>
     8 #include<stack>
     9 #include<algorithm>
    10 #include<string>
    11 #include<cmath>
    12 using namespace std;
    13 
    14 int main()
    15 {
    16     int N;
    17     float num1, num2;
    18     int p1, p2,n1,n2;
    19     p1 = p2 = 0;
    20     n1 = n2 = 0;
    21     cin >> N >> num1 >> num2;
    22     stack<int>S1, S2;
    23     while (num1 - floor(num1) != 0.0)
    24     {
    25         num1 *= 10.0;
    26         p1++;
    27     }
    28     while (num2 - floor(num2) != 0.0)
    29     {
    30         num2 *= 10.0;
    31         p2++;
    32     }
    33     int N1 = static_cast<int>(num1);
    34     int N2 = static_cast<int>(num2);
    35     while (N1)
    36     {
    37         S1.push(N1 % 10);
    38         N1 /= 10;
    39         n1++;
    40     }
    41     while (N2)
    42     {
    43         S2.push(N2 % 10);
    44         N2 /= 10;
    45         n2++;
    46     }
    47     n1 -= p1;
    48     n2 -= p2;
    49     int ans1 = 0, ans2 = 0;
    50     int s1 = 0, s2 = 0;
    51     for (int i = 0; i < N; i++)
    52     {
    53         if (!S1.empty()) {
    54             ans1 = ans1 * 10 + S1.top();
    55             S1.pop();
    56             s1++;
    57         }
    58         if (!S2.empty()) {
    59             ans2 = ans2 * 10 + S2.top();
    60             S2.pop();
    61             s2++;
    62         }
    63     }
    64     if (s1 != N) {
    65         ans1 *= 10;
    66         s1++;
    67     }
    68     if (s2 != N) {
    69         ans2 *=10;
    70         s2++;
    71     }
    72     if (ans1 == ans2 && n1 == n2)
    73     {
    74         if(ans1==0)
    75             cout << "YES "<< ans1;
    76         else
    77         {
    78             cout << "YES" << " 0." << ans1;
    79             if (n1 > 0)
    80                 cout << "*10^" << n1;
    81         }
    82     }
    83     else
    84     {
    85         cout << "NO ";
    86         cout << "0." << ans1;
    87         if (n1 > 0)
    88             cout << "*10^" << n1<<" ";
    89         cout << "0." << ans2;
    90         if (n2 > 0)
    91             cout << "*10^" << n1;
    92     }
    93 }
    View Code
  • 相关阅读:
    数据类型装换
    变量及数据类型
    27 网络通信协议 udp tcp
    26 socket简单操作
    26 socket简单操作
    14 内置函数 递归 二分法查找
    15 装饰器 开闭原则 代参装饰器 多个装饰器同一函数应用
    12 生成器和生成器函数以及各种推导式
    13 内置函数 匿名函数 eval,exec,compile
    10 函数进阶 动态传参 作用域和名称空间 函数的嵌套 全局变量
  • 原文地址:https://www.cnblogs.com/57one/p/12055324.html
Copyright © 2011-2022 走看看