HOJ 2201 Sum of Cubes

探索每一个整数是否能被分解为两个立方数之和,本文详细解析算法并提供实例解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time limit : 10 sec Memory limit : 64 M


According to Goldbach’s conjecture, every evennumber can be expressed as a sum of two oddprimes. Which numbers can be expressed as thesum of two cubes?

For each test case, a line will contain a positive integer n which will be at most one million.For each test case, output two integers x and y such that x3 + y3 = n. If there are manysuch pairs of numbers, choose x to be as small as possible. If there are no such pairs, output“impossible”.

The last line will contain the integer 0, which should not be processed.

Sample Input

1
2
3
1000000
0

Sample Output

0 1
1 1
impossible
0 100


Solution:

#include <stdio.h>
#include <math.h>

int main()
{
        int x, y, n;
        while(1 == scanf("%d", &n))
        {
                if(0 == n)
                        return 0;
                x = -600;
                y = 600;
                for(;x <= y;)
                {
                        if ((int)(pow(x, 3) + pow(y, 3)) > n)
                        {
                                --y;
                        }
                        else if ((int)(pow(x, 3) + pow(y, 3)) < n)
                        {
                                ++x;
                        }
                        else
                        {
                                printf("%d %d\n", x, y);
                                break;
                        }
                }
                if (x > y)
                {
                        printf("impossible\n");
                }
        }
        return 0;
} 


注意:x和y并没有指明是正数



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值