User:Larry/Code

From Algorithmist
Jump to navigation Jump to search

Here's a bunch of my code snipplets that I copy and paste when necessary.

Java[edit]

Sorting[edit]

//Stupid Comparator that costed me 750 pts: public class D implements Comparable { // Declare variables here

// Constructor public D( Stuff Stuff ){ // Initialize Variables }

public int compareTo( Object o ){ D variable = ( D ) o; // Return 0 if it's the same // Returns negative if it's less than // Returns positive if it's more than }

Vector conversion[edit]

/* Vector to Int Array */ int[] v2i( Vector a ) {

  int[] k = new int[ a.size() ];
  for ( int i = 0; i < k.length; i++ )
     k[i] = ( (Integer) a.get( i ) ).intValue();
  return k;

}

/* Vector to Double Array */ double[] v2i( Vector a ) {

  double[] k = new double[ a.size() ];
  for ( int i = 0; i < k.length; i++ )
     k[i] = ( (Double) a.get( i ) ).doubleValue();
  return k;

}

C#[edit]

String Search[edit]

Array.Sort( StringComparer.Ordinal );

Binominal[edit]

// Initialize to -1's. int[,] dp = new int[100,100];

int choose( int n, int k ) {

 if ( k == n || k == 0 ) return 1;
 if ( dp[n,k] >= 0 ) return dp[n,k];
 return dp[n,k] = choose( n - 1, k - 1 ) + choose( n - 1, k );

}

Most[edit]

GCD[edit]

int gcd( int a, int b ){

  return b > 0 ? gcd( b, a % b ) : a;

}

Cross Product[edit]

/* Cross Product */ double cross( double ax, double ay, double bx, double by, double cx, double cy ) {

    return ( ( bx - ax ) * ( cy - ay ) ) - ( ( cx - ax ) * ( by - ay ) );

}

/* Cross Product */ int cross( int ax, int ay, int bx, int by, int cx, int cy ) {

    return ( ( bx - ax ) * ( cy - ay ) ) - ( ( cx - ax ) * ( by - ay ) );

}