Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
public class CubeSum implements Comparable<CubeSum> {
    private final int sum;
    private final int i;
    private final int j;

    public CubeSum(int i, int j) {
        this.sum = i*i*i + j*j*j;
        this.i = i;
        this.j = j;
    }

    public int compareTo(CubeSum that) {
        if (this.sum < that.sum) return -1;
        if (this.sum > that.sum) return +1;
        return 0;
    }

    public String toString() {
        return sum + " = " + i + "^3" + " + " + j + "^3";
    }


    public static void main(String[] args) {

        int N = Integer.parseInt(args[0]);

        // initialize priority queue
        MinPQ<CubeSum> pq = new MinPQ<CubeSum>();
        for (int i = 0; i <= N; i++) {
            pq.insert(new CubeSum(i, i));
        }

        // find smallest sum, print it out, and update
        while (!pq.isEmpty()) {
            CubeSum s = pq.delMin();
            StdOut.println(s);
            if (s.j < N) pq.insert(new CubeSum(s.i, s.j + 1));
        }
    }

}


The program prints 0(0^3+0^3) 1(0^3+1^3) 2(1^3 + 1^3) 8(0^3+2^3) 9(1^3+2^3) etc when a user supplies 10 for n,but i dont understand how dis is done.i saw this code online
Posted
Updated 26-Jul-12 5:34am
v3
Comments
Sandeep Mewara 26-Jul-12 9:23am    
I doubt on how anyone will explain you how the code is resulting in what you say.

Did DEBUGGING the code not helped?
brian 3 26-Jul-12 9:27am    
it did not help..iam still confused??? plz help
lewax00 26-Jul-12 10:11am    
I updated your question, this isn't C++, it's Java, and I fixed the pre tags to match.
brian 3 26-Jul-12 10:31am    
iam also trying to use dis program to find all distinct integers a,b,c and d between 0 and 10^6 such dat a^3 + b^3 = c^3 + d^3 eg 1729 = 9^3 + 10^3 = 1^3 + 12^3,but iam confused on how to do it,can u help me
[no name] 26-Jul-12 10:31am    
How is this repost different from your previous homework?

1 solution

If that code works (I've not looked at it) then you almost have it:

Main is the entry point, there are some functions, don't waste time trying to understand what a class is and try to focus on the smallest parts (divide and conquer).

Try to read it, the code itself is explanatory...

You should start looking for each instruction to see what that instruction does.

After that and little by little you will understand everything.

Good luck.
 
Share this answer
 
Comments
brian 3 26-Jul-12 11:57am    
@joan murt....plz i have looked at it and still dont understand it...dis is not a homework assignment.. when the user enters 10 as n,it will generate all those values i wrote above.
Joan M 26-Jul-12 12:00pm    
I'm sorry... I've seen your old post in which you were telling us that this was homework... :(
Anyway, don't expect to see it and in 2 minutes understand it... it will take longer than that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900