算法竞赛入门经典(刘汝佳)——代码笔记

Reference: 《算法竞赛入门经典》(刘汝佳)第一版、第二版

------------------------------------------------------------------------------------------------------------------------------------------------

1. 第2章 小结与习题

(1) Ex1 第一版P32 2-7 近似计算

#include <iostream>
//#include <cstdio>
//#include <ctime>
#include <fstream>
#include <cmath>
//#include <iomanip>

using namespace std;

ifstream fin("input.txt");
ofstream fout("output.txt");

// compute the value of PI
int main()
{
    double pi = 0;
    int i = 1;
    double temp = static_cast<double>(1)/(2*i-1);
    while(fabs(temp - 1e-6) > 1e-9)  // the last item is lesser than 1e-6
    {
        pi += temp;
        i++;
        temp = static_cast<double>(1)/(2*i-1);
        if(i%2==0)
            temp = -temp;
    }
    pi += temp;
    pi = 4*pi;

    fout << pi << endl;

    return 0;
}

(2)Ex2 第一版P32 2-8 子序列的和

#include <iostream>
//#include <cstdio>
//#include <ctime>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;

ifstream fin("input.txt");
ofstream fout("output.txt");

// compute the value of PI
int main()
{
    int n, m;
    fin >> n >> m;
    //long long n_square = n*n, m_square = m*m;
    double result = 0.0;

    for(long long i = n; i <= m; i++)  // i的类型设为long long避免在i*i时发生溢出
    {
        result += static_cast<double>(1)/(i*i);
    }
    fout << setprecision(6) << result << endl;  // setprecesion()设定有效位数而非小数的位数

    return 0;
}

(3)Ex3 第一版P32 2-9 分数化小数

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    FILE *fin, *fout;
    fin = fopen("input.txt", "r");
    fout = fopen("output.txt", "w");

    int a, b, c;
    fscanf(fin, "%d%d%d", &a, &b, &c);
    double result;

    result = static_cast<double>(a)/b;
    fprintf(fout, "%.*f", c, result);  // printf对于输出任意精度可以用此格式

    fclose(fin);
    fclose(fout);

    return 0;
}


2. 第3章 数组和字符

(1)Ex1 第一版P34 3-1 开灯问题

#include <iostream>
#include <cstdio>
#include <fstream>
#define MAX_LEN 1000+10

using namespace std;

ifstream fin("input.txt");
ofstream fout("output.txt");

int a[MAX_LEN] = {0};

int main()
{
    int n, k;
    fin >> n >> k;

    int i,j;
    for(i = 1; i <= k; i++)  // k个人按开关操作k次
    {
        for(j = 1; j <= n; j++)
        {
            if(j%i==0)  // 进行按下开关的操作
                a[j] = !a[j];
        }
    }
    // 输出亮着的灯的编号
    for(i = 1; i < n; i++)
        if(a[i]==1)
            fout << i << " ";
    if(a[i]==1)
        fout << i << endl;
    else
        fout << endl;

    return 0;
}

(2)Ex2 第一版P35 3-2 蛇形填数

#include <iostream>
#include <cstring>
#define MAX_LEN 10

using namespace std;
 
int a[MAX_LEN][MAX_LEN];

int main()
{
    FILE *fin, *fout;
    fin = fopen("input.txt", "r");
    fout = fopen("output.txt", "w");

    int n;
    fscanf(fin, "%d", &n);
    int x, y, num = 0;

    memset(a, 0, sizeof(a));
    num = a[x=0][y=n-1] = 1;

    while(num < n*n)
    {
        while(x+1<n && !a[x+1][y])  // 向下移动至边界或已经有数填入的位置为止
            a[++x][y] = ++num;
        while(y-1>=0 && !a[x][y-1])  // 向左移动
            a[x][--y] = ++num;
        while(x-1>=0 && !a[x-1][y])  // 向上移动
            a[--x][y] = ++num;
        while(y+1<n && !a[x][y+1])  // 向右移动
            a[x][++y] = ++num;
    }
    for(x = 0; x < n; x++)
    {
        for(y = 0; y < n; y++)
            fprintf(fout, "%3d", a[x][y]);  // 用%3d来输出保持格式对齐,不用输出空格
        fprintf(fout, "\n");
    }
    fclose(fin);
    fclose(fout);

    return 0;
}

(3)第一版P37 3-3 竖式问题

<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值