HOJ 2201 Sum of Cubes

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並沒有指明是正數



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章