热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

比较科学符号

比较科学符号中给出的两个浮点数原文:https://www

比较科学符号

中给出的两个浮点数

原文:https://www . geeksforgeeks . org/compare-two-floating-point-numbers-in-scientific-entrepreneurs/

给定两根弦 NMa * 10bT7】的形式。任务是比较给定的两个浮点数,并打印出较小的数字,如果两个数字相等,则打印相等。

0

示例:

n 和 M 是由两部分组成的两个数字:


  1. a1 为 N 的尾数,a2 为 m 的尾数

  2. b1 是 N 的指数,b2 是 m 的指数

输入: N = 310^2,M = 29910^0
输出: M
解释:
a1 = 3,b1 = 2
a2 = 299,B2 = 0
n = 310^2 = 300
m = 299
10^0 = 299。
我们知道 299 比 300 小。

输入: N = -510^3,M = -5010^2
输出:相等
解释:t8】a1 =-5,b1 = 3
a2 = -50,B2 = 2
n = -510^3 =-5000
m = -50
10^2 =-5000
因此,n 和 m 相等。

输入: N = -210^1,M = -310^1
输出: M
解释:
a1 = -2,b1 = 1
a2 = -3,B2 = 1
n =-20
m =-30
-30 小于-20,因此 m 是较小的数字。

天真方法:我们将计算从字符串 N 和 M 中提取的数字的值,然后比较哪一个更好。大整数类将用于存储和计算 Java 中的 N 和 M 的值。对于较大的测试用例,这种方法会产生超过时间限制的错误。

最佳方法:


  • 从字符串 N 和 m 中提取尾数和指数

  • 找到' * '的索引(假设穆林),那么穆林之前的子串就是尾数(a1)。

  • 找到'^'的指数,假设 powInd ,那么 powInd 之后的子串就是指数(b1)。

  • 同样,找出 a2 和 b2。

  • 如果(a1 > 0 & & a2 <0) ,则打印 M ( M 始终较小)。

  • 同样的,如果(a2 > 0 & & a1 <0) ,打印 N

  • 否则将需要使用日志进行比较。

  • 以下公式将用于计算日志,并得出一个比较结果,以确定哪个数字更大。


N = a1 * 10 ^ b1

从两侧取原木底座 10

对数 N =对数(a1)+B1 ——-(1)

同样,log(M)= log(a2)+B2 ——( 2)

从等式(2)中减去等式(1)
ans =(2)–(1)
ans = log(a1/a2)+B1–B2

因此:int ans = log(a1/a2)+B1–B2



  • 如果 a1 <0 ,那么 ans = -ans 。这是因为 a1 和 a2 都是负的。

    • 如果年<0 ,打印 N

    • else if年> 0 ,打印 M



  • 否则打印平等。

下面是实现上述方法的 Jave 程序:

Java 语言(一种计算机语言,尤用于创建网站)


// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
class GFG
{
  // Function to extract mantissa
  static int[] extract_mantissa(String N,
                                String M)
  {
    int mantissa[] = new int [2];
    int mulInd1 = N.indexOf('*');
    int a1 = Integer.parseInt(
             N.substring(0, mulInd1));
    mantissa[0] = a1;
    int mulInd2 = M.indexOf('*');
    int a2 = Integer.parseInt(
             M.substring(0, mulInd2));
    mantissa[1] = a2;
    return mantissa;
  }
  // Function to extract exponent
  static int[] extract_exponent(String N,
                                String M)
  {
    int exponent[] = new int [2];
    int powInd1 = N.indexOf('^');
    int b1 = Integer.parseInt(
             N.substring(powInd1 + 1));
    exponent[0] = b1;
    int powInd2 = M.indexOf('^');        
    int b2 = Integer.parseInt(
             M.substring(powInd2 + 1));
    exponent[1] = b2;
    return exponent;
  }
  // Function to find smaller number
  static void solution(int a1, int b1,
                       int a2, int b2)
  { 
    double x = ((double)(a1) /
                (double)(a2));
    double ans = (b1 - b2 +
                  Math.log10(x));
    // If both are negative
    if(a1 < 0)
      ans = -ans;
    if(ans < 0)
      System.out.println("N");
    else if(ans > 0)
      System.out.println("M");
    else
      System.out.println("Equal");
  }
  static void solve(String N, String M)
  {
    // Extract mantissa(a1) and mantissa(a2)
    // from num1 and num2
    int mantissa[] = extract_mantissa(N, M);
    // Extract exponent(b1) and exponent(b2)
    // from num1 and num2
    int exponent[] = extract_exponent(N, M);
    if(mantissa[0] > 0 && mantissa[1] < 0)
      System.out.println("M");
    else if(mantissa[0] < 0 && mantissa[1] > 0)
      System.out.println("N");
    else
    {
      // if mantissa of both num1 and num2
      // are positive or both are negative
      solution(mantissa[0], exponent[0],
               mantissa[1], exponent[1]);
    }
  }
  // Driver code
  public static void main (String[] args)
  {
    // Mantissa is negative and
    // exponenent is positive
    String N = "-5*10^3";
    String M = "-50*10^2";
    solve(N, M);
    // Mantissa is negative and
    // exponent is negative
    N = "-5*10^-3";
    M = "-50*10^-2";
    solve(N, M);
    // Mantissa is positive and
    // exponent is negative
    N = "5*10^-3";
    M = "50*10^-2";
    solve(N, M);
    // Mantissa is positive and
    // exponent is positive
    N = "5*10^3";
    M = "50*10^2";
    solve(N, M);
  }
}


Python 3


