在信息论中,两个等长字符串之间的汉明距离是两个字符串对应位置的字符不同的个数。

 1 #include <stdio.h>
 2 /*给定两个字符串,求两个字符串的汉明距离*/
 3 int hamming_distance(char *s1, char *s2)
 4 {
 5     int i;
 6     int distance = 0;
 7     for(i=0;s1[i] && s2[i];i++)
 8     {
 9         if(s1[i]!=s2[i])
10             distance++;
11     }
12     while(s1[i])
13     {
14         distance++;
15         i++;
16     }
17     while(s2[i])
18     {
19         distance++;
20         i++;
21     }
22     return distance;
23 }
24 
25 int main()
26 {
27     char *s1 = "0100";
28     char *s2 = "1100";
29     printf("%d\n",hamming_distance(s1,s2));
30     return 0;
31 }

 

相关文章:

  • 2021-04-08
  • 2021-08-08
  • 2021-05-27
  • 2021-07-10
  • 2022-02-14
  • 2021-08-08
  • 2022-12-23
  • 2021-08-05
猜你喜欢
  • 2022-12-23
  • 2021-12-30
  • 2022-02-06
  • 2021-11-16
  • 2021-10-26
  • 2022-12-23
相关资源
相似解决方案