zoukankan      html  css  js  c++  java
  • 编程珠玑chapter1

    /* Copyright (C) 1999 Lucent Technologies */
    /* From 'Programming Pearls' by Jon Bentley */
    
    /* bitsort.c -- bitmap sort from Column 1
     *   Sort distinct integers in the range [0..N-1]
     */
    
    #include <stdio.h>
    
    #define BITSPERWORD 32
    #define SHIFT 5
    #define MASK 0x1F // MASK = 31
    #define N 1000000
    int a[1 + N/BITSPERWORD];
    //i >> SHIFT -> i >> 5 -> i/32 -> 数字i所对应的数组的index
    //i & MASK -> i & 31 -> i % 32 -> 对应数组元素的bit位
    //a[i >> SHIFT] -> 指的是i所在的位所在的数组位置
    //1<<(i & MASK)
    void set(int i) {        a[i>>SHIFT] |=  (1<<(i & MASK)); }
    void clr(int i) {        a[i>>SHIFT] &= ~(1<<(i & MASK)); }
    int  test(int i){ return a[i>>SHIFT] &   (1<<(i & MASK)); }
    
    int main()
    {	int i;
    	for (i = 0; i < N; i++)
    		clr(i);
    /*	Replace above 2 lines with below 3 for word-parallel init
    	int top = 1 + N/BITSPERWORD;
    	for (i = 0; i < top; i++)
    		a[i] = 0;
     */
    	while (scanf("%d", &i) != EOF)
    		set(i);
    	for (i = 0; i < N; i++)
    		if (test(i))
    			printf("%d\n", i);
    }
    
    
  • 相关阅读:
    jquery-validate v1.19.2 源码分析
    jquery之遍历-Section04
    jquery之元素-Section03
    jquery之效果-Section02
    jquery之入门-Section01
    CSS世界(七)思维导图
    shell文件处理awk
    jquery插件懒加载
    jquery插件改变背景色
    jquery多库共存
  • 原文地址:https://www.cnblogs.com/xiangshancuizhu/p/1918362.html
Copyright © 2011-2022 走看看