Sami
BAN USERimport java.util.ArrayList;
public class ConnectedNode {
Node root;
ConnectedNode() {
root = new Node(1);
prepareNodes();
}
private static class Node {
int id;
boolean visited;
ArrayList<Node> nodes = new ArrayList<Node>();
public Node(int id) {
this.id = id;
}
}
void printConnectedEachOther(Node node) {
if (node == null || node.visited) {
return;
}
boolean print = true;
for (Node n : node.nodes) {
if (!n.nodes.contains(node)) {
print = false;
break;
}
}
if (print) {
System.out.println(node.id);
}
node.visited = true;
for (Node connected : node.nodes) {
printConnectedEachOther(connected);
}
}
void prepareNodes() {
Node n2 = new Node(2);
Node n3 = new Node(3);
Node n4 = new Node(4);
Node n5 = new Node(5);
root.nodes.add(n2);
root.nodes.add(n3);
root.nodes.add(n4);
n2.nodes.add(n3);
n3.nodes.add(n2);
n4.nodes.add(root);
}
public static void main(String[] args) {
ConnectedNode cn = new ConnectedNode();
cn.printConnectedEachOther(cn.root);
}
}
public class IntToString {
char[] number = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999 };
public String toString(int num) {
int size = 0;
boolean symbol = false;
if (num < 0) {
symbol = true;
size = 1;
num = -num;
}
int maxPosition = sizeTable.length - 1;
if (num > sizeTable[maxPosition]) {
throw new RuntimeException("Number should be < "
+ sizeTable[maxPosition]);
}
for (int i = 0; i <= maxPosition; i++) {
if (num <= sizeTable[i]) {
size = size + i + 1;
break;
}
}
char[] nums = new char[size];
if (symbol) {
nums[0] = '-';
}
int i = size - 1;
while (num != 0) {
int d = num - (num / 10) * 10;
num = num / 10;
nums[i--] = number[d];
}
return new String(nums);
}
public static void main(String[] args) {
IntToString to = new IntToString();
System.out.println(to.toString(-9999999));
}
}
- Sami April 25, 2016
- Sami April 28, 2016