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: 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,1000000].
Output format:
An integer that represents the result of the calculation.
Input Sample:
342315
Sample output:
13
time limit: 500ms memory limit: 32000kb
1#include <stdio.h>2 intMain () {3 intnum,dicn,binn,single,twon=1, i=1, tenn=1;4scanf"%d",&num);5 inttemp=num;6 for(;(temp/tenn)! =0; i++){7single=num%Ten;//printf ("single=%d\n", single); 8binn=! ((single%2) ^ (i%2));9dicn+=binn*Twon; Tentwon*=2; Onetenn*=Ten; Anum=temp/TenN; - } -printf"%d\n", DICN); the return 0; -}
About 50min
Source of the topic
Introduction to Programming--c language _ Zhejiang University-China University Mooc (MU Class) http://www.icourse163.org/course/ZJU-199001
C Programming _zju--4th week 2_ Numeric eigenvalues