Programming Perl----------Column1

原创 2012年05月31日 14:03:53


Problem 3.

    This problem let us write all the solutions, and run, then based on the results we can get the most effective solution.

Now I don't want to compare these solutions including last file has given.

C++ bitset solution:

#include <iostream>
#include <bitset>

#define MAX_LENGTH 10000000

int main (int argc, char *argv[]) {
    std::bitset<MAX_LENGTH> array;

    int32_t i;
    for (i = 0; i < MAX_LENGTH; i++) {
        array[i] = 0;
    }

    i = 0;
    while (std::cin >> i) {
        array[i] = 1;
    }

    for (i = 0; i < MAX_LENGTH; i++ ) {
        if (array[i]) {
            std::cout << i << " is set" << std::endl;
        }
    }

    return 0;
}

C using the last file functions:

#include <stdlib.h>
#include <stdio.h>

#define MAX_LENGTH 10000000
#define INT_LENGTH 32
#define SHIFT 5
#define MASK 0X1F

int32_t integerArray[1 + MAX_LENGTH >> SHIFT];

void set(int32_t i){
    integerArray[i >> SHIFT] |= (1 << (i & MASK));
}

void clear(int32_t i) {
    integerArray[i >> SHIFT] &= ~(1 << (i & MASK));
}

int test(int32_t i) {
    return integerArray[i >> SHIFT] & (1 << (i & MASK));
}

int main (void) {
    int32_t i;
    for (i = 0; i < 10000000; i++) {
        clear(i);
    }

    while (scanf("%d", &i) != EOF) {
        set(i);
        if (test(i)) {
            printf ("%d is set\n", i);
        }
    }

    return 0;
}




相关文章推荐

20 Killer Perl Programming Tips for Beginners on UNIX / Linux OS

1. List all Installed Perl Modules from Unix Command Line $ perl -MFile::Find=find -MFile::Spec::Fu...

第二十七章 Introduction to Perl Programming(中文版)

Perl基础 脚本 Perl 是一种脚本语言,它在每次运行前都需要编译。在unix/linux下,通常perl脚本都是以 #!/usr/bin/perl 为开头的。这其实是perl(可执行文件...
  • maidanm
  • maidanm
  • 2012年08月11日 03:18
  • 1114

《Advandced Perl Programming》第七章、POE(2)

>第七章、POE(2) 原文链接: http://blog.yikuyiku.com/?p=1362 1. Wheels Wheels就是POE的I/O系统的动力之源。 wheel帮助...

Usefunctional programming in Perl to make test automation code more structural

Abstract In USD test automation, we used Perl as the majorprogramming language. With daily practice...

java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\xB3' for column 'Content' at row 1

在尝试将 emoji 表情符号 插入MySQL数据库时,遇到以下错误信息:### The error occurred while setting parameters ### SQL: INSERT...

【报错】BatchUpdateException: Data truncated for column 'id' at row 1

【报错】 log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry). l...

MySQL字符集 GBK、GB2312、UTF8区别 解决 MYSQL中文乱码问题以及error 1406:data too long for column 'name' at row 1

MySQL中涉及的几个字符集 character-set-server/default-character-set:服务器字符集,默认情况下所采用的。 character-set-databas...

【Hibernate错误解决办法】Could not execute JDBC batch update; Data too long for column 'name' at row 1

刚开始学Hibernate
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Programming Perl----------Column1
举报原因:
原因补充:

(最多只允许输入30个字)