简体   繁体   English

使用Java检查十进制数字顺序的逻辑

[英]logic for checking sequential order in decimal numbers using java

I want to check the sequential order of decimal numbers and find the missing number. 我想检查十进制数字的顺序,找到丢失的数字。 For eg: If i have 1.1.1, 1.1.3, 1.1.4, 2.1.1, 2.1.3, 2.1.2, 3, etc Here i need to find the missing number 1.1.2 and also out of sequence 2.1.2. 例如:如果我有1.1.1、1.1.3、1.1.4、2.1.1、2.1.3、2.1.2、3,等等,在这里我需要找到丢失的数字1.1.2,并且也超出顺序2.1 .2。 Kindly help me with logic. 请逻辑帮助我。

This does sound suspiciously like homework but, here's some hints for the algorithm. 这听起来确实像是家庭作业,但这里有一些算法提示。 For simplicity, not efficiency, try a 2-step approach. 为了简单而不是效率,请尝试采用两步法。

You'll have to treat each value in your initial list as an ordered set of integers. 您必须将初始列表中的每个值都视为有序的整数集。 That is the value 2.1.3 is, an ArrayList whose elements are 2, 1, 3. 即值2.1.3是一个ArrayList,其元素为2、1、3。

First determine what's out of sequence - this catches the 2.1.2 value. 首先确定什么是乱序-这捕获了2.1.2值。 Something's out of sequence when the value of any part of the n-th element of the list is greater than any part of the (n+1)-th element. 当列表的第n个元素的任何部分的值大于第(n + 1)个元素的任何部分的值时,出现乱序。 Walk through the list of values comparing two at a time; 遍历一次比较两个值的列表; breaking each element into an list of integers. 将每个元素分解为整数列表。

Second, sort the list and determine if there are gaps. 其次,对列表进行排序并确定是否存在差距。 Sorting would still need to treat each value as a set of integers. 排序仍然需要将每个值视为一组整数。 A gap in the sorted list would be defined as a change of more than 1 in any part of the two values. 排序列表中的间隔将定义为两个值中任何部分的变化大于1。 Stop comparing 2 values when you find a gap and move onto the next 2 values to compare. 找到差距后,停止比较两个值,然后移至接下来的两个值进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM