import java.util.*;
class it1 implements Iterator<Integer> {
int v, nn;
public it1 (int n) {
v=0;
nn = (int)Math.pow(2, n);
}
public boolean hasNext () {
if (v < nn) { return true; }
return false;
}
public Integer next () { return v++; }
public void reset () { v=0; }
public void remove() {}
}
public class f10 {
public void tt (int n) {
int i, j, b, ibest=0;
double h, h1, hh, h1best=0;
double e[] = new double[n];
for (i=0; i<n; i++) { e[i] = Math.sqrt(i); }
for (i=0,h=0; i<n; i++) { h += e[i]; } hh = h/2;
it1 ii = new it1 (n);
while (ii.hasNext()) {
i=ii.next();
for (j=0, b=1, h1=0; j<n; j++, b<<=1) {
if ((i & b) != 0) { h1 += e[j]; }
}
if (Math.abs(h1-hh) < Math.abs(h1best-hh)) {
h1best=h1; ibest=i;
}
}
System.out.println ("hh="+hh+" h1best="+h1best+" ibest="+
Integer.toString(ibest, 2));
}
public static void main (String args[]) {
f10 f = new f10();
f.tt(Integer.parseInt(args[0]));
}
}
I would appreciate any help

New Topic/Question
Reply



MultiQuote





|