eng.mohamed.CSD2014
BAN USERpublic class P24 {
private static void print(String start, int n) {
for (int i = 2; i < n; i++) {
StringBuilder newString = new StringBuilder();
int j = 0;
while (j < start.length()) {
char c = start.charAt(j);
int count = 0;
while (j < start.length() && start.charAt(j) == c) {
count++;
j++;
}
newString.append(count + "" + c);
}
System.out.println(newString);
start = newString.toString();
}
}
public static void main(String[] args) {
print("1", 10);
}
}
public class P23 {
private static void compare(String first, String second) {
for (int i = 0; i < first.length(); i++) {
if (first.charAt(i) > second.charAt(i)) {
System.out.println("First is larger");
return;
} else if (first.charAt(i) < second.charAt(i)) {
System.out.println("Second is larger");
return;
}
}
System.out.println("Equal");
}
public static void main(String[] args) {
compare("010203", "030201");
}
}
import java.util.Stack;
public class P22 {
private static boolean balancedCheck(String s) {
Stack<Character> stack = new Stack<Character>();
for (char c : s.toCharArray()) {
if (c == '[' || c == '(' || c == '{') {
stack.push(c);
} else if (c == ')') {
if (stack.isEmpty() || stack.peek() != '(') {
return false;
}
stack.pop();
} else if (c == '}') {
if (stack.isEmpty() || stack.peek() != '{') {
return false;
}
stack.pop();
} else if (c == ']') {
if (stack.isEmpty() || stack.peek() != '[') {
return false;
}
stack.pop();
}
}
return stack.isEmpty();
}
public static void main(String[] args) {
System.out.println(balancedCheck("{a[(b]]}"));
}
}
public class P21 {
private static void printSpircal(char[][] matrix) {
int n = matrix.length;
for (int r = 0; r <= n / 2; r++) {
for (int j = r; j < n - r; j++) {
System.out.print(matrix[r][j] + " ");
}
for (int j = r + 1; j < n - r; j++) {
System.out.print(matrix[j][n - r - 1] + " ");
}
for (int j = n - r - 2; j >= r; j--) {
System.out.print(matrix[n - r - 1][j] + " ");
}
for (int j = n - r - 2; j > r; j--) {
System.out.print(matrix[j][r] + " ");
}
}
System.out.println();
}
public static void main(String[] args) {
char[][] matrix = { { 'a', 'b', 'c', '1' }, { 'd', 'e', 'f', '2' },
{ 'g', 'h', 'k', '3' }, { '4', '5', '6', '7' } };
printSpircal(matrix);
}
}
import java.util.ArrayList;
public class A2 {
private static ArrayList<Integer> primes(int n) {
boolean[] notPrime = new boolean[n + 1];
notPrime[0] = notPrime[1] = true;
ArrayList<Integer> results = new ArrayList<Integer>();
for (int i = 2; i < n; i++) {
if (notPrime[i] == false) {
results.add(i);
for (int j = i * i; j < n; j += i) {
notPrime[j] = true;
}
}
}
return results;
}
public static void main(String[] args) {
System.out.println(primes(100));
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Prime {
private static ArrayList<Integer> findPrims(int n) {
ArrayList<Integer> primes = new ArrayList<Integer>();
boolean[] notPrime = new boolean[n + 1];
notPrime[0] = notPrime[1] = true;
for (int i = 2; i < notPrime.length; i++) {
if (notPrime[i] == false) {
primes.add(i);
for (int j = i * i; j < notPrime.length; j += i) {
notPrime[j] = true;
}
}
}
return primes;
}
private static int[] primeFactors(ArrayList<Integer> primes, int n) {
int[] powers = new int[primes.size()];
int c = 0;
while (n != 1) {
while (n % primes.get(c) == 0) {
n /= primes.get(c);
powers[c]++;
}
c++;
}
return powers;
}
private static void get() {
Scanner scan = new Scanner(System.in);
String s = scan.next().substring(2);
while (s.length() < 4)
s += "0";
int num = Integer.parseInt(s);
ArrayList<Integer> primes = findPrims(10000);
int[] p10000 = primeFactors(primes, 10000);
int[] pNum = primeFactors(primes, num);
int numerator = 1, dunmerator = 1;
for (int i = 0; i < p10000.length; i++) {
int min = Math.min(p10000[i], pNum[i]);
p10000[i] -= min;
pNum[i] -= min;
numerator *= Math.pow(primes.get(i), pNum[i]);
dunmerator *= Math.pow(primes.get(i), p10000[i]);
}
System.out.println(numerator + "/" + dunmerator);
}
public static void main(String[] args) {
get();
}
}
import java.util.Scanner;
import java.util.TreeSet;
public class P5767531572756480 {
private static boolean get() {
boolean colorful = true;
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
TreeSet<Integer> mask = new TreeSet<Integer>();
for (int i = 0; i < a.length; i++)
a[i] = scan.nextInt();
int power = 1 << n;
for (int i = 0; i < power; i++) {
int tmp = 1;
for (int j = 0; j < a.length; j++) {
if ((i & 1 << j) != 0) {
tmp *= a[j];
}
}
if (mask.contains(tmp)) {
colorful = true;
break;
} else
mask.add(tmp);
}
return colorful;
}
public static void main(String[] args) {
System.out.println(get() ? "Colorful" : "Not Colorful");
}
}
[[[
private static String transate(String seq) {
char[][] dic = { { 'a', 'b', 'c' }, { 'd', 'e', 'f' },
{ 'g', 'h', 'i' }, { 'j', 'k', 'l' }, { 'm', 'n', 'o' },
{ 'p', 'q', 'r', 's' }, { 't', 'u', 'v' },
{ 'w', 'x', 'y', 'z' } };
String res = "";
for (int i = 0; i < seq.length(); i++) {
if (seq.charAt(i) == '#') {
res += ' ';
} else if (seq.charAt(i) >= '0' && seq.charAt(i) <= '9') {
char last = seq.charAt(i);
int index = (last - '0') - 2;
int c = 0;
while (++i < seq.length() && seq.charAt(i) == last)
c = (c + 1) % dic[index].length;
res += dic[index][c];
}
}
return res;
}
]]]
let d = 1 and k = 3,
what is the output of that case ?
I implemented your algorithm
import java.util.Scanner;
public class TextReverse{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char[] text = scan.nextLine().toCharArray();
int start = 0;
for (int i = 0; i < text.length; i++) {
if (text[i] == ' ' || i == text.length - 1) {
int tempI = text[i] == ' ' ? i - 1 : i;
while (tempI > start) {
char temp = text[start];
text[start] = text[tempI];
text[tempI] = temp;
start++;
tempI--;
}
start = i + 1;
}
}
for (int i = 0; i < text.length / 2; i++) {
char temp = text[i];
text[i] = text[text.length - i - 1];
text[text.length - i - 1] = temp;
}
System.out.println(text);
}
}
public class careercup {
private static String data = "a?bc?def?g";
private static void get(int index, String counter) {
if (index == data.length())
System.out.println(counter);
else if (data.charAt(index) == '?') {
get(index + 1, "0" + counter);
get(index + 1, "1" + counter);
} else {
get(index + 1, data.charAt(index) + counter);
}
}
public static void main(String[] args) {
get(0, "");
}
}
/**
* find all subset which have summation = certain value in list
*
* @author mohamed
*
*/
public class SubSetSum {
// list of numbers
private static int[] list = { 1, 3, 5, 2, 7 };
/**
*
* @param index
* index = 0
* @param rem
* = K (the value which sum of subset must be)
* @param visited
* bitmask equal list in length and all items in it= "false"
* @return true if subset found else false
*/
private static boolean get(int index, int rem, boolean[] visited) {
// base case
if (index == list.length || rem < 0)
return false;
if (rem == 0) {
// if we find subset its sum = rem
for (int i = 0; i < visited.length; i++) {
if (visited[i])
System.out.print(list[i] + " ");
}
System.out.println();
return true;
} else {
visited[index] = true;
boolean res1 = get(index + 1, rem - list[index], visited);
visited[index] = false;
return res1 | get(index + 1, rem, visited);
}
}
public static void main(String[] args) {
get(0, 10, new boolean[list.length]);
}
}
- eng.mohamed.CSD2014 July 22, 2015