Computer Testing Questions (convert digital string to binary-java), java
/**
Title: convert a numeric string to a binary string
Description: enter an integer to convert each integer to a binary number. If the last and third bits are "0", the output value is "0". If it is "1 ", output "1 ".
Question category: bitwise operation
Difficulty: elementary
Score: 60
Run Time Limit: 10 Sec
Memory limit: 128 MByte
Phase: apply for an exam
Input: a string of integers with a length less than 1024. The integers are separated by spaces.
Output: an integer string of 1/0 separated by Spaces
Sample input: 240 0
Sample output: 0 0
Answer:
*/
Public class Main {public static void main (String [] args) {// obtain the nth digit of the corresponding binary number in decimal format. // (x> (n-1 )) & 1 // enter a string of integers // obtain the string array // convert the number string to an integer // obtain the integer of 0/1 // obtain the string of 0/1. cin= new Numeric (System. in); String srcStr = cin. nextLine (); String [] strArray = srcStr. split (""); StringBuilder sb = new StringBuilder (); if (strArray. length >=1024) {System. out. println ("length cannot exceed 1024"); System. exit (0) ;}for (int I = 0; I <strArray. length; I ++) {int number = Integer. parseInt (strArray [I]); int bit = number> 2 & 1; sb. append (bit + "");} System. out. println (sb. toString (). substring (0, sb. length ()-1); cin. close ();}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.