POJ 1543 解题报告

这里我是先打表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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值