HOJ 2201 Sum of Cubes

来源:互联网 发布:苹果同步铃声软件 编辑:程序博客网 时间:2024/05/17 05:07

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

12310000000

Sample Output

0 11 1impossible0 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并没有指明是正数



原创粉丝点击