Computer Testing Questions (convert digital string to binary-java), java

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.