Compare two large numbers(比较两个大数)
问题描述
已经提供了代表两个数字的两个字符串作为输入.字符串中的数字可能很大,以至于它们可能无法用 Java 数据类型 int 表示.目标是比较两个数字并将该数字输出为字符串
Two strings representing two numbers have been provided as input. The numbers in the string can be so large that they may not be represented by the Java data type int. The objective is to compare the two numbers and output that number as a string
例如我们必须比较:
874986754789289867753896798679854698798789857387687546456"
"874986754789289867753896798679854698798789857387687546456"
和
98347598375689758967756458678976893478967586857687569874"
"98347598375689758967756458678976893478967586857687569874"
它们都超出了 JAVA 中的 long 和 int 数据类型的范围在比较之后,我们必须将该数字输出为字符串
which both are out of range of long and int data types in JAVA and after comparing we have to output that number as a string
推荐答案
您可以先查看每个字符串的长度.如果其中一个较长并且您知道它们都是无符号值,则较长的字符串具有较大的数字.如果它们的长度相同,则从左到右开始逐个字符地比较字符串.当您找到第一个更大的数字时,您会得出结论该数字更大.
you could start by first looking at each string's length. if one of them is longer and you know they are both unsigned values, the longer string has the bigger number. if they both have the same length, you start comparing the strings char by char starting from left to right. when you found your first bigger digit you conclude that number is bigger.
这篇关于比较两个大数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:比较两个大数


基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01