lareinev
BAN USERpublic class Parenthesis {
static void pp(int lL, int rL, int pos, char[] str){
if(lL < 0 || lL > rL)
return;
if((lL==rL && rL==0)){
System.out.println(str);
return;
}
if(pos >= str.length)
return;
str[pos] = '(';
pp(lL-1, rL, pos+1, str);
str[pos] = ')';
pp(lL, rL-1, pos+1, str);
}
public static void main(String[] args) {
int n = 9;
int pairs = n/2;
char[] str = new char[pairs*2];
pp(pairs, pairs, 0, str);
}
}
Stack<String> w = new Stack<String>();
//int n = 55; // 110111
//int n = 53; // 110101
int n = 54; // 110110
//int n = 52; // 110100
int thousands = (n&((~(0xFFFFFFFF<<3))<<3))>>3;
int hundreds = n&(~(0xFFFFFFFF<<3));
if((hundreds&(1<<1)) > 0){
if((hundreds&1) > 0)
w.push("eleven");
else
w.push("ten");
}else {
if((hundreds&1) > 0)
w.push("one");
}
if((hundreds&(1<<2)) > 0) {
w.push("one hundred");
}
if(thousands >0) {
w.push("and");
if((thousands&(1<<1)) > 0){
if((thousands&1) > 0)
w.push("eleven thousand");
else
w.push("ten thousand");
}else {
if((thousands&1) > 0)
w.push("one thousand");
}
if((thousands&(1<<2)) > 0) {
w.push("one hundred");
}
}
while(!w.empty())
System.out.print(w.pop() + " ");
public static void main(String[] args) {
String[] words = {"Apple", "Melon", "Orange", "Watermelon"};
String[] parts = {"a", "mel", "lon", "lon", "el", "An"};
for(int i = 0; i<words.length; i++) {
int pos = words[i].length(), k = -1, plen = 0;
for(int j = 0; j < parts.length; j++) {
int idx = words[i].indexOf(parts[j]);
if(idx>-1 && (parts[j].length() > plen || (parts[j].length() == plen && idx<pos))) {
pos = idx;
k = j;
plen = parts[j].length();
}
}
if(k>-1)
System.out.println(words[i].substring(0, pos) + "[" + words[i].substring(pos, pos+plen) + "]" + words[i].substring(pos+plen, words[i].length()));
else
System.out.println(words[i]);
}
}
}
Output:
Apple
Me[lon]
Or[a]nge
Water[mel]on
int k = 2;
void sort(int[] arr) {
int n = arr.length;
int[] idx = new int[n];
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
int delta = j - i;
int leftShift = idx[j] - delta;
int rightShift = idx[i] + delta;
if(arr[i] > arr[j] && ( Math.abs(leftShift) <= k ) ) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
idx[i] = leftShift;
idx[j] = rightShift;
}
}
}
}
- lareinev April 11, 2018
2. a) Bought 600 roses: cost = $0.3*600 = $180. Revenue = 600*(0.2 (a customer wanted exactly 600 roses) +0.3 (customers wanted 900 but i had only 600 )+05(customers wanted 1200 but again i had only 600)) = $600, profit = 600-180 = $420
- lareinev May 10, 2018b) Bought 900 roses: cost = $270. Revenue = 600*0.2 (if only 600 roses sold) + 900*(0.3+0.5) (if someone wanted those 900 roses or a customer wanted 900 but i had only 900) = 120+720=$840, profit = $570
c) Bought 1200 roses: cost = $360. Revenue = 600*0.2 + 900*0.3 + 1200*0.5 = 120+270+600=990, profit = $630