|
Import java.util.ArrayList;
Import java.util.Scanner;
Import java.util.StringTokenizer;
import java.io.*;
public class ABC {
public static void main(String[] args) throws IOException {
ArrayList<String> arr1 = new ArrayList<String>();
ArrayList<String> arr2 = new ArrayList<String>();
Scanner in = new Scanner(System.in);
String dano = in.nextLine();
StringTokenizer str = new StringTokenizer(dano,"+=");
String A = str.nextToken();
String B = str.nextToken();
int C = Integer.parseInt(str.nextToken());
int counter = 0;
addWays("", A, arr1);
addWays("", B, arr2);
for(int i = 0;i<arr1.size();i++){
for(int j = 0;j<arr2.size();j++){
if(Integer.parseInt(arr1.get(i))+Integer.parseInt(arr2.get(j))==C){
System.out.println(arr1.get(i)+"+"+arr2.get(j));
counter++;
}
}
}
System.out.println(counter+" possible ways!");
}
public static void addWays(String temp, String word, ArrayList<String> arr) {
if (word.length() <= 1) {
arr.add(temp + word);
} else {
for (int i = 0; i < word.length(); i++) {
String cur = word.substring(i, i + 1);
String before = word.substring(0, i);
String after = word.substring(i + 1);
addWays(temp + cur, before + after, arr);
}
}
}
}
BUS
Import java.util.ArrayList;
Import java.util.Scanner;
Import java.util.StringTokenizer;
public class Bus {
static ArrayList<Character> charac = new ArrayList<Character>();
static ArrayList<String> strarr = new ArrayList<String>();
static ArrayList<String>str = new ArrayList<String>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = in.nextLine();
char[] str = new char[a.length() - 1];
for (int i = 0; i < str.length; i++) {
str[i] = a.charAt(i);
}
char[] op = { '+', '-', '/', '*', ' ' };
Bf(str, 0, op, a);
for (int i = 0; i < strarr.size(); i++) {
for(int j = 0;j<strarr.get(i).length();j++){
System.out.print(s)
}
}
}
public static void bf(char[] str, int index, char[] op, String a) {
if (index == str.length) {
String s = "";
for (int i = 0; i < str.length; i++) {
s += a.charAt(i);
if(str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
s+= ' ';
if (str[i]!= ' ')
s+= str[i];
if(str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
s+= ' ';
}
s += a.charAt(a.length() - 1);
Strarr.add(s);
Return;
} else {
for (int i = 0; i < op.length; i++) {
str[index] = op[i];
bf(str, index + 1, op, a);
}
}
}
}
Great Common Divisor
import java.util.*;
public class GreatestCommonDivisor {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.println(gcd(a,b));
}
Public static int gcd(int p, int q)
{
if (q == 0) return p;
int r = p % q;
Return gcd(q, r);
}
}
Labirinth
Import java.io.File;
import java.io.*;
Import java.util.Scanner;
public class Labirint {
public static void main(String args[]) throws Exception{
Scanner in = new Scanner(new File("input.txt"));
int n = in.nextInt();
int m = in.nextInt();
int a[][] =new int[n][m];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
a[i][j]= in.nextInt();
int x = in.nextInt()-1;
int y = in.nextInt()-1;
a[x][y] = 2;
FindPath(a,x,y);
}
private static void findPath(int[][] a, int x, int y) {
if(x==0||y==0||x==a.length-1||y==a[0].length-1){
PrintArray(a);
Return;
}
if(x > 0 && a[x-1][y] == 0){// jogary koteriledi
a[x-1][y] = a[x][y]+1;
findPath(a, x-1, y);// kaityp keledi
a[x-1][y] = 0;
}
if(y < a[x].length-1 && a[x][y+1] == 0){ //onga juredi
a[x][y+1] = a[x][y]+1;
findPath(a,x,y+1);
a[x][y+1] = 0;
}
if(x < a.length-1 && a[x+1][y] == 0){// tomen tusedi
a[x+1][y] = a[x][y]+1;
findPath(a, x+1, y);
a[x+1][y] = 0;
}
if(y>0 && a[x][y-1]==0){ // solga juredi
a[x][y-1] = a[x][y]+1;
findPath(a, x, y-1);
a[x][y-1] = 0;
}
}
private static void printArray(int[][] a) {
System.out.println("——————————————");
for(int i=0; i<a.length; i++, System.out.println())
for(int j=0; j<a[i].length; j++)
System.out.printf("%3d",a[i][j]);
System.out.println("——————————————");
}
}
Museum
import java.util.*;
import java.io.*;
public class Museum {
public static void main(String [] args)throws Exception{
Scanner input = new Scanner(new File("input.txt"));
PrintWriter output = new PrintWriter(new File("output.txt"));
int n = Integer.parseInt(input.nextLine());
int maxDif = 0, count = 1, index = 0;
int max [] = new int [n];
int min [] = new int [n];
for(int i = 0; i < n; i++){
String line = input.nextLine();
String [] s = line.split(" ");
String line1 = s [0];
String [] s1 = line1.split(":");
String line2 = s [1];
String [] s2 = line2.split(":");
max[i]=Integer.parseInt(s2[0]+s2[1]);
min[i]=Integer.parseInt(s1[0]+s1[1]);
if((max[i]-min[i])>maxDif){
maxDif = max[i]-min[i];
index = i;
}
}
for(int k = 0; k < n; k++){
if(max[index]>max[k] && min[index]<max[k]){
count++;
}else
if(max[index]>min[k] && min[index]<min[k]){
count++;
}
}
Дата добавления: 2015-10-30; просмотров: 132 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Дизайн високого рівня | | | Output.print(count); |