[Introduction to Programming-c] numbers for eigenvalues

Source: Internet
Author: User

The topic is the exercises of the C language of NetEase cloud classroom


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.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/58/18/wKioL1So_efzTxNkAACR8zWlokE440.jpg "title=" 1.png " alt= "Wkiol1so_efztxnkaacr8zwloke440.jpg"/>

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


My solution: The main use of bit operations to judge parity, XOR or to determine whether the consistency

#include <stdio.h>int main () {int in,out,pos;out = pos = 0;//init scanf ("%d", &in), while (in > 0) {int flag = (( IN%10) (&1) ^ (pos&1);//Whether the value is consistent based on the number parity and the digital parity. Since the POS is calculated from 0, so with XOR, just achieve odd parity parity get 1 rule out + = flag<<pos;// The left shift number turns into 10 binary note instead of right shift pos++;in/= 10;} printf ("%d", out); return 0;}


This article is from the "RYANSCN" blog, make sure to keep this source http://ryanscn.blog.51cto.com/2725212/1599034

[Introduction to Programming-c] numbers for eigenvalues

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.