Introduction

This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)

Saturday, November 3, 2012

uva 386 - Perfect Cubes


Problem link
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
using namespace std;
const double eps = 1E-9;
//-----------------------------
class ans { public: int a; int b; int c; int d;};
//-----------------------------
ans kq[225];
//-----------------------------
bool behon(ans a1, ans a2) {
 if (a1.a<a2.a) return(true);
 else if (a1.a==a2.a && a1.b<a2.b) return(true);
 else if (a1.a==a2.a && a1.b==a2.b && a1.c<a2.c) return(true);
 return(false);
}
//-----------------------------
int main() {
 int cnt=0;
 for (int i=2; i<=200; i++)
  for (int j=i+1; j<=200; j++)
   for (int k=j+1; k<=200; k++)
   {
    double sum=i*i*i+j*j*j+k*k*k;
    double z = pow(sum,1.0/3.0);
    if (int(z+eps)*int(z+eps)*int(z+eps)==int(sum) && int(z+eps)<=200) 
    {
     cnt++;
     kq[cnt].a=int(z+eps);
     kq[cnt].b=i;
     kq[cnt].c=j;
     kq[cnt].d=k;
    }
   }
 for (int i=1; i<=cnt-1; i++)
  for (int j=i+1; j<=cnt; j++)
   if (behon(kq[j],kq[i])) 
   {
    ans tmp;
    tmp=kq[i]; kq[i]=kq[j]; kq[j]=tmp;
   }
 for (int i=1; i<=cnt; i++)
  cout << "Cube = " << kq[i].a << ", Triple = (" << kq[i].b << "," << kq[i].c << "," << kq[i].d
    << ")" << endl;
 return 0;
}
//-----------------------------

No comments:

Post a Comment