Pages

Sunday 2 December 2012

UVA - 386 - Perfect Cubes

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        int[] cubes = new int[201];
        HashSet<Integer> hs=new HashSet<Integer>();
        for (int i = 2; i < 201; i++) {
            cubes[i] = i * i * i;
            hs.add(cubes[i]);
        }
        ArrayList<Number> list=new ArrayList<Number>();
        for(int i=2;i<200;i++){
            for(int j=2;j<i;j++){
               for(int z=2;z<j;z++){
                   if(hs.contains(cubes[i]+cubes[j]+cubes[z]))
                     list.add(new Number((int)Math.cbrt(cubes[i]+cubes[j]+cubes[z]), z, j,i));
               }
            }
        }
        Collections.sort(list);
        while(!list.isEmpty()){
            Number temp=list.remove(0);
            sb.append("Cube = ").append(temp.getA()).append(", Triple = (").append(temp.getB()).append(",").append(temp.getC()).append(",").append(temp.getD()).append(")\n");
        }
        System.out.print(sb);
    }
}

class Number implements Comparable<Number> {

    private int a, b, c, d;

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }

    public int getD() {
        return d;
    }

    public void setD(int d) {
        this.d = d;
    }

    public Number(int a, int b, int c, int d) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    @Override
    public int compareTo(Number o) {
        if (this.a > o.getA()) {
            return 1;
        } else if (this.a < o.getA()) {
            return -1;
        } else {
            if (this.b > o.getB()) {
                return 1;
            } else if (this.b < o.getB()) {
                return -1;
            } else {
                if (this.c > o.getC()) {
                    return 1;
                } else if (this.c < o.getC()) {
                    return -1;
                } else {
                    if (this.d > o.getD()) {
                        return 1;
                    } else if (this.d < o.getD()) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            }
        }
    }
}

No comments:

Post a Comment