Algorithm Learning (ix)

Source: Internet
Author: User

1.Parity Control

Description: Anna lives in Al Gore, and Bob lives in Betigueri. Because these stars are located in different constellations-Perseus and Orion, the distances between them are far away. They found a way to communicate via e-mail.

However, due to the distance, some letters may be changed during transmission. The simple form of error checking is made by Anna: All letters are transmitted in the usual ASCII code, one byte per symbol. Each byte consists of 8 bits, but the highest bit is not used in the English language, usually 0.

Let's set this bit to 0 or 1 so that the sum of the ' 1 ' bits in the entire byte is always even (2, 4, 6, or 8), and here are some ways to encode the letters:

Symbol Ascii-code binary num-of-bits encoded-binary encoded-Dec'A'65 01000001 2) 01000001 65'B'66 01000010 2) 01000010 66'C'67 01000011 3) 11000011 195'.'46 00101110 4) 00101110 46' '32 00100000 1) 10100000 160

The communication line cannot change a bit more in each transmission byte. Therefore, there are odd ' 1 ' bits of bytes that are considered corrupted.

We are getting the message in this protected code. Our task is to check each letter and remove the broken letters. The others should be converted to normal ASCII and printed as characters.

Input data: The bytes that will contain the message being transmitted (represented by a sequence of decimal values, separated by a space).

The original information contains only the Latin alphabet (small and uppercase), numbers, and spaces.

The end of the message is represented by a dot character. -You can assume that this will never be destroyed.

Answer: Removes the message of the deleted bytes and is expressed as a character instead of a number.

For example:

input data:238 236 225Answer:ana.

Analysis: The input data, first with the bin () into the binary, in the format encoded-binary, not enough 8 bits through Zfill (8) to make up 8 digits, with count (' 1 ') to calculate the total number of ' 1 ', is odd when the broken character should be removed.

Then the first bit is ' 1 ' for ' 0 ', which is converted to ascii-code with int (str,2) and finally converted to character. When the first bit is ' 0 ', it is converted directly to characters using encoded-Dec .

Test data:

72 120 226 75 160 246 168 244 119 207 226 48 224 116 101 160 121 53 215 230 182 173 250 212 65 51 115 121 180 160 178 54 2 34 103 89 26 243 119 228 162 102 160 108 57 197 68 240 160 78 64 186 237 160 160 210 71 107 235 57 78 77 105 230 113 231 1 16 160 196 226 107 193 225 205 160 81 238 209 197 16 82 127 215 216 185 160 114 80 57 119 240 160 85 51 111 202 195 119 16 0 233 181 46

The code is as follows:

1data = input (). Split (' ')2Msg_lsit = []3 4  forIinchData:5string = Bin (int (i)) [2:]#Remove prefix ob6     ifString.count ('1')% 2! = 0:#when the number of digits in 1 is odd, the data is wrong, remove7         Continue8     elifString.count ('1')% 2 = = 0:#1 is an even number of digits9String = String.zfill (8)#fill 8-digit numberTen  One         ifString[0] = ='0':#when the first bit is 0 o'clock, convert directly to character Amsg =chr (int (i)) - msg_lsit.append (msg) -         ifString[0] = ='1':#when the first bit is 1 o'clock, convert first to Ascii-code, in the converted to character theNew_string ='0'+ string[1:] -msg = chr (int (new_string, 2)) - msg_lsit.append (msg) - Print("'. Join (msg_lsit)) +  -Output: HXBK vwob0te 5wzta34 26Yswdf l9edp Nm rgk9nmiqgt ba nqewx rp9wp U3OJCW.

2.Quadratic equation (two-time equation)

Description: Now we are going to create a program to solve the two-time equation.

A * x^2 + B * x + C = 0

A, B and C are constants (the "coefficients" of the equation) x is a variable that finds the correct solution of x, for example, coefficients 3,-5 and-2, we have the equation:

3 * x^2-5 * x-2 = 0

We can see that the value of the x=2 is quite appropriate.

The general formula for finding these values (or "roots") by the coefficients of the equations is as follows:

X1 = (-B + sqrt (b^2-4*a*c))/(* * = (-B-SQRT (b^2-4*a*c))/(2*a)

For example, these expressions will produce:

X1 = (5 + sqrt (5^2 + 4*3*2))/(2*3) = (5 + 7)/6 = 2= (5-sqrt (5^2 + 4*3*2))/(2*3) = (5-7)/6 = -1/3

In other words, this equation has two roots. Strictly speaking, there are always two roots here. And the complex number can be solved when i=sqrt (-1).

Input data: The first line contains the number of test cases.

Each test case contains three values (a, B, and C, respectively).

Answer: The root of the two roots should be included (even if they are equal). Use spaces to separate the pairs of values, and use semicolons to separate pairs. The plural should be 5-2i or -1+1.

Please also note that:
The order of the median values is important-for the real root, the output is greater first, and for complex roots, output a+bi and A-bi;

In this task, the root is always expressed in integers, so do not print any decimal points.

For example:

input data:33-3 -61 0 225answer:2-1; 0+1i 0-1i; -5-5

Test data:

166 0-2162-18 167 70 4279-162 7205 30 901 4 689-90 1892-20 488 64-728-160 10882 32 1309 90 813-30 1239 144 13058 4 0-4007-84 595

The code is as follows:

1tast_cases = Int (input ())#number of test cases2 3 ImportMath#To import the math module, you need to use the SQRT module4  forIinchRange (tast_cases):5data =input (). Split ()6A =Int (data[0])7B = Int (data[1])8C = Int (data[2])9M = b**2-4*a*CTen     ifM >=0: OneX1 = Round ((math.sqrt (M)-B)/(A)) AX2 = Round ((-math.sqrt (M)-B)/(A)) -     Else: -         ifRound (MATH.SQRT (ABS (M))/(2*a)) >0: theX1 ='{}+{}i'. Format (Round (-b/(2*a)), round (MATH.SQRT (ABS (M))/(A))) -X2 ='{}{}i'. Format (Round (-b/(2*a)), round (-MATH.SQRT (ABS (M))/(A))) -         Else: -X1 ='{}{}i'. Format (Round (-B/(2 * A)), round (MATH.SQRT (ABS (M))/(2 *A))) +X2 ='{}+{}i'. Format (Round (-B/(2 * A)), round (-MATH.SQRT (ABS (M))/(2 *A))) -     Print(('{} {}'. Format (X1, X2)), end=';') +  AOutput: 6-6; 8 1; -5+6i-5-6i; 10 8; -3+3i-3-3i; -2+8i-2-8i; 7 3; 6 4; 1-9; 10+6i 10-6i; -8+1i-8-1i; -1-9; 5+4i 5-4i; -8+9i-8-9i; 5-10; 6+7i 6-7i;

Algorithm Learning (ix)

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.