[UVA][Math] 11428 - Cubes
Problem B
Cubes
Input: Standard Input
Output: Standard Output
Given a positive integer N you will have to find two positive integers x and y such that:
Input
The input file contains at most 10o lines of inputs. Each line contains a positive integer N (0<N≤10000). Input is terminated by a line containing a single zero. This line should not be processed.
Output
For each line of input produce one or more lines of output. Each of these lines contains two positive integers x, y separated by a single space, such that. If there is no such integer values of x and y then produce the line “No solution” instead. If there is more than one solution then output the one with smallest value of y.
Sample Input Output for Sample Input
7 37 12 0
|
2 1 4 3 No solution
|
Problemsetter: Shahriar Manzoor
Special Thanks: Derek Kisman
很明顯的會發現 59^3 - 58^3 已經是極限了,
而如果 x 的範圍再大的話, 則 y 不管怎樣, 都會超出範圍
#include <stdio.h>
int main() {
int i, j;
int x[10001], y[10001];
int hasAns[10001] = {};
for(i = 2; i <= 60; i++) {
for(j = i-1; j >= 1; j--) {
if(i*i*i - j*j*j <= 10000 && hasAns[i*i*i - j*j*j] == 0) {
hasAns[i*i*i - j*j*j] = 1;
x[i*i*i - j*j*j] = i;
y[i*i*i - j*j*j] = j;
}
}
}
int n;
while(scanf("%d", &n) == 1 && n) {
if(hasAns[n])
printf("%d %d\n", x[n], y[n]);
else
puts("No solution");
}
return 0;
}