算法竞赛入门经典第二章

math.h
floor(x)函数返回不超过x的最大整数

time.h
clock()函数返回程序目前为止运行的时间,除以CLOCKS_PER_SEC得到单位为秒

输入结束符
Windows:Ctrl+Z
Linux:Ctrl+D

输入输出重定向:

freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);

编译选项_DEBUG的使用
DEBUG模式下输出到文件data.out,从data.in输入
不是DEBUG即忽略#define LOCAL,从而从标准输入输入,输出到标准输出

#include
using namespace std;
#ifdef _DEBUG
    #define LOCAL
#endif // _DEBUG
int main() {
    int k;
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif // LOCAL
    scanf("%d", &k);
    printf("%d\n", k);
}

文件输入输出版本,不要忘记fclose()

#include
using namespace std;
int main() {
    int k;
    FILE *fin, *fout;
    fin = fopen("data.in", "rb");
    fout = fopen("data.out", "wb");
    fscanf(fin, "%d", &k);
    fprintf(fout,"%d\n", k);
    fclose(fin);
    fclose(fout);
}

习题2-1

#include
using namespace std;
#ifdef _DEBUG
#define LOCAL
#endif // _DEBUG
int main() {
    int k=99;
    while (++k < 1000) {
        int A = k / 100, B = k / 10 % 10, C = k % 10;
        if (A*A*A + B*B*B + C*C*C == k) {
            cout << k <#ifdef LOCAL
            cout << A << " " << B << " " << C << endl;
#endif // LOCAL
        }
    }
}

习题2-2

#include
using namespace std;
int main() {
    int a, b, c, ans, cnt = 0;
    while (scanf("%d%d%d", &a, &b, &c)) {
        ++cnt;
        ans = 0;
        for (int i = 10; i < 100; ++i) {
            if (i % 3 == a && i % 5 == b && i % 7 == c) {
                ans = i;
                break;
            }
        }
        cout << "Case " << cnt << ": ";
        if (ans) cout << ans << endl;
        else cout << "No answer" << endl;
    }
}

习题2-3

#include
using namespace std;
int main() {
    int n;
    cin >> n;
    n = n*2-1;
    int cnt = 0;
    while (n > 0) {
        for (int i = 1; i <= cnt; ++i) cout << " ";
        for (int i = 1; i <= n; ++i)cout << "#";
        cout << endl;
        n -= 2, ++cnt;
    }
}

习题2-4

#include
using namespace std;
int main() {
    long long int n, m,cnt=0;
    double sum;
    while (scanf("%lld%lld", &n, &m) && n) {
        ++cnt;
        sum = 0.0;
        while (n <= m) {
            sum += 1.0 / (n*n);
            ++n;
        }
        printf("Case %lld: %.5lf\n", cnt, sum);
    }
}

习题2-5

#include
using namespace std;
#pragma warning(disable:4996)
int main() {
    int a, b, c, cnt = 0;
    int sta[105], top = -1;
    while (scanf("%d%d%d", &a, &b, &c) && (a || b || c)) {
        ++cnt;
        top = -1;
        c += 2;
        while (c--)
            sta[++top] = a / b, a = a % b * 10;
        if(sta[top]>=5){
            int k = top-1;
            while (k>0 && sta[k] == 9)
                sta[k--] = 0;
            sta[k] += 1;
        }
        printf("Case %d: %d.", cnt, sta[0]);
        for (int i = 1; i < top; ++i)
            printf("%d", sta[i]);
        cout << endl;
    }
}

习题2-6

#include
using namespace std;
int main() {
    int vis[10] = {};
    for (int a = 1; a <= 9; ++a) {
        vis[a] = 1;
        for (int b = 1; b <= 9; ++b) {
            if (vis[b]) continue;
            vis[b] = 1;
            for (int c = 1; c <= 9; ++c) {
                if (vis[c]) continue;
                vis[c] = 1;
                for (int d = 1; d <= 9; ++d) {
                    if (vis[d]) continue;
                    vis[d] = 1;
                    for (int e = 1; e <= 9; ++e) {
                        if (vis[e]) continue;
                        vis[e] = 1;
                        for (int f = 1; f <= 9; ++f) {
                            if (vis[f]) continue;
                            vis[f] = 1;
                            for (int g = 1; g <= 9; ++g) {
                                if (vis[g]) continue;
                                vis[g] = 1;
                                for (int h = 1; h <= 9; ++h) {
                                    if (vis[h]) continue;
                                    vis[h] = 1;
                                    for (int i = 1; i <= 9; ++i) {
                                        if (vis[i]) continue;
                                        vis[i] = 1;
                                        int A = a * 100 + b * 10 + c, B = d * 100 + e * 10 + f, C = g * 100 + h * 10 + i;
                                        if (A * 2 == B &&A * 3 == C)
                                            printf("%d %d %d\n", A, B, C);
                                        vis[i] = 0;
                                    }
                                    vis[h] = 0;
                                }
                                vis[g] = 0;
                            }
                            vis[f] = 0;
                        }
                        vis[e] = 0;
                    }
                    vis[d] = 0;
                }
                vis[c] = 0;
            }
            vis[b] = 0;
        }
        vis[a] = 0;
    }
} 

思考题1
任务1

#include
int main() 
{
    int n;
    scanf("%d", &n);
    for (int i = 2; i <= 2*n; i+=2)
        printf("%d\n", i);
    return 0;
}

任务2

#include
int main() 
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
        printf("%d\n", i<<1);
    return 0;
}

题2
无限循环:每次+0.1会有误差,从而导致10.0的时候不等于10
算法竞赛入门经典第二章_第1张图片

#include
int main() 
{
    double i;
    for (i = 0;i != 10; i += 0.1)
        printf("%.1f\n", i);
}

你可能感兴趣的:(算法竞赛入门经典,算法竞赛入门经典)