Вы находитесь на странице: 1из 9

Integer(int 

value)
          Constructs a newly allocated Integer object that represents the specified int
value. Integer(String s)
          Constructs a newly allocated Integer object that represents the int value
indicated by the String parameter.
parseInt(String s, int radix)
          Parses the string argument as a signed integer in the radix specified by the second
argument.
InputStreamReader isr = new InputStreamReader(System.in);
}
}

To provide the ability to read from a stream, the InputStreamReader class implements
the read() method that it inherits from Reader. The Reader class provides two versions
of the read() method.

public static void main(String[] args) {


InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
}
}
Because the role of the InputStreamReader variable is to initialize the
BufferedReader object, you do not have to first declare an
InputStreamReader variable. You can pass the instead of the
InputStreamReader class directly to the BufferedReader declaration.

This could be done as follows: public static void main(String[] args) {


BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
}
}
-----
static Node merge(Node a, Node b)
{ Node dummy = new Node();
Node head = dummy, c = head;
while ((a != null) && (b != null))
if (less(a.item, b.item))
{ c.next = a; c = a; a = a.next; }
else
{ c.next = b; c = b; b = b.next; }
c.next = (a == null) ? b : a;
return head.next;
}
-----
static Node mergesort(Node c)
{
if (c == null || c.next == null) return c;
Node a = c, b = c.next;
while ((b != null) && (b.next != null))
{ c = c.next; b = (b.next).next; }
b = c.next; c.next = null;
return merge(mergesort(a), mergesort(b));
}
-----
static Node mergesort(Node t)
{
NodeQueue Q = new NodeQueue();
if (t == null || t.next == null) return t;
for (Node u = null; t != null; t = u)
{ u = t.next; t.next = null; Q.put(t); }
t = Q.get();
while (!Q.empty())
{ Q.put(t); t = merge(Q.get(), Q.get()); }
return t;
}

