publicclassInversions{// Return the number of inversions in the permutation a[].publicstaticlongcount(int[] a){long count =0;for(int i =0; i < a.length; i++){for(int j = i +1; j < a.length; j++){if(a[i]> a[j]){
count++;}}}return count;}// Return a permutation of length n with exactly k inversions.publicstaticint[]generate(int n,long k){int i =0, j = n -1;int[] p =newint[n];for(int idx =0; idx < n; idx++){if(k >=(j - i)){
k -= j;
p[idx]= j--;}else{
p[idx]= i++;}}return p;}// Takes an integer n and a long k as command-line arguments,// and prints a permutation of length n with exactly k inversions.publicstaticvoidmain(String[] args){int n =Integer.parseInt(args[0]);long k =Long.parseLong(args[1]);int[] p =generate(n, k);for(int i : p){StdOut.print(i +" ");}}}
publicclassRamanujan{// Is n a Ramanujan number?publicstaticbooleanisRamanujan(long n){int count =0;long n_cbrt =(long)Math.cbrt(n);for(long i =1; i <= n_cbrt; i++){double j =Math.cbrt(n - i * i * i);if(Math.abs(j -Math.round(j))<Double.MIN_VALUE){
count++;}}return count >=4;}// Takes a long integer command-line arguments n and prints true if// n is a Ramanujan number, and false otherwise.publicstaticvoidmain(String[] args){long n =Long.parseLong(args[0]);StdOut.println(isRamanujan(n));}}
publicclassMaximumSquareSubmatrix{// Returns the size of the largest contiguous square submatrix// of a[][] containing only 1s.publicstaticintsize(int[][] a){int n = a.length;int[][] aux =newint[n][n];for(int i =0; i < n; i++){for(int j =0; j < n; j++){
aux[i][j]= a[i][j];}}for(int i =1; i < n; i++){for(int j =1; j < n; j++){// here is the keyif(aux[i][j]==1){int min =Math.min(aux[i-1][j], aux[i][j-1]);
min =Math.min(min, aux[i-1][j-1])+1;
aux[i][j]=Math.max(aux[i][j], min);}}}int ans =0;for(int i =0; i < n; i++){for(int j =0; j < n; j++){
ans =Math.max(ans, aux[i][j]);}}return ans;}// Reads an n-by-n matrix of 0s and 1s from standard input// and prints the size of the largest contiguous square submatrix// containing only 1s.publicstaticvoidmain(String[] args){int n =StdIn.readInt();int[][] a =newint[n][n];for(int i =0; i < n; i++){for(int j =0; j < n; j++){
a[i][j]=StdIn.readInt();}}int ans =size(a);StdOut.println(ans);}}
Here is the original linkRamanujan.javaRevesPuzzle.java(汉诺塔问题的升级版,4根柱子的情况)public class Ramanujan { // Is n a Ramanujan number? public static boolean isRamanujan(long n) { int count = 0; for (long i = 1, n_3 = (long)Math