Skip to content

Instantly share code, notes, and snippets.

View yao2030's full-sized avatar

yao2030 yao2030

  • Shanghai, China
View GitHub Profile
@yao2030
yao2030 / Square.java
Created November 1, 2012 02:10
Recursive squares
public class Square
{
public static void drawSquare(int N, double x, double y, double r)
{
if(N == 0)
return;
StdDraw.square(x, y, r);
StdDraw.show(100);
drawSquare(N-1, x - r , y + r, r/2.2);
drawSquare(N-1, x + r , y + r, r/2.2);
@yao2030
yao2030 / Combination.java
Created October 31, 2012 12:38
Use recursion to implement combination of a set of characters
public class Combination
{
public static void combinate(String prefix, String s)
{
StdOut.println(prefix);
int N = s.length();
if (N == 0) return;
for (int i = 0; i < N; i++)
combinate(prefix + s.charAt(i), s.substring(i+1, N));
@yao2030
yao2030 / Permutation.java
Created October 31, 2012 09:15
Permutations and Permutations of size k, using recursion to implement
public class Permutation
{
public static void perm1(String s) { perm1("", s); }
public static void perm1(String prefix, String s)
{
int N = s.length();
if (N == 0) System.out.println(prefix);
else
{
for(int i = 0; i < N; i++)
@yao2030
yao2030 / BP.java
Created October 31, 2012 01:19
use recursion to print out the binary representation of an integer
public class BP
{
public static String binary(int N)
{
if(N / 2 == 0) return "" + N;
return binary(N/2) + "" + N % 2;
}
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
@yao2030
yao2030 / Ruler.java
Created October 30, 2012 10:30
a recursive program to write plot the subdivisions of a ruler using StdDraw library, <introduction to programming in java an interdisciplinary approach
public class Ruler
{
public static String ruler(int N)
{
if (N <= 0) return "";
if (N == 1) return "1";
return ruler(N-1) + N + ruler(N-1);
}
public static void drawRuler(String a)
@yao2030
yao2030 / Post.java
Created October 29, 2012 01:32
Post bar code. U.S. Postal System
public class Post
{
public static void halfHeight(double i)
{
StdDraw.setPenRadius(.01);
StdDraw.line(i, 0, i, 1);
}
public static void fullHeight(double i)
{
StdDraw.setPenRadius(0.01);
@yao2030
yao2030 / Mine.java
Created October 28, 2012 08:59
mine sweeper
public class Mine
{
public static void main(String[] args)
{
int M = Integer.parseInt(args[0]);
int N = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[2]);
boolean[][] mine = new boolean[M+2][M+2];
for(int i = 1; i <= M; i++)
@yao2030
yao2030 / Calendar.java
Created October 28, 2012 08:58
calenday java implementating
public class Calendar
{
public static boolean isLeapYear(int year)
{
if((year % 4 ==0 && year % 100 != 0) || (year % 400 == 0))
return true;
return false;
}
public static String getMonth(int month)
{
@yao2030
yao2030 / Hadamar.java
Created October 23, 2012 06:40
Hadamard matrix
public class Hadamard
{
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
boolean[][] H = new boolean[N][N];
H[0][0] = true;
for(int n = 1; n < N; n += n)
{
for(int i = 0; i < n; i++)
@yao2030
yao2030 / Kary.java
Created October 20, 2012 04:35
conver a number to k-base
public class Kary
{
public static void main(String[] args)
{
int i = Integer.parseInt(args[0]);
int k = Integer.parseInt(args[1]);
int v = 1;
while (v <= i/k)
v *= k;
int n = i;