Introduction to MOOC Programming in Chinese Universities--c language: Third week quiz

Source: Internet
Author: User

1. Number of parity (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

Reference code:

1#include <stdio.h>2 intMain () {3     intOdd=0, even=0, a=0;//odd variable odd, even variable even; input value variable A is initialized to 0.4      while(1)5     {6scanf"%d",&a);7         if(a!=-1)8         {9             if(a%2==0) even++;//a%2, equal to 0 is a can be divisible by 2, so a is even, even variable even value +1.Ten             Elseodd++; One         } A         Else  Break;//Enter the value a=-1, jump out of the loop, enter the end. -     } -printf"%d%d\n", Odd,even); the     return 0; -}

2. Numeric eigenvalues (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

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

tip: To decompose integers from right to left, the digits are added each time 1 , and the binary value is multiplied each time 2 .

Input Format :

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

Output format:

An integer that represents the result of the calculation.

Input Sample:

342315

Sample output:

13

Time limit: 500ms memory limit: 32000kb

Reference code:

1#include <stdio.h>2 intMain () {3     inta=-1, b=-1, i=1, t=1, sum=0;//Initialization of variables4scanf"%d",&a);5     //remove one number from the low to the highest from a6      while(1)7     {8         if(a<Ten)//A only one number9         {Ten             if((a%2) = = (i%2)) b=1; One             Elseb=0; Asum=sum+b*T; -              Break; -         } the         Else                      //A has two digits above -         { -b=a%Ten; -             if((b%2) = = (i%2)) b=1; +             Elseb=0; -Sum=sum+b*t;//the binary value of the corresponding number of digits is obtained by the T-Multiply 2. +t=t*2; Ai++; ata=a/Ten; -         } -     } -printf"%d", sum); -     return 0;      -}

Introduction to MOOC Programming in Chinese Universities--c language: Third week quiz

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.