本文整理汇总了Java中edu.princeton.cs.algs4.StdIn类的典型用法代码示例。如果您正苦于以下问题:Java StdIn类的具体用法?Java StdIn怎么用?Java StdIn使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StdIn类属于edu.princeton.cs.algs4包,在下文中一共展示了StdIn类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
public static void main(String[] args) {
ResizingArrayStack<String> stack = new ResizingArrayStack<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) stack.push(item);
else if (stack.isEmpty()) StdOut.println("BAD INPUT");
else StdOut.print(stack.pop() + " ");
}
StdOut.println();
// print what's left on the stack
StdOut.print("Left on stack: ");
for (String s : stack) {
StdOut.print(s + " ");
}
StdOut.println();
}
示例2: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
public static void main(String[] args) {
ResizingArrayQueue<String> q = new ResizingArrayQueue<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) q.enqueue(item);
else if (q.isEmpty()) StdOut.println("BAD INPUT");
else StdOut.print(q.dequeue() + " ");
}
StdOut.println();
// print what's left on the stack
StdOut.print("Left on queue: ");
for (String s : q) {
StdOut.print(s + " ");
}
StdOut.println();
}
示例3: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
/**
* client program
* takes a command-line integer k; reads in a sequence of strings from standard input using StdIn.
* readString(); and prints exactly k of them, uniformly at random. Print each item from the sequence at most once.
*/
public static void main(String[] args) {
int k = Integer.parseInt(args[0]);
RandomizedQueue<String> queue = new RandomizedQueue<>();
while (!StdIn.isEmpty()) {
queue.enqueue(StdIn.readString());
}
int cnt = -1;
for (String s : queue) {
if (++cnt == k)
break;
StdOut.println(s);
}
}
示例4: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
long start = System.currentTimeMillis();
Quick3string.sort(a);
long end = System.currentTimeMillis();
System.out.println(end - start);
/*
long start = System.currentTimeMillis();
MSD.sort(a);
long end = System.currentTimeMillis();
System.out.println(end - start);
*/
}
示例5: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
public static void main(String[] args) {
In in = new In(args[0]);
int context = Integer.parseInt(args[1]);
String text = in.readAll().replaceAll("\\s+", " ");
int N = text.length();
SuffixArray sa = new SuffixArray(text);
while (StdIn.hasNextChar()) {
String q = StdIn.readLine();
for (int i = sa.rank(q); i < N && sa.select(i).startsWith(q); i++) {
int from = Math.max(0, sa.index(i) - context);
int to = Math.min(N-1, from + q.length() + 2*context);
StdOut.println(text.substring(from,to));
}
}
}
示例6: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
/**
* Unit tests the <tt>SymbolGraph</tt> data type.
*/
public static void main(String[] args) {
String filename = args[0];
String delimiter = args[1];
SymbolGraph sg = new SymbolGraph(filename, delimiter);
Graph G = sg.G();
while (StdIn.hasNextLine()) {
String source = StdIn.readLine();
if (sg.contains(source)) {
int s = sg.index(source);
for (int v : G.adj(s)) {
StdOut.println(" " + sg.name(v));
}
}
else {
StdOut.println("input not contain '" + source + "'");
}
}
}
示例7: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
public static void main(String[] args) {
int max = Integer.parseInt(args[0]);
FixedCapacityStackOfStrings stack = new FixedCapacityStackOfStrings(max);
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) stack.push(item);
else if (stack.isEmpty()) StdOut.println("BAD INPUT");
else StdOut.print(stack.pop() + " ");
}
StdOut.println();
// print what's left on the stack
StdOut.print("Left on stack: ");
for (String s : stack) {
StdOut.print(s + " ");
}
StdOut.println();
}
示例8: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
public static void main(String[] args) {
Deque<String> op = new LinkedList<String>();
Deque<Double> val = new LinkedList<Double>();
while (!StdIn.isEmpty()) {
String s = StdIn.readString();
if (s.equals("(")) ;
else if (s.equals("+")) op.addFirst("+");
else if (s.equals("*")) op.addFirst("*");
else if (s.equals("-")) op.addFirst("-");
else if (s.equals("/")) op.addFirst("/");
else if (s.equals(")")) {
String operator = op.removeFirst();
Double n1 = val.removeFirst();
Double n2 = val.removeFirst();
if (operator.equals("+")) val.addFirst(n1 + n2);
else if (operator.equals("*")) val.addFirst(n1 * n2);
else if (operator.equals("-")) val.addFirst(n2 - n1);
else if (operator.equals("/")) val.addFirst(n2 / n1);
}
else val.addFirst(Double.parseDouble(s));
}
System.out.println(val.removeFirst());
}
示例9: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
public static void main(String[] args) {
int max = Integer.parseInt(args[0]);
FixedCapacityStack<String> stack = new FixedCapacityStack<String>(max);
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) stack.push(item);
else if (stack.isEmpty()) StdOut.println("BAD INPUT");
else StdOut.print(stack.pop() + " ");
}
StdOut.println();
// print what's left on the stack
StdOut.print("Left on stack: ");
for (String s : stack) {
StdOut.print(s + " ");
}
StdOut.println();
}
示例10: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
/**
* Reads in a sequence of strings from standard input; insertion sorts them;
* and prints them to standard output in ascending order.
*/
public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
Insertion.sort(a);
//Insertion.sort(null); //not handled
show(a);
}
示例11: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
public static void main(String[] args) {
BinarySearchSymbolTable<String, Double> scoresMap = new BinarySearchSymbolTable<>(2);
scoresMap.put("A+", 4.33);
scoresMap.put("A", 4.00);
scoresMap.put("A-", 3.67);
scoresMap.put("B+", 3.33);
scoresMap.put("B", 3.00);
scoresMap.put("B-", 2.67);
scoresMap.put("C+", 2.33);
scoresMap.put("C", 2.00);
scoresMap.put("C-", 1.67);
scoresMap.put("D", 1.00);
scoresMap.put("F", 0.00);
String[] grades = StdIn.readAllLines();
double gpa = new Exercise1().computeGPA(scoresMap, grades);
StdOut.printf("GPA: %.2f", gpa);
}
示例12: readInputAndBuildConcordance
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
private Map<String, List<Integer>> readInputAndBuildConcordance() {
int wordIndex = 0;
Map<String, List<Integer>> concordanceMap = new HashMap<>();
while (StdIn.hasNextLine()) {
String wordLine = StdIn.readLine();
String[] words = wordLine.split(" ");
for(String word : words) {
if(!concordanceMap.containsKey(word)) {
concordanceMap.put(word, new ArrayList<>());
}
concordanceMap.get(word).add(wordIndex);
wordIndex++;
}
}
return concordanceMap;
}
示例13: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
public static void main (String[] args) {
Exercise40_MoveToFront<Character> moveToFront = new Exercise40_MoveToFront<>();
while (StdIn.hasNextChar()){
moveToFront.insert(StdIn.readChar());
}
// Test data
// moveToFront.insert('a');
// moveToFront.insert('b');
// moveToFront.insert('c');
// moveToFront.insert('d');
// moveToFront.insert('c');
// moveToFront.insert('d');
// moveToFront.insert('z');
for (char character : moveToFront) {
StdOut.println(character);
}
}
示例14: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
public static void main(String[] args) {
int k = Integer.parseInt(args[0]);
RandomizedQueue<String> queue = new RandomizedQueue<>();
while (!StdIn.isEmpty()) {
queue.enqueue(StdIn.readString());
}
Iterator<String> iterator = queue.iterator();
for (int i = 0; iterator.hasNext() && i < k; i++) {
StdOut.println(iterator.next());
}
}
示例15: main
import edu.princeton.cs.algs4.StdIn; //导入依赖的package包/类
public static void main(String[] args) {
int M = Integer.parseInt(args[0]);
MinPQ<Transaction> pq = new MinPQ<>();
while (StdIn.hasNextLine()) {
String line = StdIn.readLine();
Transaction item = new Transaction(line);
pq.insert(item);
if (pq.size() > M) pq.delMin();
}
}