StdArrayIO.java
public class StdArrayIO
{
private StdArrayIO() { }
public static double[] readDouble1D()
{
int N = StdIn.readInt();
double[] a = new double[N];
for (int i = 0; i < N; i++)
{
a[i] = StdIn.readDouble();
}
return a;
}
public static void print(double[] a)
{
int N = a.length;
StdOut.println(N);
for (int i = 0; i < N; i++)
{
StdOut.printf("%9.5f ", a[i]);
}
StdOut.println();
}
public static double[][] readDouble2D()
{
int M = StdIn.readInt();
int N = StdIn.readInt();
double[][] a = new double[M][N];
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
a[i][j] = StdIn.readDouble();
}
}
return a;
}
public static void print(double[][] a)
{
int M = a.length;
int N = a[0].length;
StdOut.println(M + " " + N);
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
StdOut.printf("%9.5f ", a[i][j]);
}
StdOut.println();
}
}
public static int[] readInt1D()
{
int N = StdIn.readInt();
int[] a = new int[N];
for (int i = 0; i < N; i++)
{
a[i] = StdIn.readInt();
}
return a;
}
public static void print(int[] a)
{
int N = a.length;
StdOut.println(N);
for (int i = 0; i < N; i++)
{
StdOut.printf("%9d ", a[i]);
}
StdOut.println();
}
public static int[][] readInt2D()
{
int M = StdIn.readInt();
int N = StdIn.readInt();
int[][] a = new int[M][N];
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
a[i][j] = StdIn.readInt();
}
}
return a;
}
public static void print(int[][] a)
{
int M = a.length;
int N = a[0].length;
StdOut.println(M + " " + N);
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
StdOut.printf("%9d ", a[i][j]);
}
StdOut.println();
}
}
public static boolean[] readBoolean1D()
{
int N = StdIn.readInt();
boolean[] a = new boolean[N];
for (int i = 0; i < N; i++)
{
a[i] = StdIn.readBoolean();
}
return a;
}
public static void print(boolean[] a)
{
int N = a.length;
StdOut.println(N);
for (int i = 0; i < N; i++)
{
if (a[i])
StdOut.println("1 ");
else
StdOut.println("0 ");
}
StdOut.println();
}
public static boolean[][] readBoolean2D()
{
int M = StdIn.readInt();
int N = StdIn.readInt();
boolean[][] a = new boolean[M][N];
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
a[i][j] = StdIn.readBoolean();
}
}
return a;
}
public static void print(boolean[][] a)
{
int M = a.length;
int N = a[0].length;
StdOut.println(M + " " + N);
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (a[i][j])
StdOut.print("1 ");
else
StdOut.print("0 ");
}
StdOut.println();
}
}
public static void main(String[] args)
{
double[] a = StdArrayIO.readDouble1D();
StdArrayIO.print(a);
StdOut.println();
double[][] b = StdArrayIO.readDouble2D();
StdArrayIO.print(b);
StdOut.println();
boolean[][] d = StdArrayIO.readBoolean2D();
StdArrayIO.print(d);
StdOut.println();
}
}
IFS.java
public class IFS
{
public static void main(String[] args)
{
int T = Integer.parseInt(args[0]);
double[] dist = StdArrayIO.readDouble1D();
double[][] cx = StdArrayIO.readDouble2D();
double[][] cy = StdArrayIO.readDouble2D();
double x = 0.0, y = 0.0;
for (int t = 0; t < T; t++)
{
int r = StdRandom.discrete(dist);
double x0 = cx[r][0]*x + cx[r][1]*y + cx[r][2];
double y0 = cy[r][0]*x + cy[r][1]*y + cy[r][2];
x = x0;
y = y0;
StdDraw.point(x, y);
if (t % 100 == 0)
StdDraw.show(10);
}
StdDraw.show(0);
}
}
No comments:
Post a Comment