package 排序.优先队列; import java.util.Scanner; public class CubeSum implements Comparable<CubeSum>{ private int i; private int j; private int sum; private CubeSum(int i,int j){ this.sum=i*i*i+j*j*j; this.i=i; this.j=j; } public String toString(){ return ("("+sum+","+i+","+j+")"); } public static void main(String args[]){ Scanner sc=new Scanner(System.in); int N=sc.nextInt(); MinPQ <CubeSum>pq=new MinPQ<CubeSum>(N); for(int i=0;i<=N;i++) { pq.insert(new CubeSum(i, 0)); } while(!pq.isEmpty()){ CubeSum s=pq.delMin(); System.out.print(s+" "); if(s.j<N) { pq.insert(new CubeSum(s.i, s.j+1)); //此处从https://algs4.cs.princeton.edu/24pq/CubeSum.java.html引入,感觉该方法收益很多 } } } @Override public int compareTo(CubeSum that) { if(this.sum<that.sum)return -1; if(this.sum>that.sum)return +1; return 0; }}
原文地址:https://www.cnblogs.com/mdl-lcc/p/11376166.html
时间: 2024-10-18 03:06:19