编程珠玑(第2版)第一章课后习题答案

第一章 开篇
1.
#include <iostream>
#include <set> // 输入相同元素在集合内只保留一个

using namespace std;

int main()
{
        set<int> S;
        int i;
        set<int>::iterator j;
        while( cin >> i ) // 输入ctrl + z回车结束输入
                S.insert(i);
        for(j = S.begin(); j != S.end(); ++j) // 这里不能用小于号,因为未定义该操作
                cout << *j << '\t';
        cout << endl;
        return 0;
}

2.
#include <iostream>

using namespace std;

#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0x1F
#define N 10000000
int a[N/BITSPERWORD + 1]; // 数组a的每个元素为int类型,具有32bit,可表示32个数

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);
        while( cin >> i ) // 输入ctrl + z回车结束输入
                set(i);
        for(i = 0; i < N; ++i)
                if(test(i))
                        cout << i << '\t';
        cout << endl;
        return 0;
}

3.
#include <iostream>
#include <algorithm>
#include <time.h>
#include <set>

using namespace std;

#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0x1F
#define N 10000000
int a[N/BITSPERWORD + 1];
int b[N], c[N], d[N];

void setb(int i) { a[i>>SHIFT] |= (1 << (i & MASK)); }
void clrb(int i) { a[i>>SHIFT] &= ~(1 << (i & MASK)); }
int testb(int i) { return a[i>>SHIFT] & (1 << (i & MASK)); }

int main()
{
        int i;
        time_t t_start, t_end;
        for (i = 0; i < N; ++i)
                b[i] = i;
        for (i = 0; i < N; ++i)
                swap(b[i], b[rand()]); // rand函数产生的随机数在区间[0,RAND_MAX],RAND_MAX通常为32767
        t_start = time(nullptr);
        for(i = 0; i < N; ++i)
                clrb(i);
        for(i = 0; i < N; ++i)
                setb(b[i]);
        for(i = 0; i < N; ++i)
                if(testb(i))
                        c[i] = i;
        t_end = time(nullptr);
        for(i = 0; i < 100; ++i)
        {
                cout << c[i] << '\t';
                if(i%5 == 9)
                        cout << endl;
        }
        cout << "位图排序用时: " << difftime(t_end, t_start) <<endl;

        t_start = time(nullptr);
        set<int> S;
        set<int>::iterator j;
        for(i = 0; i < N; ++i)
                S.insert(b[i]);
        for(i = 0, j = S.begin(); j != S.end(); ++i, ++j) // 这里不能用小于号,因为未定义该操作
                d[i] = *j;
        t_end = time(nullptr);
        for(i = 0; i < 100; ++i)
        {
                cout << d[i] << '\t';
                if(i%5 == 9)
                        cout << endl;
        }
        cout << "标准库set排序用时: " << difftime(t_end, t_start) <<endl;

        t_start = time(nullptr);
        sort(b, b+N);
        t_end = time(nullptr);
        for(i = 0; i < 100; ++i)
        {
                cout << b[i] << '\t';
                if(i%5 == 9)
                        cout << endl;
        }
        cout << "sort算法排序用时: " << difftime(t_end, t_start) <<endl;
        return 0;
}

4.
#include <iostream>
#include <algorithm>

using namespace std;

#define N 100
int a[N];

int main()
{
        int i;
        time_t t_start, t_end;
        for(i = 0; i < N; ++i)
                a[i] = i;
        for(i = 0; i < N; ++i)
                swap(a[i], a[rand()%N]);
        for(i = 0; i < N; ++i)
        {
                cout << a[i] << '\t';
                if(i%10 == 9)
                        cout << endl;
        }
        cout << endl;
        return 0;
}

5.
#include <iostream>
#include <algorithm>
#include <time.h>
#include <set>

using namespace std;

#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0x1F
#define N 10000000
int a[N/2/BITSPERWORD + 1]; // 采用两趟算法,大小减半
int b[N], c[N];

void setb(int i) { a[i>>SHIFT] |= (1 << (i & MASK)); }
void clrb(int i) { a[i>>SHIFT] &= ~(1 << (i & MASK)); }
int testb(int i) { return a[i>>SHIFT] & (1 << (i & MASK)); }

int main()
{
        int i;
        time_t t_start, t_end;
        for (i = 0; i < N; ++i)
                b[i] = i;
        for (i = 0; i < N; ++i)
                swap(b[i], b[rand()]);

        /* 第一趟排序[0, N/2) */
        for(i = 0; i < N/2; ++i)
                clrb(i);
        for(i = 0; i < N; ++i)
                if(b[i] < N/2)
                        setb(b[i]);
        for(i = 0; i < N; ++i)
                if(testb(i))
                        c[i] = i;

        /* 第二趟排序[N/2, N) */
        for(i = 0; i < N/2; ++i)
                clrb(i);
        for(i = 0; i < N; ++i)
                if(b[i] >= N/2)
                        setb(b[i]-N/2);
        for(i = 0; i < N; ++i)
                if(testb(i))
                        c[i+N/2] = i+N/2;
        for(i = N/2-50; i < N/2+50; ++i)
        {
                cout << c[i] << '\t';
                if(i%5 == 9)
                        cout << endl;
        }

        return 0;
}

你可能感兴趣的:(编程珠玑)