Skip to content

Instantly share code, notes, and snippets.

@boxysean
Last active December 19, 2015 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boxysean/6013449 to your computer and use it in GitHub Desktop.
Save boxysean/6013449 to your computer and use it in GitHub Desktop.
Class 03 solutions
import java.util.Scanner;
public class A {
public static String binaryString(int x, int n) {
StringBuilder sb = new StringBuilder();
sb.append(Integer.toString(x, 2));
while (sb.length() < n) {
sb.insert(0, '0');
}
return sb.toString();
}
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
for (int count = 0; count < N; count++) {
if (count != 0) {
System.out.println();
}
int n = in.nextInt();
int h = in.nextInt();
for (int i = 0; i < (1 << n); i++) {
if (Integer.bitCount(i) == h) {
System.out.println(binaryString(i, n));
}
}
}
}
}
import java.util.Scanner;
import java.util.Stack;
public class B {
public static int twosComplement(int x) {
return (x ^ 0xFFFFFFFF) + 1;
}
public static void printBinaryString(int x) {
Stack<Character> stack = new Stack<Character>();
while (x > 0) {
char ch = (char) ('0' + (x & 1));
stack.push(ch);
x >>= 1;
}
while (!stack.isEmpty()) {
System.out.print(stack.pop());
}
System.out.println();
}
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int x = in.nextInt();
// Really easy if you know the Java API
// System.out.println(Integer.reverseBytes((int) x));
// int y = (x & 0xFF000000) >> 24
// | (x & 0x00FF0000) >> 8
// | (x & 0x0000FF00) << 8
// | (x & 0x000000FF) << 24;
int y = (x >> 24) & 0x000000FF
| (x >> 8) & 0x0000FF00
| (x << 8) & 0x00FF0000
| (x << 24) & 0xFF000000;
System.out.printf("%d converts to %d\n", x, y);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment