ACM刷题打卡-160309枚举

ZOJ 1331 - Perfect Cubes

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main() {

    //clock_t st, ed;
    //st = clock();

    vector cube;
    cube.push_back(0);
    for(int i = 1; i <= 200; ++i) {
        cube.push_back(pow(i, 3));
    }

    int a, b, c, d;
    for(a = 2; a <= 200; ++a) {
        for(b = 2; b <= 200; ++b) {
            if(cube[b] > cube[a])
                break;
            for(c = b; c <= 200; ++c) {
                if(cube[b] + cube[c] > cube[a])
                    break;
                for(d = c; d <= 200; ++d) {
                    if(cube[b] + cube[c] + cube[d] > cube[a])
                        break;
                    int right = cube[b] + cube[c] + cube[d];
                    int left = cube[a];
                    if(right == left) {
                        cout << "Cube = " << a << ", Triple = (" << b << "," << c << "," << d << ")" << endl;
                    }
                }
            }
        }
    }
    //ed = clock();
    //double dur = (double)(ed - st);
    //cout << (dur / CLOCKS_PER_SEC);
    return 0;
}

ZOJ 1760 - Doubles

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main() {

    int first;
    while(cin >> first && first != -1) {
        vector list;
        list.push_back(first);
        int temp;
        while(cin >> temp && temp != 0) {
            list.push_back(temp);
        }

        int cnt = 0;
        for (int i = 0; i < list.size(); ++i) {
            for (int j = 0; j < list.size(); ++j) {
                if(list[j] == 2 * list[i])
                    cnt++;
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

ZOJ 2130 - 5 Spots

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main() {

    int w, h, sw, sh;
    while(scanf("%d %d %d %d",&h, &w, &sh, &sw) != EOF) {

        getchar();
        vector pic;
        vector part;

        for (int i = 0; i < h; ++i) {
            string line;
            getline(cin, line);
            pic.push_back(line);
        }

        for (int j = 0; j < sh; ++j) {
            string sline;
            getline(cin, sline);
            part.push_back(sline);
        }

        int cnt = 0;

        for (int i = 0; i <= h - sh; ++i) {
            for (int j = 0; j <= w - sw; ++j) {
                bool flag = true;

                for (int k = 0; k < sh; ++k) {
                    if (!flag) {
                        break;
                    }
                    for (int l = 0; l < sw; ++l) {
                        if (part[k][l] == '.') {
                            continue;
                        }
                        else if (part[k][l] != pic[i + k][j + l]) {
                            flag = false;
                            break;
                        }
                    }
                }

                if (flag) {
                    cnt++;
                }
            }
        }

        cout << cnt << endl;
    }

    return 0;
}

ZOJ 2189 - Exact Change Only

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main() {

    double exact;
    while(scanf("%lf",&exact) != EOF) {

        int a, b, c, d;
        int left = int(exact * 100);

        cin >> a >> b >> c >> d;
        int cnt = 999;
        vector result;
        for (int i = 0; i <= a; ++i) {
            for (int j = 0; j <= b; ++j) {
                for (int k = 0; k <= c; ++k) {
                    for (int l = 0; l <= d; ++l) {
                        int right = i * 25 + j * 10 + k * 5 + l;
                        int num = i + j + k + l;
                        
                        if(left == right && num < cnt) {
                            cnt = num;
                            result.clear();
                            result.push_back(i);
                            result.push_back(j);
                            result.push_back(k);
                            result.push_back(l);
                        }
                    }
                }
            }
        }
        if(cnt == 999) {
            cout << "NO EXACT CHANGE" << endl;
        }
        else {
            for (int i = 0; i < result.size(); ++i) {
                if(i == 0) {
                    cout << result[i];
                }
                else {
                    cout << " " << result[i];
                }
            }
            cout << endl;
        }
    }

    return 0;
}

你可能感兴趣的:(ACM刷题打卡-160309枚举)