Algorithm Learning (13), algorithm Learning

Source: Internet
Author: User

Algorithm Learning (13), algorithm Learning
1. Funny Words Generator

Note: compile a program to generate a group of interesting words.

Follow these steps:

1. There are any number of letters in a word, but the letters are in an odd number (1, 3, 5 ,......) It should be a consonants, and the even numbers of letters (2, 4, 6 ,......) It should be a vowel.

2. It is required that the consonants be bcdfghjklmnprstvwxz and that the vowels be aeiou (note that q and y are skipped ).

3. Use parameters to implement A simple linear coequal generator. parameters A = 445, C = 700001, M = 2097152, and the initial value X0 are used as input data (I .e. seeds ). Formula:Xnext = (A * Xcur + C) % M

4. to generate a word consisting of N letters, use the random number generated by this generator as the initial value to generate the next random number. For example, when X0 = 0 and N = 4, you will get the four numbers 700001,1821950, 1967079,1537772.

5. convert these random values into letters, index the position of the consonants or 5 vowels, and select a letter from the above string (see step 2 ).

For example, if X0 = 0, we want to generate four letters of words, we have the following calculation:

Random Value Letter Index Letter 700001 700001% 19 = 3 F # f's Index is 3, so it is at the fourth position of the consonants. 1821950 1821950% 5 = 0 A # The index of a is 0, so it is at the first position of the vowel. 1967079 1967079% 19 = 9 M 1537772 1537772% 5 = 2 I

The resulting word is fami.

Input data:The first line contains the number of generated words and the seed value of the random generator X0.
The next line contains the expected word length, which is separated by spaces.
Answer:It should contain the words you generated and are separated by spaces.

For example:

input data:3 04 5 6answer:fami wovaw kelasi

Another example:

input data:4 20149 9 9 9answer:foravanad zibecefeb wagabenip wedivonow

Test data:

23 3449854 3 6 7 6 8 3 8 7 7 4 6 6 8 7 7 8 3 6 8 3 8 6

The Code is as follows:

1 consonant = 'bcdfghjklmnprstvwxz '2 vowels = 'aeiou' 3 4 test_case = input (). split () # Number of test cases 5 6 A = 445 7 C = 700001 8 M = 2097152 9 X = int (test_case [1]) 10 nums = [int (I) for I in input (). split ()] 11 for N in nums: 12 total = [] # Find the generated random number 13 while N> 0: 14 N-= 115 X = (A * X + C) % M 16 total. append (X) 17 letters = [] 18 for I in total: 19 if total. index (I) % 2 = n = I % 1921 letter = consona Nt [n] # index 22 letters. append (letter) 23 elif total. index (I) % 2! = N = I % 525 letter = vowels [n] 26 letters. append (letter) 27 word = ''. join (letters) # merge characters 28 print (word, end = '') 29 30 output: hodu tob vapola cizilah selapi hirugife wam nocofavo buhinut xuweduf suo gon=rinuki votawezu juragah topilin rawotisi nij facgucocada jen lebumuci fuline

 

2. Tic-Tac-Toe

Note: An algorithm of the word game-the check function, which can determine whether the game is over and win by one or the other.

This game is played on square 3x3. Let's assume that these squares are named as numbers 1 to 9:

 1 | 2 | 3---+---+--- 4 | 5 | 6---+---+--- 7 | 8 | 9

The two players turn their tags (X is the first, O is the second) into any remaining number blocks. One Party completes three lines marked (3 x or 3 o) and will win immediately. That is, each column in each row and two diagonal lines win a total of 8. For example, the steps X and O are as follows:

 X   O------- 7     5 4     1 9     2 8

The preceding steps form the following squares:

 O | O |  ---+---+--- X | O |  ---+---+--- X | X | X

The first player (X-word) connects to the first line through the third row to win the game.

  Problem description:

You will get a series of moving sequences (assuming the first step is completed by X), by the number of cells marked to be placed, however, your task is to determine which step has completed the first line (either side can connect to the first line, but it must be completed first ).

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

Each row below has a test case, which is exactly nine numbers and describes the cells executed in order.

Answer: It should include the first step (9 steps from 1 to 9, that is, the output wins in the first step.). If no player wins after the last move (no winner), 0 is output.

For example:

Input data: 37 5 4 1 9 2 8 3 6 # Win in step 5 5 1 3 7 6 4 2 9 8 # Win in step 5 5 1 2 8 6 4 7 3 9 # No one win, output 0 answer: 7 6 0

Test data:

159 7 6 8 5 3 2 1 44 1 5 6 3 8 2 9 71 8 2 5 4 3 7 9 64 8 5 1 9 6 2 3 77 6 9 4 5 2 1 8 38 4 9 5 3 7 1 2 69 3 8 7 6 1 5 2 48 3 9 2 1 4 6 7 51 7 8 9 6 2 5 4 36 1 5 2 4 3 7 9 88 9 1 6 7 4 2 3 53 9 1 7 2 5 4 8 69 5 1 4 8 3 6 7 21 5 4 8 9 3 6 2 76 8 9 3 2 1 4 5 7

The Code is as follows:

1 test_cases = int (input () 2 win = [3 {1, 2, 3}, 4 {4, 5, 6}, 5 {7, 8, 9 }, 6 {1, 4, 7}, 7 {2, 5, 8}, 8 {3, 6, 9}, 9 {1, 5, 9}, 10 {3, 5, 7} 11] 12 # All winning matches location condition 13 14 def ans (Xs, OS): 15 A = [] 16 for I in win: 17 if I & Xs = I or I & OS = I: # if the intersection of the subset and Xs or OS in win is equal to the subset itself, indicates that someone wins the game 18 a = max (data. index (x) for x in I) + 1 # locate the index position of the winning game 19. append (a) 20 if len (A)> 0: 21 print (min (A), end = '') # index 22 else: 23 print (0, end = '') # none of them win 24 25 for _ in range (test_cases): 26 data = [int (m) for m in input (). split ()] 27 Xs = set (data [: 2]) # position of X 28 OS = set (data [1: 2]) # O position 29 ans (Xs, OS) 30 31 output: 9 9 7 0 7 9 8 9 0 5 8 8 8 0

 

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.