babesh
BAN USER#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int parity(int n, int bits, int p) {
return (bits == 0) ? p : parity(n, bits - 1, (n & (1 << (bits-1))) ? ((p + 1) % 2) : p);
}
int main(int argc, char**argv) {
int n = 0;
sscanf(argv[1], "%d", &n);
int bits = floor(log(n)/log(2)) + 1;
int p = parity(n, bits, 0);
printf("%d", p);
return 0;
}
// creates a copy of a and also dumps to stdout
char* cp(char *a)
{
char *f;
f=(char*)malloc(strlen(a)*sizeof(char)); // need to allocate an extra character for null termination, malloc may fail, so should initialize f to null, and probably check for it
while(*a != 0)
{
*f=*a; // pointer to beginning of character string is lost, should have a char *g = f before the while and return g instead
f++;
a++;
} // f is not null terminated, should do *f = '\0';
cout<< f;
return f;
}
Flow water upwards from Pacific and Atlantic and look for intersection.
import java.util.*;
public class Divide {
private static class Node {
public int row;
public int column;
public int hashCode() {
return row + column;
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (obj instanceof Node) {
Node n = (Node)obj;
return (n.row == this.row && n.column == this.column);
} else {
return false;
}
}
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append(this.row);
buf.append(",");
buf.append(this.column);
return buf.toString();
}
public Node() {
}
public Node(int row, int column) {
this.row = row;
this.column = column;
}
};
private int array[][];
private HashSet<Node> pacific;
private HashSet<Node> atlantic;
private HashSet<Node> divide;
private int rows;
private int columns;
private Divide() {
}
private void divide(int array[][]) {
this.array = array;
this.rows = array.length;
this.columns = array[0].length;
this.pacific = this.initPacific();
this.atlantic = this.initAtlantic();
this.divide = new HashSet<Node>();
this.findDivide();
this.dump();
}
private void dump() {
for (Node node : this.divide) {
System.out.print("{");
System.out.print(node.row);
System.out.print(",");
System.out.print(node.column);
System.out.print("}");
System.out.print(",\n");
}
}
private void findDivide() {
HashSet<Node> newPacific = new HashSet<Node>(this.pacific);
HashSet<Node> newAtlantic = new HashSet<Node>(this.atlantic);
while (true) {
newPacific = this.findNext(newPacific, 1, 1);
newAtlantic = this.findNext(newAtlantic, -1, -1);
if (!newPacific.isEmpty() || !newAtlantic.isEmpty()) {
this.pacific.addAll(newPacific);
this.atlantic.addAll(newAtlantic);
} else {
HashSet<Node>intersect = new HashSet<Node>(this.pacific);
intersect.retainAll(this.atlantic);
this.divide.addAll(intersect);
return;
}
}
}
private HashSet<Node> findNext(HashSet<Node>set, int rowDir, int columnDir) {
HashSet<Node> newSet = new HashSet<Node>();
for (Node node : set) {
int otherRow = node.row + rowDir;
if (0 <= otherRow && otherRow < this.rows) {
int n = this.array[otherRow][node.column];
if (n >= this.array[node.row][node.column]) {
Node otherNode = new Node(otherRow, node.column);
newSet.add(otherNode);
}
}
int otherColumn = node.column + columnDir;
if (0 <= otherColumn && otherColumn < this.columns) {
int n = this.array[node.row][otherColumn];
if (n >= this.array[node.row][node.column]) {
Node otherNode = new Node(node.row, otherColumn);
newSet.add(otherNode);
}
}
}
return newSet;
}
private HashSet<Node>initPacific() {
HashSet<Node>set = new HashSet<Node>();
for (int i = 0; i < this.rows; i++) {
set.add(new Node(i, 0));
}
for (int i = 1; i < this.columns; i++) {
set.add(new Node(0, i));
}
return set;
}
private HashSet<Node>initAtlantic() {
HashSet<Node> set = new HashSet<Node>();
for (int i = 0; i < this.rows; i++) {
set.add(new Node(i, this.columns-1));
}
for (int i = 0; i < this.columns-1; i++) {
set.add(new Node(this.rows - 1, i));
}
return set;
}
public static void main(String args[]) {
int array[][]= { { 1,2,2,3,5},{3,2,3,4,4},{2,4,5,3,1},{6,7,1,4,5},{5,1,1,2,4}};
Divide divide = new Divide();
divide.divide(array);
}
}
Assuming you only need to find one pair, you only have to make one pass:
import java.util.*;
public class Sum {
public static void main(String args[]) {
int numbers[] = { 2, 5, 3, 7, 9, 8};
int sum = 11;
HashSet<Integer> opposingPair = new HashSet<Integer>();
for (int i = 0; i < numbers.length; i++) {
int n = numbers[i];
int diff = sum - n;
if (opposingPair.contains(new Integer(diff))) {
System.out.print(n);
System.out.print(",");
System.out.print(diff);
return;
} else {
opposingPair.add(new Integer(n));
}
}
}
}
import java.util.*;
import java.io.*;
public class Sub {
private static class Node {
public int n;
public ArrayList<Node> children;
public Node(int n) {
this.n = n;
this.children = new ArrayList<Node>();
}
public void add(Node node) {
Boolean found = Boolean.FALSE;
for (Node child : this.children) {
if (child.n < node.n) {
child.add(node);
found = Boolean.TRUE;
}
}
if (!found) {
this.children.add(node);
}
}
public ArrayList<Node> longest() {
ArrayList<Node> l = null;
for (Node child : this.children) {
ArrayList<Node> lChild = child.longest();
if (l == null || lChild.size() > l.size()) {
l = lChild;
}
}
if (l == null) {
l = new ArrayList<Node>();
}
l.add(0, this);
return l;
}
}
public static void main(String args[]) {
int numbers[] = { -1, 2, 100, 100, 101, 3, 4, 5, -7 };
Node head = new Node(Integer.MAX_VALUE);
for (int i = 0; i < numbers.length; i++) {
head.add(new Node(numbers[i]));
}
ArrayList<Node> longest = head.longest();
StringBuffer buf = new StringBuffer();
for (int i = 1; i < longest.size(); i++) {
if (i > 1) {
buf.append(",");
}
buf.append(Integer.toString(longest.get(i).n));
}
System.out.println(buf.toString());
}
}
Idea is to map from customer token to container slots. Also keep stacks of free customer tokens, free slots, and misplaced slots for each container.
So a customer comes in. You pop a free customer token. You find the object size and try to pop progressively larger containers till you find a free slot. Associate the customer token with the container slot. If the slot is bigger than it needs to be, push that customer into the misplaced slots stack for that container. If you find nothing and its a large object then look in the misplaced slots and see if you also have free slots in the smallest free stack. Remove one of them while keeping customer token and add it back to the smallest free stack, then proceed with adding the large object since freeing made a space available.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct Node {
struct Node *parent;
char *name;
};
typedef struct Node Node;
Node *processToken(char *name, Node *current) {
if (strcmp(name, ".") == 0) {
return current;
} else if (strcmp(name, "..") == 0) {
if (current != NULL && current->parent != NULL) {
return current->parent;
} else {
Node *node = malloc(sizeof(Node));
node->name = name;
node->parent = NULL;
if (current != NULL) {
current->parent = node;
}
return node;
}
} else {
Node *node = malloc(sizeof(Node));
node->parent = current;
node->name = name;
return node;
}
}
void printNode(Node *node) {
if (node == NULL) {
return;
}
if (node->parent != NULL) {
printNode(node->parent);
printf("/");
}
printf("%s", node->name);
}
char *mystrsep(char **ps) {
char *o = *ps;
if (*o == '\0') {
return NULL;
}
while (**ps != '/' && **ps != '\0') {
(*ps)++;
}
if (**ps == '/') {
**ps = '\0';
(*ps)++;
}
return o;
}
int main(int argc, char **argv) {
Node *current = NULL;
char *s = strdup("e/../../a/d/./../b/c");
char *token = NULL;
while ((token = mystrsep(&s)) != NULL) {
current = processToken(token, current);
}
printNode(current);
printf("\n");
return 0;
}
As other posters said:
// #
// ##
// ###
// ####
#include <stdio.h>
int findMax(int *ints, int cInts) {
int max = 0;
for (int iInt = 0; iInt < cInts; iInt++) {
if (ints[iInt] > max) {
max = ints[iInt];
}
}
return max;
}
void printHashes(int *ints, int cInts) {
int max = findMax(ints, cInts);
for (int row = 0; row < max; row++) {
for (int iInt = 0; iInt < cInts; iInt++) {
if (ints[iInt] >= (max - row)) {
printf("#");
} else {
printf(" ");
}
}
printf("\r\n");
}
}
int main(int argc, char **argv) {
int ints[] = { 1, 4, 3, 2 };
printHashes(ints, sizeof(ints)/sizeof(int));
return 0;
}
The idea is to do this recursively by looking at all your children and seeing if you extend any of them. Just choose the longest of the child sequences to extend.
Whenever you are done with a node, see if its extension is longer than the longest. If it is, then update the longest.
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
int longest = 0;
struct Node {
int value;
struct Node **children;
int cChildren;
};
typedef struct Node Node;
int findLongest(Node *node) {
int myLongest = 1;
for (int iChild = 0; iChild < node->cChildren; iChild++) {
Node *child = node->children[iChild];
int childLongest = findLongest(child);
if (child->value == (node->value + 1)) {
if ((childLongest + 1) > myLongest) {
myLongest = childLongest + 1;
}
}
}
if (myLongest > longest) {
longest = myLongest;
}
return myLongest;
}
RepZoeKim, AT&T Customer service email at A9
Holds a job in the social work sector and directly deals primarily with women and children who are fleeing domestic ...
Repmaryjcowell, Android Engineer at AMD
Hey I am information designer.Information Designers facilitate the delivery of information by translating complex information into information that users ...
- babesh March 17, 2015