1) To convert a decimal number x to binary
You just need to find the largest power of 2 that is less than x, then subtract it from x; repeating the process until you get 1 or 0, which is the unit digit. The power numbers indicate where there is a 1 in the binary (counting from 0, right to left). Ex,
135-128 = 7; 7-4=3; 3-2=1, so the right most digit of the binary is 1.
2^2=4, 2^7 = 128, so there are two 1's in the 7th and 2nd places(counting from 0, right to left). So the final result is 10000101.
2) To convert octal to binary, or binary to octal, you just need to replace 1 octal digit with 3 binary digits as below. Add leading 0's as necessary.
000 = 0
001 = 1
010 = 2
011 = 3
100 = 4
101 = 5
110 = 6
111 = 7
Ex, 345 = 011100101, 10110101 = 010 110 101 = 265 (octal numbers are italic)
3) To convert hexadecimal to binary, or binary to hexadecimal, you just need to replace 1 hexadecimal digit with 4 binary digits as below. Add leading 0's as necessary.
0000 = 0 1000 = 8
0001 = 1 1001 = 9
0010 = 2 1010 = A
0011 = 3 1011 = B
0100 = 4 1100 = C
0101 = 5 1101 = D
0110 = 6 1110 = E
0111 = 7 1111 = F
Ex, A4E = 101001001110, 11010110101 = 0110 1011 0101 = 6B5 (hex numbers are italic)
You have learnt a lot in the past few months, now you are ready for your first local contest. To help you prepare, you want to complete the challenges below:
2) Coding Task The coding task's code base is below. Feel free to copy and paste it in Codiva to run and test with all data. Be sure to answer all 4 questions in comment lines.
--------------------------------
class Deletion {
public static void delete(String s){
int count=0;
if (s.length()==0) {
System.out.println(count);
return;
}
else if (s.lastIndexOf('0')>=0){
s=s.substring(s.lastIndexOf('0')+1); // Q1: What does this line do?
count++;
delete(s); // recursive call!
}else{
s=reduce(s);
count++;
delete(s); // recursive call!
}
}
// This is a helper function to reduce the max digit
public static String reduce (String s){
char[] a = s.toCharArray(); // Q2: What does this line do?
int largeIndex=0;
char large = a[0];
for(int i=1;i<a.length;i++) // Q3: What does this for loop do?
{
if(a[i]>=large){
large=a[i];
largeIndex=i;
}
}
if((large-48)%2==1){ // '0' ascii code is 48, so large-48 is the integer value
a[largeIndex]-=1;
}
else {
a[largeIndex]-=2;
}
return new String(a); // turn char array back into a string
}
public static void main(String[] args) {
/* Q4: Can you add a Scanner object to read 5 lines of input data from a file? this main only process 1 string */
String x = "3580254";
delete(x);
}
}
Questions:
1) 42
2) 22
3)20026
4) B
5) Idk
Coding:
1) Say that s is the only digit after the very last zero in the code.
2) It makes s its own char in the array.
3) It defines the largest char and that largeIndex is = i.
4)Yes
1. adds two strings
2. makes a char array of 0
3. defines the i in a char array
4. no
1. 19
2. 3
3. 010000001110
4. B
5. -11
Short Question Answers
Questions:
1. 42
2. 22
3. 20026
4. b
5. -12
Code:
1. it redefines s to be only the digits after the last 0
2. it puts every character of s as a separate char element in an array
3. it defines large as the largest char in the array, and largeIndex as the index of large
4. yes
1)42
2)21
3)20026
4)B
5)-12
1. Identifys the zero's position in the string
2. Connects 2 strings together
3. Scans every line of the array
4. IDK
1. 42
2. 13
3. 10034
4. B
5.3/40