13.8k views
2 votes
In this question, you need to read an integer as a char array. Then find the largest k bits of the integer with their original sequence. For example, the input integer is 1432219 and k is given as 4 . Then the output should be 4329. The input first gives the value of k, and then the integer. Assume that k is larger than 0 and smaller than the length of the integer. The input integer will be less than 20 bits. It is better to store the input as a char array for convenient operations.

User LeWoody
by
8.0k points

1 Answer

1 vote

Final answer:

To find the largest k bits of an integer, convert the integer to a char array and iterate through it to find the largest k bits.

Step-by-step explanation:

To find the largest k bits of an integer, you can convert the integer to a char array and iterate through the array to find the largest k bits. Here's one way to do it:

  1. Convert the integer to a string using the to_string() function.
  2. Convert the string to a char array.
  3. Initialize a variable to store the largest k bits.
  4. Create a loop that iterates through the char array, starting from the first element.
  5. Inside the loop, check if the current k-bit sequence is larger than the largest k bits found so far. If it is, update the largest k bits variable to the current k-bit sequence.
  6. Continue the loop until the end of the char array, keeping track of the largest k bits found.
  7. Output the largest k bits.

For example, if the input integer is 1432219 and k is 4, the char array representation is ['1', '4', '3', '2', '2', '1', '9']. The largest k bits would be '4329'.

User Ehftwelve
by
7.6k points