比较两个大数 [英] Compare two large numbers

查看:22
本文介绍了比较两个大数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经提供了代表两个数字的两个字符串作为输入.字符串中的数字可能很大,以至于它们可能无法用 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.

这篇关于比较两个大数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