[英]How do I calculate similarity of two integers?
Actually it's quite hard to describe: 实际上很难描述:
I want to implement an algorithm which compares figure by figure of the same position (as I do my calculations in a 10-based system it's rather the same "power of ten") of two given integers/number (with the same "length"). 我想实现一种算法,该算法通过两个给定的整数/数字(具有相同的“长度”)来比较同一位置的图形(因为我在基于10的系统中进行计算,这是相同的“十进制的幂”) )。 It should return the grade of equality as following:
它应该返回平等等级,如下所示:
I do not want to do my calculations based on a string-comparison, as I'll doing this in a way bigger scenario :) 我不想基于字符串比较来进行计算,因为我将以更大的场景进行此操作:)
public static int Compare(int i1, int i2)
{
int result = 0;
while(i1 != 0 && i2 != 0)
{
var d1 = i1 % 10;
var d2 = i2 % 10;
i1 /= 10;
i2 /= 10;
if(d1 == d2)
{
++result;
}
else
{
result = 0;
}
}
if(i1 != 0 || i2 != 0)
{
throw new ArgumentException("Integers must be of same length.");
}
return result;
}
Note: it does not handle negative integers 注意:它不处理负整数
Update: fixed after question update 更新:问题更新后已修复
See the Answer to this SO Question 请参阅此SO问题的答案
You can Split the digits by the first method and Get the Similarity from the Second Method: 您可以通过第一种方法拆分数字并从第二种方法获取相似性:
int[] GetIntArray(int num)
{
List<int> listOfInts = new List<int>();
while(num > 0)
{
listOfInts.Add(num % 10);
num /= 10;
}
listOfInts.Reverse();
return listOfInts.ToArray();
}
int GetSimilarity(int firstNo, int secondNo)
{
int[] firstintarray = GetIntArray(firstNo)
int[] secondintarray = GetIntArray(secondNo)
if (firstintarray.Count != secondintarray.Count)
{
throw new ArgumentException("Numbers Unequal in Length!");
}
int similarity = 0;
for(i = 0; i < firstintarray.Count; i++)
{
if (secondintarray[i] = firstintarray[i])
{
similarity++;
continue;
}
break;
}
}
Now you can Compare the the two int arrays like this : 现在,您可以像这样比较两个int数组:
int Similarity = GetSimilarity(4491, 4461);// Returns 2
For all cases where X and Y are not equal: 对于X和Y不相等的所有情况:
Length - Math.Floor(Math.Log10(Math.Abs(X - Y)) + 1)
4491 and 1020 4491和1020
4 - Math.Floor(Math.Log10(Math.Abs(4491 - 1020)) + 1) = 0
4491 and 4493 4491和4493
4 - Math.Floor(Math.Log10(Math.Abs(4491 - 4493)) + 1) = 3
Just to try to salvage something from this question after my last attempt... 只是为了在我上次尝试后尝试解决此问题中的某些内容...
int Compare(int x, int y)
{
int pow10 = (int)Math.Pow(10, Math.Floor(Math.Log(Math.Max(x, y), 10)));
int matches = 0;
while(pow10 > 0 && (x / pow10) == (y / pow10))
{
matches++;
pow10 /= 10;
}
return matches;
}
It sounds like the Levenshtein Distance would be appropriate. 听起来Levenshtein距离合适。 This is a standard way to measure the difference between two strings.
这是测量两个字符串之间差异的标准方法。 In your case, the strings are the decimal representations of the numbers.
在您的情况下,字符串是数字的十进制表示形式。
I thing the best way to calculate it is using Euclidean Similarity. 我认为最好的计算方法是使用欧几里得相似度。
Please see this link: http://stackoverflow.com/questions/11555355/calculating-the-distance-between-2-points 请参阅此链接: http : //stackoverflow.com/questions/11555355/calculating-the-distance-between-2-points
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.