2016a
Материал из PTHSWiki
(Различия между версиями)
Antonk (обсуждение | вклад) (→Чтение массива из файла (BufferedReader - по одному числу в строке)) |
Antonk (обсуждение | вклад) |
||
(не показаны 12 промежуточных версий 1 участника) | |||
Строка 1: | Строка 1: | ||
+ | == Бинарные файлы == | ||
+ | <source lang="java"> | ||
+ | import java.io.IOException; | ||
+ | import java.nio.file.Files; | ||
+ | import java.nio.file.Path; | ||
+ | import java.nio.file.Paths; | ||
+ | import java.util.ArrayList; | ||
+ | import java.util.Arrays; | ||
+ | import java.util.Random; | ||
+ | |||
+ | public class Main { | ||
+ | |||
+ | public static void main(String[] args) throws IOException { | ||
+ | byte[] arr = Files.readAllBytes(Paths.get("output.txt")); | ||
+ | for (int i = 0; i < arr.length; i++) { | ||
+ | System.out.println((arr[i]+256)%256); | ||
+ | } | ||
+ | |||
+ | int a = 209; | ||
+ | ArrayList<Byte> al = new ArrayList<>(); | ||
+ | al.add((byte) 1); | ||
+ | al.add((byte) 6); | ||
+ | al.add((byte) 87); | ||
+ | al.add((byte)(a - 256*(a/128))); | ||
+ | |||
+ | byte[] arr2 = new byte[al.size()]; | ||
+ | for (int i = 0; i < al.size(); i++) { | ||
+ | arr2[i] = al.get(i); | ||
+ | } | ||
+ | |||
+ | Files.write(Paths.get("output.txt"), arr2); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </source> | ||
+ | |||
+ | == Конь-2 == | ||
+ | |||
+ | <source lang="java"> | ||
+ | import java.util.ArrayList; | ||
+ | import java.util.LinkedList; | ||
+ | |||
+ | public class Main { | ||
+ | static int[] marked; | ||
+ | static int[] dist; | ||
+ | |||
+ | public static void bfs(int z) { | ||
+ | LinkedList<Integer> queue = new LinkedList<Integer>(); | ||
+ | |||
+ | for (int i = 0; i < dist.length; i++) { | ||
+ | dist[i] = -1; | ||
+ | } | ||
+ | |||
+ | marked[z] = 1; | ||
+ | dist[z] = 0; | ||
+ | queue.add(z); | ||
+ | int i; | ||
+ | |||
+ | while (queue.size() > 0) { | ||
+ | int x = queue.remove(); | ||
+ | if ((x%8 > 1) && (x-10 >= 0) && (marked[x-10] == 0)) { | ||
+ | i = x-10; | ||
+ | marked[i] = 1; | ||
+ | dist[i] = dist[x] + 1; | ||
+ | queue.add(i); | ||
+ | } | ||
+ | if ((x%8 > 1) && (x+6 <= 63) && (marked[x+6] == 0)) { | ||
+ | i = x+6; | ||
+ | marked[i] = 1; | ||
+ | dist[i] = dist[x] + 1; | ||
+ | queue.add(i); | ||
+ | } | ||
+ | if ((x%8 < 6) && (x+10 <= 63) && (marked[x+10] == 0)) { | ||
+ | i = x+10; | ||
+ | marked[i] = 1; | ||
+ | dist[i] = dist[x] + 1; | ||
+ | queue.add(i); | ||
+ | } | ||
+ | if ((x%8 < 6) && (x-6 >= 0) && (marked[x-6] == 0)) { | ||
+ | i = x-6; | ||
+ | marked[i] = 1; | ||
+ | dist[i] = dist[x] + 1; | ||
+ | queue.add(i); | ||
+ | } | ||
+ | |||
+ | if ((x%8 > 0) && (x-17 >= 0) && (marked[x-17] == 0)) { | ||
+ | i = x-17; | ||
+ | marked[i] = 1; | ||
+ | dist[i] = dist[x] + 1; | ||
+ | queue.add(i); | ||
+ | } | ||
+ | if ((x%8 < 7) && (x-15 >= 0) && (marked[x-15] == 0)) { | ||
+ | i = x-15; | ||
+ | marked[i] = 1; | ||
+ | dist[i] = dist[x] + 1; | ||
+ | queue.add(i); | ||
+ | } | ||
+ | if ((x%8 < 7) && (x+17 <= 63) && (marked[x+17] == 0)) { | ||
+ | i = x+17; | ||
+ | marked[i] = 1; | ||
+ | dist[i] = dist[x] + 1; | ||
+ | queue.add(i); | ||
+ | } | ||
+ | if ((x%8 > 0) && (x+15 <= 63) && (marked[x+15] == 0)) { | ||
+ | i = x+15; | ||
+ | marked[i] = 1; | ||
+ | dist[i] = dist[x] + 1; | ||
+ | queue.add(i); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == Конь-3 == | ||
+ | |||
+ | <source lang="java"> | ||
+ | import java.util.ArrayList; | ||
+ | import java.util.LinkedList; | ||
+ | |||
+ | public class Main3 { | ||
+ | static int[] marked; | ||
+ | |||
+ | public static void bfs(int z) { | ||
+ | LinkedList<Integer> queue = new LinkedList<Integer>(); | ||
+ | |||
+ | marked[z] = 1; | ||
+ | queue.add(z); | ||
+ | int i; | ||
+ | |||
+ | while (queue.size() > 0) { | ||
+ | int x = queue.remove(); | ||
+ | |||
+ | for (i = 0; i <= 63; i++) { | ||
+ | if ((Math.abs((x%8-i%8)*(x/8-i/8)) == 2) && (marked[i] == 0)) { | ||
+ | marked[i] = marked[x]+1; | ||
+ | queue.add(i); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </source> | ||
+ | |||
+ | == DFS + BFS == | ||
+ | |||
+ | <source lang="java"> | ||
+ | import java.io.File; | ||
+ | import java.io.FileNotFoundException; | ||
+ | import java.util.LinkedList; | ||
+ | import java.util.Scanner; | ||
+ | |||
+ | public class Main { | ||
+ | static int arr[][]; | ||
+ | static int V; | ||
+ | static int marked[]; | ||
+ | |||
+ | public static void dfs(int z, int k) { | ||
+ | marked[z] = k; | ||
+ | for (int i = 0; i < V; i++) { | ||
+ | if ((arr[z][i] == 1) && (marked[i] == 0)) { | ||
+ | dfs(i, k); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public static void bfs(int z) { | ||
+ | marked[z] = 1; | ||
+ | |||
+ | LinkedList<Integer> queue = new LinkedList<Integer>(); | ||
+ | queue.add(z); | ||
+ | |||
+ | while (queue.size() > 0) { | ||
+ | int x = queue.remove(); | ||
+ | for (int i = 0; i < V; i++) { | ||
+ | if ((arr[x][i] == 1) && (marked[i] == 0)) { | ||
+ | queue.add(i); | ||
+ | marked[i] = 1; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) throws FileNotFoundException { | ||
+ | Scanner sc = new Scanner(new File("input.txt")); | ||
+ | |||
+ | V = sc.nextInt(); | ||
+ | |||
+ | arr = new int[V][V]; | ||
+ | |||
+ | for (int i = 0; i < V; i++) { | ||
+ | for (int j = 0; j < V; j++) { | ||
+ | arr[i][j] = sc.nextInt(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | marked = new int[V]; | ||
+ | |||
+ | int z = 0; | ||
+ | |||
+ | bfs(z); | ||
+ | |||
+ | for (int i = 0; i < V; i++) { | ||
+ | System.out.println(marked[i]); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </source> | ||
+ | |||
+ | |||
+ | == Java == | ||
+ | |||
+ | Этот кусочек кода считывает файл построчно и выводит сумму числе в каждой строке | ||
+ | <source lang="java"> | ||
+ | while (sc.hasNext()) { | ||
+ | String numberString = sc.nextLine(); | ||
+ | int summa = 0; | ||
+ | for (String s : numberString.split(" ")) { | ||
+ | summa += Integer.parseInt(s); | ||
+ | } | ||
+ | System.out.println(summa); | ||
+ | }</source> | ||
+ | |||
+ | == Java. Список смежности == | ||
+ | |||
+ | <source lang="java"> | ||
+ | import java.util.ArrayList; | ||
+ | import java.util.Collections; | ||
+ | |||
+ | |||
+ | public class Main { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | // int[] arr = new int[10]; | ||
+ | // arr[i] = 7 | ||
+ | // x = 2*arr[i] | ||
+ | // добавить в конец массива число 10 | ||
+ | |||
+ | // ArrayList<Integer> arr = new ArrayList<Integer>(); | ||
+ | // arr.set(i, 7); | ||
+ | // x = 2 * arr.get(i); | ||
+ | // arr.add(10); | ||
+ | |||
+ | // создать пустой список смежности | ||
+ | ArrayList< ArrayList<Integer> > arr = new ArrayList< ArrayList<Integer> >(); | ||
+ | int V = 20; | ||
+ | for (int i = 0; i < V; i++) { | ||
+ | ArrayList<Integer> tmp = new ArrayList<Integer>(); | ||
+ | arr.add(tmp); | ||
+ | } | ||
+ | |||
+ | // добавили ребро 3-5 | ||
+ | arr.get(3).add(5); | ||
+ | arr.get(5).add(3); | ||
+ | |||
+ | // сортировать третью строку | ||
+ | Collections.sort(arr.get(3)); | ||
+ | |||
+ | // вывести все вершины, с которыми связана третья | ||
+ | int a = 3; | ||
+ | for (int i = 0; i < arr.get(a).size(); i++) { | ||
+ | System.out.println(arr.get(a).get(i)); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </source> | ||
+ | |||
+ | == Java Swing. ActionListener == | ||
+ | |||
+ | <source lang="java"> | ||
+ | class Window3 extends JFrame { | ||
+ | int num1; | ||
+ | char operation; | ||
+ | Window3() { | ||
+ | super("Ы:)"); | ||
+ | operation = 'E'; | ||
+ | setSize(700, 666); | ||
+ | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
+ | |||
+ | Box b = Box.createVerticalBox(); | ||
+ | |||
+ | final JButton b1 = new JButton("+"); | ||
+ | b.add(b1); | ||
+ | final JButton b2 = new JButton("-"); | ||
+ | b.add(b2); | ||
+ | final JButton b3 = new JButton("="); | ||
+ | b.add(b3); | ||
+ | |||
+ | final JTextArea tf = new JTextArea(); | ||
+ | b.add(tf); | ||
+ | tf.setText("0"); | ||
+ | |||
+ | ActionListener al = new ActionListener() { | ||
+ | @Override | ||
+ | public void actionPerformed(ActionEvent arg0) { | ||
+ | if (operation != 'E') { | ||
+ | b3.doClick(); | ||
+ | } | ||
+ | |||
+ | num1 = Integer.parseInt(tf.getText()); | ||
+ | tf.setText(""); | ||
+ | operation = ((JButton)(arg0.getSource())).getText().charAt(0); | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | b1.addActionListener(al); | ||
+ | b2.addActionListener(al); | ||
+ | |||
+ | b3.addActionListener(new ActionListener() { | ||
+ | @Override | ||
+ | public void actionPerformed(ActionEvent e) { | ||
+ | int num2 = Integer.parseInt(tf.getText()); | ||
+ | |||
+ | switch (operation) { | ||
+ | case '+': | ||
+ | tf.setText(Integer.toString(num1+num2)); | ||
+ | break; | ||
+ | case '-': | ||
+ | tf.setText(Integer.toString(num1-num2)); | ||
+ | break; | ||
+ | // default: | ||
+ | // tf.setText(""); | ||
+ | } | ||
+ | |||
+ | operation = 'E'; | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | setContentPane(b); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == Java Swing. JTabbedPane == | ||
+ | |||
+ | <source lang="java"> | ||
+ | Box b = Box.createVerticalBox(); | ||
+ | |||
+ | JTabbedPane tp = new JTabbedPane(); | ||
+ | JPanel panel1 = new JPanel(); | ||
+ | panel1.setLayout(new FlowLayout()); | ||
+ | panel1.add(new JButton("1111111")); | ||
+ | JTextArea ta = new JTextArea(20, 20); | ||
+ | panel1.add(ta); | ||
+ | panel1.add(new JScrollPane(ta)); | ||
+ | tp.addTab("Заголовок", panel1); | ||
+ | JPanel panel2 = new JPanel(); | ||
+ | panel2.setLayout(new FlowLayout()); | ||
+ | panel2.add(new JButton("2222222")); | ||
+ | panel2.add(new JTextArea(20, 20)); | ||
+ | tp.addTab("Заголовок2", panel2); | ||
+ | |||
+ | b.add(tp); | ||
+ | </source> | ||
+ | |||
+ | == Java Swing. JFileChooser == | ||
+ | |||
+ | <source lang="java"> | ||
+ | import java.awt.GridLayout; | ||
+ | import java.awt.event.ActionEvent; | ||
+ | import java.awt.event.ActionListener; | ||
+ | import java.io.File; | ||
+ | import java.io.FileNotFoundException; | ||
+ | import java.util.Scanner; | ||
+ | |||
+ | import javax.swing.*; | ||
+ | |||
+ | class Window2 extends JFrame { | ||
+ | |||
+ | |||
+ | Window2() { | ||
+ | super("Ы)"); | ||
+ | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
+ | setSize(300, 400); | ||
+ | |||
+ | Box b = Box.createVerticalBox(); | ||
+ | |||
+ | JButton b1 = new JButton("+"); | ||
+ | b.add(b1); | ||
+ | JButton b2 = new JButton("Exit"); | ||
+ | b.add(b2); | ||
+ | |||
+ | final JTextArea tf = new JTextArea(); | ||
+ | b.add(tf); | ||
+ | |||
+ | b1.addActionListener(new ActionListener() { | ||
+ | |||
+ | @Override | ||
+ | public void actionPerformed(ActionEvent e) { | ||
+ | JFileChooser opener = new JFileChooser(); | ||
+ | int res = opener.showOpenDialog(getParent()); | ||
+ | if (res == JFileChooser.APPROVE_OPTION) { | ||
+ | File f = opener.getSelectedFile(); | ||
+ | try { | ||
+ | Scanner s = new Scanner(f); | ||
+ | while (s.hasNext()) { | ||
+ | String str = s.nextLine(); | ||
+ | tf.append(str+"\n"); | ||
+ | } | ||
+ | } catch (FileNotFoundException e1) { | ||
+ | // TODO Auto-generated catch block | ||
+ | e1.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | b2.addActionListener(new ActionListener() { | ||
+ | |||
+ | @Override | ||
+ | public void actionPerformed(ActionEvent arg0) { | ||
+ | System.exit(0); | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | setContentPane(b); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public class Main { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | JFrame w = new Window2(); | ||
+ | w.setVisible(true); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == Java Swing == | ||
+ | |||
+ | <source lang="java"> | ||
+ | import java.awt.BorderLayout; | ||
+ | import java.awt.FlowLayout; | ||
+ | import java.awt.event.ActionEvent; | ||
+ | import java.awt.event.ActionListener; | ||
+ | import java.awt.event.MouseAdapter; | ||
+ | import java.awt.event.MouseEvent; | ||
+ | import java.util.Random; | ||
+ | |||
+ | import javax.swing.*; | ||
+ | |||
+ | class TestWindow extends JFrame { | ||
+ | TestWindow() { | ||
+ | super("Заголовок окна"); | ||
+ | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
+ | setSize(300, 400); | ||
+ | |||
+ | // JPanel panel = new JPanel(); | ||
+ | // panel.setLayout(new BorderLayout()); | ||
+ | // | ||
+ | // JPanel upPanel = new JPanel(); | ||
+ | // upPanel.setLayout(new FlowLayout()); | ||
+ | // JButton b1 = new JButton("1"); | ||
+ | // upPanel.add(b1); | ||
+ | // JButton b2 = new JButton("2"); | ||
+ | // upPanel.add(b2); | ||
+ | // | ||
+ | // JPanel downPanel = new JPanel(); | ||
+ | // downPanel.setLayout(new FlowLayout()); | ||
+ | // JButton b3 = new JButton("3"); | ||
+ | // downPanel.add(b3); | ||
+ | // JButton b4 = new JButton("4"); | ||
+ | // downPanel.add(b4); | ||
+ | // | ||
+ | // panel.add(upPanel, BorderLayout.NORTH); | ||
+ | // panel.add(downPanel, BorderLayout.SOUTH); | ||
+ | // setContentPane(panel); | ||
+ | |||
+ | // Box box = Box.createVerticalBox(); | ||
+ | // JButton b1 = new JButton("1"); | ||
+ | // JButton b2 = new JButton("2"); | ||
+ | // JButton b3 = new JButton("3"); | ||
+ | // JButton b4 = new JButton("4"); | ||
+ | // box.add(b1); | ||
+ | // box.add(Box.createVerticalStrut(20)); | ||
+ | // box.add(b2); | ||
+ | // box.add(Box.createVerticalGlue()); | ||
+ | // box.add(b3); | ||
+ | // box.add(b4); | ||
+ | // setContentPane(box); | ||
+ | |||
+ | final JPanel panel = new JPanel(); | ||
+ | panel.setLayout(null); | ||
+ | final JButton b1 = new JButton("1"); | ||
+ | b1.setSize(100, 57); | ||
+ | b1.setLocation(100, 87); | ||
+ | |||
+ | b1.addActionListener(new ActionListener() { | ||
+ | public void actionPerformed(ActionEvent e) { | ||
+ | Random r = new Random(); | ||
+ | b1.setLocation(r.nextInt(panel.getWidth()), r.nextInt(panel.getHeight())); | ||
+ | int a = Integer.parseInt(b1.getText()); | ||
+ | |||
+ | b1.setText(Integer.toString(a+1)); | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | b1.addMouseListener(new MouseAdapter() { | ||
+ | public void mouseEntered(MouseEvent e) { | ||
+ | Random r = new Random(); | ||
+ | b1.setLocation(r.nextInt(panel.getWidth()), r.nextInt(panel.getHeight())); | ||
+ | int a = Integer.parseInt(b1.getText()); | ||
+ | |||
+ | b1.setText(Integer.toString(a+1)); | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | panel.add(b1); | ||
+ | setContentPane(panel); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public class Main { | ||
+ | public static void main(String[] args) { | ||
+ | JFrame window = new TestWindow(); | ||
+ | window.setVisible(true); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | |||
+ | == Задание по отладке == | ||
+ | |||
+ | <source lang="java"> | ||
+ | public class Errors { | ||
+ | public static void main(String[] args) { | ||
+ | int arr[] = new int[20]; | ||
+ | |||
+ | for (int i = 0; i <= 20; i++) { | ||
+ | arr[i] = i*i; | ||
+ | } | ||
+ | |||
+ | int sum = 0; | ||
+ | for (int i = 0; i <= 20; i++) { | ||
+ | if (i % 2 == 0) { | ||
+ | sum = sum + arr[i]; | ||
+ | } else { | ||
+ | sum = sum - arr[i]; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | System.out.println(sum); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </source> | ||
+ | |||
== Чтение массива из файла (BufferedReader - по одному числу в строке) == | == Чтение массива из файла (BufferedReader - по одному числу в строке) == | ||
Строка 181: | Строка 736: | ||
} | } | ||
</pre> | </pre> | ||
+ | |||
+ | == Создание собственных классов == | ||
+ | <syntaxhighlight lang="java"> | ||
+ | import java.util.Scanner; | ||
+ | |||
+ | public class Main { | ||
+ | |||
+ | public static class Person { | ||
+ | public String name; | ||
+ | public int ege; | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | Scanner sc = new Scanner(System.in); | ||
+ | |||
+ | Person arr[] = new Person[2]; | ||
+ | |||
+ | for (int i = 0; i < arr.length; i++) { | ||
+ | arr[i] = new Person(); | ||
+ | arr[i].name = sc.next(); | ||
+ | arr[i].ege = sc.nextInt(); | ||
+ | } | ||
+ | |||
+ | for (int i = 0; i < arr.length; i++) { | ||
+ | System.out.println(arr[i].ege + " " + arr[i].name); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | == ArrayList и чтение до конца файла == | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | import java.io.File; | ||
+ | import java.io.FileNotFoundException; | ||
+ | import java.util.ArrayList; | ||
+ | import java.util.Scanner; | ||
+ | |||
+ | public class Main { | ||
+ | public static void main(String[] args) throws FileNotFoundException { | ||
+ | Scanner sc = new Scanner(new File("input.txt")); | ||
+ | |||
+ | ArrayList<Point> al = new ArrayList<Point>(); | ||
+ | |||
+ | while (sc.hasNext()) { | ||
+ | int a = sc.nextInt(); | ||
+ | int b = sc.nextInt(); | ||
+ | |||
+ | Point p = new Point(a, b); | ||
+ | al.add(p); | ||
+ | } | ||
+ | |||
+ | for (int i = 0; i < al.size(); i++) { | ||
+ | al.get(i).print(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> |
Текущая версия на 11:29, 21 января 2016
Бинарные файлы
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class Main {
public static void main(String[] args) throws IOException {
byte[] arr = Files.readAllBytes(Paths.get("output.txt"));
for (int i = 0; i < arr.length; i++) {
System.out.println((arr[i]+256)%256);
}
int a = 209;
ArrayList<Byte> al = new ArrayList<>();
al.add((byte) 1);
al.add((byte) 6);
al.add((byte) 87);
al.add((byte)(a - 256*(a/128)));
byte[] arr2 = new byte[al.size()];
for (int i = 0; i < al.size(); i++) {
arr2[i] = al.get(i);
}
Files.write(Paths.get("output.txt"), arr2);
}
}
Конь-2
import java.util.ArrayList;
import java.util.LinkedList;
public class Main {
static int[] marked;
static int[] dist;
public static void bfs(int z) {
LinkedList<Integer> queue = new LinkedList<Integer>();
for (int i = 0; i < dist.length; i++) {
dist[i] = -1;
}
marked[z] = 1;
dist[z] = 0;
queue.add(z);
int i;
while (queue.size() > 0) {
int x = queue.remove();
if ((x%8 > 1) && (x-10 >= 0) && (marked[x-10] == 0)) {
i = x-10;
marked[i] = 1;
dist[i] = dist[x] + 1;
queue.add(i);
}
if ((x%8 > 1) && (x+6 <= 63) && (marked[x+6] == 0)) {
i = x+6;
marked[i] = 1;
dist[i] = dist[x] + 1;
queue.add(i);
}
if ((x%8 < 6) && (x+10 <= 63) && (marked[x+10] == 0)) {
i = x+10;
marked[i] = 1;
dist[i] = dist[x] + 1;
queue.add(i);
}
if ((x%8 < 6) && (x-6 >= 0) && (marked[x-6] == 0)) {
i = x-6;
marked[i] = 1;
dist[i] = dist[x] + 1;
queue.add(i);
}
if ((x%8 > 0) && (x-17 >= 0) && (marked[x-17] == 0)) {
i = x-17;
marked[i] = 1;
dist[i] = dist[x] + 1;
queue.add(i);
}
if ((x%8 < 7) && (x-15 >= 0) && (marked[x-15] == 0)) {
i = x-15;
marked[i] = 1;
dist[i] = dist[x] + 1;
queue.add(i);
}
if ((x%8 < 7) && (x+17 <= 63) && (marked[x+17] == 0)) {
i = x+17;
marked[i] = 1;
dist[i] = dist[x] + 1;
queue.add(i);
}
if ((x%8 > 0) && (x+15 <= 63) && (marked[x+15] == 0)) {
i = x+15;
marked[i] = 1;
dist[i] = dist[x] + 1;
queue.add(i);
}
}
}
}
Конь-3
import java.util.ArrayList;
import java.util.LinkedList;
public class Main3 {
static int[] marked;
public static void bfs(int z) {
LinkedList<Integer> queue = new LinkedList<Integer>();
marked[z] = 1;
queue.add(z);
int i;
while (queue.size() > 0) {
int x = queue.remove();
for (i = 0; i <= 63; i++) {
if ((Math.abs((x%8-i%8)*(x/8-i/8)) == 2) && (marked[i] == 0)) {
marked[i] = marked[x]+1;
queue.add(i);
}
}
}
}
}
DFS + BFS
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
static int arr[][];
static int V;
static int marked[];
public static void dfs(int z, int k) {
marked[z] = k;
for (int i = 0; i < V; i++) {
if ((arr[z][i] == 1) && (marked[i] == 0)) {
dfs(i, k);
}
}
}
public static void bfs(int z) {
marked[z] = 1;
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.add(z);
while (queue.size() > 0) {
int x = queue.remove();
for (int i = 0; i < V; i++) {
if ((arr[x][i] == 1) && (marked[i] == 0)) {
queue.add(i);
marked[i] = 1;
}
}
}
}
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("input.txt"));
V = sc.nextInt();
arr = new int[V][V];
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
arr[i][j] = sc.nextInt();
}
}
marked = new int[V];
int z = 0;
bfs(z);
for (int i = 0; i < V; i++) {
System.out.println(marked[i]);
}
}
}
Java
Этот кусочек кода считывает файл построчно и выводит сумму числе в каждой строке
while (sc.hasNext()) {
String numberString = sc.nextLine();
int summa = 0;
for (String s : numberString.split(" ")) {
summa += Integer.parseInt(s);
}
System.out.println(summa);
}
Java. Список смежности
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
// int[] arr = new int[10];
// arr[i] = 7
// x = 2*arr[i]
// добавить в конец массива число 10
// ArrayList<Integer> arr = new ArrayList<Integer>();
// arr.set(i, 7);
// x = 2 * arr.get(i);
// arr.add(10);
// создать пустой список смежности
ArrayList< ArrayList<Integer> > arr = new ArrayList< ArrayList<Integer> >();
int V = 20;
for (int i = 0; i < V; i++) {
ArrayList<Integer> tmp = new ArrayList<Integer>();
arr.add(tmp);
}
// добавили ребро 3-5
arr.get(3).add(5);
arr.get(5).add(3);
// сортировать третью строку
Collections.sort(arr.get(3));
// вывести все вершины, с которыми связана третья
int a = 3;
for (int i = 0; i < arr.get(a).size(); i++) {
System.out.println(arr.get(a).get(i));
}
}
}
Java Swing. ActionListener
class Window3 extends JFrame {
int num1;
char operation;
Window3() {
super("Ы:)");
operation = 'E';
setSize(700, 666);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Box b = Box.createVerticalBox();
final JButton b1 = new JButton("+");
b.add(b1);
final JButton b2 = new JButton("-");
b.add(b2);
final JButton b3 = new JButton("=");
b.add(b3);
final JTextArea tf = new JTextArea();
b.add(tf);
tf.setText("0");
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (operation != 'E') {
b3.doClick();
}
num1 = Integer.parseInt(tf.getText());
tf.setText("");
operation = ((JButton)(arg0.getSource())).getText().charAt(0);
}
};
b1.addActionListener(al);
b2.addActionListener(al);
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int num2 = Integer.parseInt(tf.getText());
switch (operation) {
case '+':
tf.setText(Integer.toString(num1+num2));
break;
case '-':
tf.setText(Integer.toString(num1-num2));
break;
// default:
// tf.setText("");
}
operation = 'E';
}
});
setContentPane(b);
}
}
Java Swing. JTabbedPane
Box b = Box.createVerticalBox();
JTabbedPane tp = new JTabbedPane();
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(new JButton("1111111"));
JTextArea ta = new JTextArea(20, 20);
panel1.add(ta);
panel1.add(new JScrollPane(ta));
tp.addTab("Заголовок", panel1);
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(new JButton("2222222"));
panel2.add(new JTextArea(20, 20));
tp.addTab("Заголовок2", panel2);
b.add(tp);
Java Swing. JFileChooser
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.*;
class Window2 extends JFrame {
Window2() {
super("Ы)");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 400);
Box b = Box.createVerticalBox();
JButton b1 = new JButton("+");
b.add(b1);
JButton b2 = new JButton("Exit");
b.add(b2);
final JTextArea tf = new JTextArea();
b.add(tf);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser opener = new JFileChooser();
int res = opener.showOpenDialog(getParent());
if (res == JFileChooser.APPROVE_OPTION) {
File f = opener.getSelectedFile();
try {
Scanner s = new Scanner(f);
while (s.hasNext()) {
String str = s.nextLine();
tf.append(str+"\n");
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
setContentPane(b);
}
}
public class Main {
public static void main(String[] args) {
JFrame w = new Window2();
w.setVisible(true);
}
}
Java Swing
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.*;
class TestWindow extends JFrame {
TestWindow() {
super("Заголовок окна");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 400);
// JPanel panel = new JPanel();
// panel.setLayout(new BorderLayout());
//
// JPanel upPanel = new JPanel();
// upPanel.setLayout(new FlowLayout());
// JButton b1 = new JButton("1");
// upPanel.add(b1);
// JButton b2 = new JButton("2");
// upPanel.add(b2);
//
// JPanel downPanel = new JPanel();
// downPanel.setLayout(new FlowLayout());
// JButton b3 = new JButton("3");
// downPanel.add(b3);
// JButton b4 = new JButton("4");
// downPanel.add(b4);
//
// panel.add(upPanel, BorderLayout.NORTH);
// panel.add(downPanel, BorderLayout.SOUTH);
// setContentPane(panel);
// Box box = Box.createVerticalBox();
// JButton b1 = new JButton("1");
// JButton b2 = new JButton("2");
// JButton b3 = new JButton("3");
// JButton b4 = new JButton("4");
// box.add(b1);
// box.add(Box.createVerticalStrut(20));
// box.add(b2);
// box.add(Box.createVerticalGlue());
// box.add(b3);
// box.add(b4);
// setContentPane(box);
final JPanel panel = new JPanel();
panel.setLayout(null);
final JButton b1 = new JButton("1");
b1.setSize(100, 57);
b1.setLocation(100, 87);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random r = new Random();
b1.setLocation(r.nextInt(panel.getWidth()), r.nextInt(panel.getHeight()));
int a = Integer.parseInt(b1.getText());
b1.setText(Integer.toString(a+1));
}
});
b1.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
Random r = new Random();
b1.setLocation(r.nextInt(panel.getWidth()), r.nextInt(panel.getHeight()));
int a = Integer.parseInt(b1.getText());
b1.setText(Integer.toString(a+1));
}
});
panel.add(b1);
setContentPane(panel);
}
}
public class Main {
public static void main(String[] args) {
JFrame window = new TestWindow();
window.setVisible(true);
}
}
Задание по отладке
public class Errors {
public static void main(String[] args) {
int arr[] = new int[20];
for (int i = 0; i <= 20; i++) {
arr[i] = i*i;
}
int sum = 0;
for (int i = 0; i <= 20; i++) {
if (i % 2 == 0) {
sum = sum + arr[i];
} else {
sum = sum - arr[i];
}
}
System.out.println(sum);
}
}
Чтение массива из файла (BufferedReader - по одному числу в строке)
/*
* Читает из файла input.txt
* В первой строке число n - количество чисел.
* В следующих n строках по одному числу в строке
*
* Пример:
* 6
* 12
* 1
* 41
* 2
* 54
* 13
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
int n = Integer.parseInt(br.readLine());
int arr[] = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Чтение массива из файла (Scanner - можно числа в одной строке)
/* * Читает из файла input.txt * В первой строке число n - количество чисел. * Во второй - n чисел. * * Пример: * 6 * 12 1 41 2 54 13 */ import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Main2 { public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(new File("input.txt")); int n = sc.nextInt(); int arr[] = new int[n]; for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextInt(); } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
А+Б. Ввод из файла. Вывод в файл
import java.io.*; import java.util.*; public class aplusb { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("aplusb.in")); PrintWriter out = new PrintWriter(new File("aplusb.out")); int a = in.nextInt(); int b = in.nextInt(); out.println(a + b); in.close(); out.close(); } }
Ввод двух чисел в строку
Scanner sc=new Scanner(System.in); int num1=sc.nextInt(); int num2=sc.nextInt();
Ввод с клавиатуры
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { int a, b; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); a = Integer.parseInt(br.readLine()); b = Integer.parseInt(br.readLine()); System.out.println(a+b); } }
Функции
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { int a, b, c; double d; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); a = Integer.parseInt(br.readLine()); b = Integer.parseInt(br.readLine()); c = sum(a, b); d = mySqrt(b); System.out.println(c); System.out.println(d); } public static double mySqrt(int a) { return Math.sqrt(a); } public static int sum(int a, int b) { System.out.println("I really want to sleep"); return a+b; } public static int max2(int a, int b) { if (a > b) { return a; } else { return b; } } }
Создание собственных классов
import java.util.Scanner;
public class Main {
public static class Person {
public String name;
public int ege;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Person arr[] = new Person[2];
for (int i = 0; i < arr.length; i++) {
arr[i] = new Person();
arr[i].name = sc.next();
arr[i].ege = sc.nextInt();
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i].ege + " " + arr[i].name);
}
}
}
ArrayList и чтение до конца файла
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("input.txt"));
ArrayList<Point> al = new ArrayList<Point>();
while (sc.hasNext()) {
int a = sc.nextInt();
int b = sc.nextInt();
Point p = new Point(a, b);
al.add(p);
}
for (int i = 0; i < al.size(); i++) {
al.get(i).print();
}
}
}