# Python 3 program to implement
# the above approach
import math
# Function to extract mantissa
def extract_mantissa(N,  M):
    mantissa = [0]*2
    mulInd1 = list(N).index('*')
    a1 = N[0: mulInd1]
    mantissa[0] = a1
    mulInd2 = list(M).index('*')
    a2 = M[0: mulInd2]
    mantissa[1] = a2
    return mantissa
# Function to extract exponent
def extract_exponent(N,  M):
    exponent = [0]*2
    powInd1 = list(N).index('^')
    b1 = N[powInd1 + 1:]
    exponent[0] = b1
    powInd2 = list(M).index('^')
    b2 = M[powInd2 + 1:]
    exponent[1] = b2
    return exponent
# Function to find smaller number
def solution(a1,  b1,
             a2,  b2):
    x = int(a1) / int(a2)
    ans = (int(b1) - int(b2) + math.log10(x))
    # If both are negative
    if(int(a1) < 0):
        ans = -ans
    if(ans < 0):
        print("N")
    elif(ans > 0):
        print("M")
    else:
        print("Equal")
def solve(N,  M):
    # Extract mantissa(a1) and mantissa(a2)
    # from num1 and num2
    mantissa = extract_mantissa(N, M)
    # Extract exponent(b1) and exponent(b2)
    # from num1 and num2
    exponent = extract_exponent(N, M)
    if(int(mantissa[0]) > 0 and int(mantissa[1]) < 0):
        print("M")
    elif(int(mantissa[0]) < 0 and int(mantissa[1]) > 0):
        print("N")
    else:
        # if mantissa of both num1 and num2
        # are positive or both are negative
        solution(mantissa[0], exponent[0],
                 mantissa[1], exponent[1])
# Driver code
if __name__ == "__main__":
    # Mantissa is negative and
    # exponenent is positive
    N = "-5*10^3"
    M = "-50*10^2"
    solve(N, M)
    # Mantissa is negative and
    # exponent is negative
    N = "-5*10^-3"
    M = "-50*10^-2"
    solve(N, M)
    # Mantissa is positive and
    # exponent is negative
    N = "5*10^-3"
    M = "50*10^-2"
    solve(N, M)
    # Mantissa is positive and
    # exponent is positive
    N = "5*10^3"
    M = "50*10^2"
    solve(N, M)
    # This code is contributed by ukasp.


C


// C# program to implement
// the above approach
using System;
class GFG
{
  // Function to extract mantissa
  public static int[] extract_mantissa(String N, String M)
  {
    int[] mantissa = new int [2];
    int mulInd1 = N.IndexOf('*');
    int a1 = int.Parse(N.Substring(0, mulInd1));
    mantissa[0] = a1;
    int mulInd2 = M.IndexOf('*');
    int a2 = int.Parse(M.Substring(0, mulInd2));
    mantissa[1] = a2;
    return mantissa;
  }
  // Function to extract exponent
  public static int[] extract_exponent(String NString M)
  {
    int[] exponent = new int [2];
    int powInd1 = N.IndexOf('^');
    int b1 = int.Parse(N.Substring(powInd1 + 1));
    exponent[0] = b1;
    int powInd2 = M.IndexOf('^');        
    int b2 = int.Parse(
             M.Substring(powInd2 + 1));
    exponent[1] = b2;
    return exponent;
  }
  // Function to find smaller number
  static void solution(int a1, int b1,
                       int a2, int b2)
  { 
    double x = ((double)(a1) /
                (double)(a2));
    double ans = (b1 - b2 +
                  Math.Log10(x));
    // If both are negative
    if(a1 < 0)
      ans = -ans;
    if(ans < 0)
      Console.WriteLine("N");
    else if(ans > 0)
      Console.WriteLine("M");
    else
      Console.WriteLine("Equal");
  }
  static void solve(String N, String M)
  {
    // Extract mantissa(a1) and mantissa(a2)
    // from num1 and num2
    int[] mantissa = extract_mantissa(N, M);
    // Extract exponent(b1) and exponent(b2)
    // from num1 and num2
    int[] exponent = extract_exponent(N, M);
    if(mantissa[0] > 0 && mantissa[1] < 0)
      Console.WriteLine("M");
    else if(mantissa[0] < 0 && mantissa[1] > 0)
      Console.WriteLine("N");
    else
    {
      // if mantissa of both num1 and num2
      // are positive or both are negative
      solution(mantissa[0], exponent[0],
               mantissa[1], exponent[1]);
    }
  }
  // Driver code
  public static void Main ()
  {
    // Mantissa is negative and
    // exponenent is positive
    String N = "-5*10^3";
    String M = "-50*10^2";
    solve(N, M);
    // Mantissa is negative and
    // exponent is negative
    N = "-5*10^-3";
    M = "-50*10^-2";
    solve(N, M);
    // Mantissa is positive and
    // exponent is negative
    N = "5*10^-3";
    M = "50*10^-2";
    solve(N, M);
    // Mantissa is positive and
    // exponent is positive
    N = "5*10^3";
    M = "50*10^2";
    solve(N, M);
  }
}
// This code is contributed by saurabh_jaiswal.

输出:

相等
M
N
相等

时间复杂度: O ( 1 )
根据给定的约束|a|最多可以是 10 个长度的字符串,如果是负数,那么它可以是 11 个长度,同样 b 也可以是 11 位数。字符串的最大长度可以是 25 (11 +3+11),所以我们可以考虑提取 a 和 b 的恒定时间操作。

辅助空间: O ( 1)


推荐阅读
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 树莓派Linux基础(一):查看文件系统的命令行操作
    本文介绍了在树莓派上通过SSH服务使用命令行查看文件系统的操作,包括cd命令用于变更目录、pwd命令用于显示当前目录位置、ls命令用于显示文件和目录列表。详细讲解了这些命令的使用方法和注意事项。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有