class Sort
{
static void sort(ITEM[] a, int l, int r)
{ PQsort(a, l, r); }
static void PQsort(ITEM[] a, int l, int r)
{ int k;
PQ pq = new PQ(r-l+1);
for (k = l; k <= r; k++)
pq.insert(a[k]);
for (k = r; k >= l; k--)
a[k] = pq.getmax();
}
}
-----
for (int k = N/2; k >= 1; k--)
sink(k, N);
while (N > 1)
{ exch(1, N); sink(1, --N); }
-----
class TextSearch
{
public static void main(String[] args)
throws IOException
{ FileReader f = new FileReader(args[0]);
BufferedReader b = new BufferedReader(f);
String text = "", line = "";
while ((line = b.readLine()) != null)
text += line + " ";
TI ti = new TI(text);
In.init(); String q; int i;
while ((q = In.getString()) != null)
if ((i = ti.search(q)) < 0)
Out.println(q + " not found");
else Out.println(q + " " + i );
}
}
class TI
{
private String text;
private int[] index;
private int N;
TI(String s)
{ text = s; N = text.length();
index = new int[N+1]; index[N] = -1;
for (int i = 0; i < N; i++)
index[i] = i;
quicksort(index, 0, N-1);
}
private char s(int i)
{ return text.charAt(i); }
private boolean less(int v, int w)
{
if (v == w) return false;
for (int i = 0; ; i++)
if (w+i >= N) return false;
else if (v+i >= N) return true;
else if (s(v+i) < s(w+i)) return true;
else if (s(v+i) > s(w+i)) return false;
}
private void exch(int[] a, int i, int j)
{ int t = a[i]; a[i] = a[j]; a[j] = t; }
private void quicksort(int[] a, int l, int r)
// Program 7.1 with int for ITEM
int search(String v)
// Program 12.14
}
-----
private int compare(String s, int v)
{ char[] key = s.toCharArray();
int t = index[v];
for (int i = 0; i < key.length; i++)
if (t+i >= N) return 1;
else if (key[i] > text(t+i)) return 1;
else if (key[i] < text(t+i)) return -1;
return 0;
}
private int searchR(int l, int r, String v)
{ int m = (l+r)/2;
if (l > r) return N;
switch (compare(v, m))
{
case -1: return searchR(l, m-1, v);
case 1: return searchR(m+1, r, v);
}
return m; // case 0
}
int search(String v)
{ return index[searchR(0, N-1, v)]; }
class ST
{
private class Node
{ ITEM item; Node l, r;
Node(ITEM x) { item = x; }
}
private Node head;
ST(int maxN)
{ head = null; }
private Node insertR(Node h, ITEM x)
{
if (h == null)
return new Node(x);
if (less(x.key(), h.item.key()))
h.l = insertR(h.l, x);
else h.r = insertR(h.r, x);
return h;
}
void insert(ITEM x)
{ head = insertR(head, x); }
private ITEM searchR(Node h, KEY v)
{
if (h == null) return null;
if (equals(v, h.item.key())) return h.item;
if (less(v, h.item.key()))
return searchR(h.l, v);
else return searchR(h.r, v);
}
ITEM search(KEY key)
{ return searchR(head, key); }
-----
private int countR(Node h)
{ if (h == null) return 0;
return 1 + countR(h.l) + countR(h.r);
}
int count() { return countR(head); }
private String toStringR(Node h)
{ if (h == null) return "";
String s = toStringR(h.l);
s += h.item.toString() + "\n";
s += toStringR(h.r);
return s;
}
public String toString()
{ return toStringR(head); }
-----
public void insert(ITEM x)
{ KEY key = x.key();
if (head == null)
{ head = new Node(x); return; }
Node p = head, q = p;
while (q != null)
if (less(key, q.item.key()))
{ p = q; q = q.l; }
else { p = q; q = q.r; }
if (less(key, p.item.key()))
p.l = new Node(x);
else p.r = new Node(x);
}
private ITEM selectR(Node h, int k)
{ if (h == null) return null;
int t = (h.l == null) ? 0 : h.l.N;
if (t > k) return selectR(h.l, k);
if (t < k) return selectR(h.r, k-t-1);
return h.item;
}
ITEM select(int k)
{ return selectR(head, k); }
private Node removeR(Node h, KEY v)
{ if (h == null) return null;
KEY w = h.item.key();
if (less(v, w)) removeR(h.l, v);
if (less(w, v)) removeR(h.r, v);
if (equals(v, w)) h = joinLR(h.l, h.r);
return h;
}
void remove(KEY v)
{ removeR(head, v); }
-----
CHAPTER 13. Balanced Trees
private Node insertR(Node h, ITEM x)
{ if (h == null) return new Node(x);
if (Math.random()*h.N < 1.0)
return insertT(h, x);
if (less(x.key(), h.item.key()))
h.l = insertR(h.l, x);
else h.r = insertR(h.r, x);
h.N++;
return h;
}
void insert(ITEM x)
{ head = insertR(head, x); }
private void removeR(Node t, KEY v, int k)
{ Node x = t.next[k];
if (!less(x.item.key(), v))
{
if (equals(v, x.item.key()))
{ t.next[k] = x.next[k]; }
if (k == 0) return;
removeR(t, v, k-1); return;
}
removeR(t.next[k], v, k);
}
void remove(ITEM x)
{ removeR(head, x.key(), lgN); N--; }

----------
CHAPTER 2. Principles of Algorithm Analysis
-----
static int search(int a[], int v, int l, int r)
{ int i;
for (i = l; i <= r; i++)
if (v == a[i]) return i;
return -1;
}
-----
static int search(int a[], int v, int l, int r)
{
while (r >= l)
{ int m = (l+r)/2;
if (v == a[m]) return m;
if (v < a[m]) r = m-1; else l = m+1;
}
return -1;
}

----------
CHAPTER 3. Elementary Data Structures
-----
class LogTable
{
static int lg(int N)
{ int i = 0;
while (N > 0) { i++; N/= 2; }
return i;
}
public static void main(String[] args)
{
for (int N = 1000; N <= 1000000000; N *= 10)
Out.println(lg(N) + " " + N);
}
}
-----
class Point
{ double x, y;
Point()
{ x = Math.random(); y = Math.random(); }
Point(double x, double y)
{ this.x = x; this.y = y; }
double r()
{ return Math.sqrt(x*x + y*y); }
double theta()
{ return Math.atan2(y, x); }
double distance(Point p)
{ double dx = x - p.x, dy = y - p.y;
return Math.sqrt(dx*dx + dy*dy);
}
public String toString()
{ return "(" + x + ", " + y + ")"; }
}
-----
-----
class CircularList
{
static class Node
{ int val; Node next;
Node(int v) { val = v; }
}
Node next(Node x)
{ return x.next; }
int val(Node x)
{ return x.val; }
Node insert(Node x, int v)
{ Node t = new Node(v);
if (x == null) t.next = t;
else { t.next = x.next; x.next = t; }
return t;
}
void remove(Node x)
{ x.next = x.next.next; }
}
-----
class CircularList
{
static class Node
{ int val; int next; }
static Node M[];
static int free, max = 10000;
CircularList()
{
M = new Node[max+1];
for (int i = 0; i < max; i++)
{ M[i] = new Node(); M[i].next = i+1; }
M[max] = new Node(); M[max].next = 0;
free = 0;
}
Node next(Node x)
{ return M[x.next]; }
int val(Node x)
{ return x.val; }
Node insert(Node x, int v)
{
int i = free; free = M[free].next;
M[i].val = v;
if (x == null) M[i].next = i;
else { M[i].next = x.next; x.next = i; }
return M[i];
}
void remove(Node x)
{ int i = x.next; x.next = M[i].next;
M[i].next = free; free = i;
}
}
-----
class intStack // ADT interface
{ // implementations and private members hidden
intStack(int)
int empty()
void push(int)
int pop()
}
-----
class Postfix
{
public static void main(String[] args)
{ char[] a = args[0].toCharArray();
int N = a.length;
intStack s = new intStack(N);
for (int i = 0; i < N; i++)
{
if (a[i] == '+')
s.push(s.pop() + s.pop());
if (a[i] == '*')
s.push(s.pop() * s.pop());
if ((a[i] >= '0') && (a[i] <= '9'))
s.push(0);
while((a[i] >= '0') && (a[i] <= '9'))
s.push(10*s.pop() + (a[i++]-'0'));
}
Out.println(s.pop() + "");
}
}
-----
class InfixToPostfix
{
public static void main(String[] args)
{ char[] a = args[0].toCharArray();
int N = a.length;
charStack s = new charStack(N);
for (int i = 0; i < N; i++)
{
if (a[i] == ')')
Out.print(s.pop() + " ");
if ((a[i] == '+') || (a[i] == '*'))
s.push(a[i]);
if ((a[i] >= '0') && (a[i] <= '9'))
Out.print(a[i] + " ");
}
Out.println("");
}
}
-----
class intStack
{
private int[] s;
private int N;
intStack(int maxN)
{ s = new int[maxN]; N = 0; }
boolean isEmpty()
{ return (N == 0); }
void push(int item)
{ s[N++] = item; }
int pop()
{ return s[--N]; }
}
-----
class intStack
{
private Node head;
private class Node
{
int item; Node next;
Node(int item, Node next)
{ this.item = item; this.next = next; }
}
intStack(int maxN)
{ head = null; }
boolean isEmpty()
{ return (head == null); }
void push(int item)
{ head = new Node(item, head); }
int pop()
{ int v = head.item; Node t = head.next;
head = t; return v; }
}
-----
class Stack
{
private Object[] s;
private int N;
Stack(int maxN)
{ s = new Object[maxN]; N = 0; }
boolean isEmpty()
{ return (N == 0); }
void push(Object item)
{ s[N++] = item; }
Object pop()
{ Object t = s[--N]; s[N] = null; return t; }
}
-----
class intStack
{
private Stack S;
intStack(int maxN)
{ S = new Stack(maxN); }
boolean isEmpty()
{ return S.isEmpty(); }
void push(int item)
{ S.push(new Integer(item)); }
int pop()
{ return ((Integer) S.pop()).intValue(); }
}
-----
-----
class intStack
{
private int[] s;
private boolean[] t;
private int N;
intStack(int maxN)
{ s = new int[maxN]; N = 0;
t = new boolean[maxN];
for (int i = 0; i < maxN; i++) t[i] = false;
}
boolean empty()
{ return N == 0; }
public void push(int item)
{ if (t[item]) return;
s[N++] = item;
t[item] = true;
}
public int pop()
{ t[s[--N]] = false; return s[N]; }
}
-----

Вам также может понравиться