|
Output.close();
}
}
Sequence
import java.util.*;
import java.io.*;
public class Sequence {
public static void main (String args []) throws Exception{
Scanner input = new Scanner(new File("input.txt"));
PrintWriter output = new PrintWriter(new File("output.txt"));
String line = input.nextLine();
String [] s = line.split("1");
int max = 0;
for(int i = 0; i < s.length; i++){
if(max < s[i].length()){
max = s[i].length();
}
}
Output.println(max);
Output.close();
}
}
Vasya & ewe kto-to
import java.util.*;
import java.io.*;
public class Vasya {
public static void main (String [] args) throws Exception {
Scanner input = new Scanner (new File("input.txt"));
PrintWriter output = new PrintWriter (new File("output.txt"));
long sum = input.nextLong();
long product = input.nextLong();
long a = 0, b = 0;
for (int i=-1000000; i<1000001; i++) {
if (i*(sum-i) == product) {
a=i;
b=sum-i;
}
}
output.println("a = "+a+" b= "+b);
Output.close();
}
}
Backtrakin’
Import java.util.Scanner;
public class BackTracking {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int []arr=new int[N];
int []arr2=new int[N];
for(int i = 0;i<N;i++){
arr[i]=in.nextInt();
}
Bf(arr,0,arr2);
}
public static void bf(int[] arr, int index, int []arr2) {
if (index == arr2.length) {
for(int i = 0;i<arr2.length;i++){
System.out.print(arr2[i]+" ");
}System.out.println();
Return;
}
for (int i = 0; i < arr.length; i++) {
arr2[index] = arr[i];
bf(arr, index + 1,arr2);
}
}
}
Increasin’ Array
Import java.util.Scanner;
Import java.util.Stack;
public class IncreasingArray {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
Stack<Integer> stack = new Stack<Integer>();
int[] array = new int[N];
for (int i = 0; i < N; i++) {
array[i] = in.nextInt();
}
for (int i = 0; i < array.length; i++) {
while (stack.size() > 0 && array[stack.peek()] < array[i]) {
int x = stack.pop();
array[x] = array[i];
}
Stack.push(i);
}
while (stack.size() > 0) {
int x = stack.pop();
array[x] = 0;
}
for (int i = 0; i < N; i++) {
System.out.print(array[i]+" ");
}
}
}
Merge sort
Import java.util.ArrayList;
Import java.util.Scanner;
public class MergeSort {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
ArrayList<Integer>arr = new ArrayList<Integer>();
for(int i = 0;i<N;i++){
Arr.add(in.nextInt());
}
System.out.println(mergesort(arr));
}
public static ArrayList<Integer> mergesort(ArrayList<Integer> m) {
if (m.size() <= 1) {
Return m;
}
ArrayList<Integer> left = new ArrayList<Integer>();
ArrayList<Integer> right = new ArrayList<Integer>();
ArrayList<Integer> result = new ArrayList<Integer>();
int middle = m.size() / 2;
for (int x = 0; x < middle; x++) {
Left.add(m.get(x));
}
for (int x = middle; x < m.size(); x++) {
Right.add(m.get(x));
}
left = mergesort(left);
right = mergesort(right);
result = merge(left, right);
Return result;
}
public static ArrayList<Integer> merge(ArrayList<Integer> left, ArrayList<Integer> right) {
ArrayList<Integer> result = new ArrayList<Integer>();
while (left.size() > 0 || right.size() > 0) {
if (left.size() > 0 && right.size() > 0) {
if (left.get(0) <= right.get(0)) {
Result.add(left.get(0));
Left.remove(0);
} else {
Result.add(right.get(0));
Right.remove(0);
}
} else if (left.size() > 0) {
Result.add(left.get(0));
Left.remove(0);
} else if (right.size() > 0) {
Result.add(right.get(0));
Right.remove(0);
}
}
Return result;
}
}
DFS
Дата добавления: 2015-10-30; просмотров: 119 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
FindPath(a, x, y-1); | | | Алгоритм Apriori |