Cloud Classroom Javaweb Micro-professional Java Basic Test 3

Source: Internet
Author: User
Tags pow

The 3rd Chapter 1, Odd even number (5 points)

Topic content:

Your program reads a series of positive integer data, input 1 indicates the end of the input, and 1 is not the input data itself. The number of odd and even numbers in the data read by the program output.

Input format:

A series of positive integers in which the range of integers is (0,100000). If input-1 indicates the end of the input.

Output format:

Two integers, the first integer representing the number of odd numbers in the read data, and the second integer representing the number of even numbers in the read data. Two integers are separated by a space.

Input Sample:

9 3 4 2 5 7-1

Sample output:

4 2

Time limit: 500ms memory limit: 32000kb

1 ImportJava.util.Scanner;2 3  Public classMain {4 5      Public Static voidMain (string[] args) {6         //TODO auto-generated Method Stub7Scanner in =NewScanner (system.in);8         intInnum =in.nextint ();9         intOddnum=0;Ten         intEvennum=0; One          while(Innum! =-1){ A             if(innum%2==0){ -evennum++; -}Else{ theoddnum++; -                  -             } -Innum =in.nextint (); +         } -System.out.println (oddnum+ "" +evennum); +     } A}
number of odd and even2, the numeric characteristic value (5 points)

Topic content:

It is a common coding algorithm to find the eigenvalues of numbers, and the odd-even feature is a simple characteristic value. For an integer, each digit is numbered from bit to digit, number 1th, 10 digit is 2nd, and so on. This integer number on the nth bit is recorded as x, if the parity of X and N is the same, write down a 1, otherwise write down a 0. 0 and 1 of the parity of the corresponding bit are recorded in the order of integers, and a binary number is formed. For example, for 342315, this binary number is 001101.

The calculation here can be expressed in the following table:

Digital

3

4

2

3

1

5

Digital digits

6

5

4

3

2

1

Digital parity

Odd

I

I

Odd

Odd

Odd

Digital odd-even

I

Odd

I

Odd

I

Odd

Parity consistent

0

0

1

1

0

1

Bits value

32

16

8

4

2

1

By adding the bit value of 1 bits to the bits value, you get the result 13.

Your program reads a nonnegative integer, the range of integers is [0,100000], and then calculates the binary number that represents the parity by the above algorithm, outputting its corresponding decimal value.

Tip: The integer is decomposed from right to left, the digits are added 1 at a time, and the binary value is multiplied by 2.

Input format:

A nonnegative integer in which the range of integers is [0,100000].

Output format:

An integer that represents the result of the calculation.

Input Sample:

342315

Sample output:

13

Time limit: 500ms memory limit: 32000kb

/**
* When not 0 and 1 o'clock, the integer digits are calculated first, then from right to left to compare each bit with the number of the parity, if the same is added 2 (the number of bits-1) the second square
* */

1 ImportJava.util.Scanner;2 3  Public classMain {4      Public Static voidMain (string[] args) {5         //TODO auto-generated Method Stub6Scanner in =NewScanner (system.in);7         intInnum =in.nextint ();8         9         intTmpnum = 6;Ten         intZnum=0; One         ints = (int) Math.pow (10, tmpnum); A         if(innum==0){ -SYSTEM.OUT.PRINTLN (0); -}Else if(innum==1){ theSYSTEM.OUT.PRINTLN (1); -}Else{ -             intTmpinnum =Innum; -              while(tmpnum>0){ +                 if((innum%s)! =innum) { -                      Break; +                 } Atmpnum--; ats = (int) Math.pow (10, tmpnum); -             } -             intTmpNum2 =tmpnum+1; -             intHeightnum =0; -              while(tmpnum>=0){ -s = (int) Math.pow (10, tmpnum); inHeightnum = tmpinnum/s;//Highest bit -                 if((heightnum%2==0 && tmpnum2%2==0) | | (Heightnum%2!=0 && tmpnum2%2!=0) ){ toZnum + = (int) Math.pow (2, tmpnum); +                      -                 } theTmpinnum = tmpinnum-heightnum*s; *tmpnum--; $TmpNum2 =tmpnum+1;Panax Notoginseng             } - System.out.println (znum); the         } +     } A      the}
Numeric eigenvalues

Cloud Classroom Javaweb Micro-professional Java Basic Test 3

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.