codecamp

HAMMING CODES 海明码

http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=2324

题目大意:(如题)

输入输出:(如题)

解题思路:

简单搜索。按递增顺序搜索要求的n个数,然后跟前面的数判断距离是否大于d,找到的一组解即为最小的。

注意:

1.0在每组数据里面都出现。

2.b给出了搜索的最大值:2^b-1。

3.计算两个数a,b的距离,只要计算a^b的二进制形式中1的个数。

核心代码: 

int dist(int x,int y) 
  
{ 
  
    int cnt,tmp; 
  
    cnt=0; 
  
    tmp=x^y; 
  
    while(tmp>0) 
  
    { 
  
        cnt++; 
  
        tmp-=(tmp&(-tmp)); 
  
    } 
  
    return cnt; 
  
} 


P – FULL TANK?
2.2.1 PREFACE NUMBERING 序言页码
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }