POJ 1543 解题报告

来源:互联网 发布:linux进去数据库命令 编辑:程序博客网 时间:2024/06/08 18:48

这里我是先打表100以内数的三次方。然后三重循环对所有的组合进行遍历,二分查找看是否是某个数的三次方。对所有的四数对进行排序输出。这里面用的是stl里面的binary_search。自己写的话能直接得到a,这里面又求了依次三次方根。

POJ不认识cbrt所以compile error了一次,改用pow(sum, 1.0/3)发现每次都少1,最后又加了个1。

thestoryofsnow1543Accepted232K0MSC++1560B

/* ID: thestor1 LANG: C++ TASK: poj1543 */#include <iostream>#include <fstream>#include <cmath>#include <cstdio>#include <cstring>#include <limits>#include <string>#include <vector>#include <list>#include <set>#include <map>#include <queue>#include <stack>#include <algorithm>#include <cassert>using namespace std;const int MAXN = 100 + 1;class Pair4{public:int a, b, c, d;Pair4() {}Pair4(int a, int b, int c, int d) : a(a), b(b), c(c), d(d) {}inline bool operator< (const Pair4& rhs) const {if (this->a != rhs.a){return this->a < rhs.a;}if (this->b != rhs.b){return this->b < rhs.b;}if (this->c != rhs.c){return this->c < rhs.c;}if (this->d != rhs.d){return this->d < rhs.d;}return true;}};int main(){// a^3 = b^3 + c^3 + d^3// a <= N (N <= 100)int N;scanf("%d", &N);int cubes[MAXN];for (int i = 1; i <= N; ++i){cubes[i] = i * i * i;}std::vector<Pair4> pair4s;int sum;for (int b = 2; b < N; ++b){for (int c = b; c < N; ++c){for (int d = c; d < N; ++d){sum = cubes[b] + cubes[c] + cubes[d];if (binary_search(cubes + 1, cubes + N + 1, sum)){// printf("%d = %d ^ 3 + %d ^ 3 + %d ^ 3\n", sum, b, c, d);pair4s.push_back(Pair4(sum, b, c, d));}}}}sort(pair4s.begin(), pair4s.end());for (int i = 0; i < pair4s.size(); ++i){printf("Cube = %d, Triple = (%d,%d,%d)\n", (int)pow((double)pair4s[i].a, (double)1.0 / 3) + 1, pair4s[i].b, pair4s[i].c, pair4s[i].d); }return 0;  }



0 0
原创粉丝点击