| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
大水啊,16ms#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
using namespace std;
int cubes[101];
int isCube(int n){
int cr = (int)pow(n+0.0, 1/3.0);
if(cr*cr*cr==n) return cr;
if((cr+1)*(cr+1)*(cr+1)==n) return cr+1;
else return -1;
}
void init(){
for(int i = 0; i <= 100; i++) cubes[i] = i*i*i;
}
struct cube{
int a,b,c,d;
}cub[2000];
bool cmp(const cube &c1, const cube &c2){
return c1.a < c2.a || (c1.a==c2.a && c1.d < c2.d);
}
int gs = 0;
void preprocess(){
init();
for(int a=6; a<=100; a++){
//cout << a << endl;
int ca = cubes[a];
int lb = (int)pow((ca-1)/3.0, 1/3.0)+1;
//cout << lb << endl;
for(int b = lb; b < a; b++){
for(int c = 2; c <= b; c++){
int d;
if((d = isCube(ca-cubes[b]-cubes[c])) != -1){
if(d<=c && d>1){
//cout << a << " " << b << " " << c << " " << d << endl;
cub[gs].a = a;
cub[gs].b = b;
cub[gs].c = c;
cub[gs].d = d;
gs++;
}
}
}
}
}
sort(cub, cub+gs, cmp);
}
int main() {
//cout << isCube(216) << endl;
//cout << 1 << endl;
preprocess();
//cout << 1 << endl;
int n;
scanf("%d", &n);
for(int i = 0; i < gs; i++){
if(cub[i].a > n) break;
printf("Cube = %d, Triple = (%d,%d,%d)\n", cub[i].a, cub[i].d, cub[i].c, cub[i].b);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator