Algorithm Learning (7), algorithm Learning

Source: Internet
Author: User

Algorithm Learning (7), algorithm Learning
1. Bulls and Cows

Note: This is a two-person game, usually played with paper and pen. The modern version is also known as the "planner ".

For the first player, Alice, select a four-digit password (1492). All the numbers are different.

The second player, Barbara, tried to guess a password. She can provide a combination of any 4-digit digits (no need to repeat)-And Alice should give a prompt each time she tries.

This prompt consists of two values:

The first number tells us how many digits are correctly guessed and where they are;

The second method is to correctly guess the number of digits, but at the wrong position.

For example, if the secret value is 1492 and Barbara's guess is 2013, Alice should use 0-2 to prompt.

If the prediction is 1865, the prompt is 1-0.

This game can also be played with 4-letter words, but in this case, it may require language knowledge.

Problem description: Your goal is to write a program to calculate these values, and a prompt is returned by the speculative value.

Example:

input data:1492 52013 1865 1234 4321 7491answer:0-2 1-0 1-2 0-3 2-1

Input dataIt will contain the first line of the secret value and the number of estimated values.

The second row contains a specified number of guesses.

AnswerThe answer should include the prompts Of These guesses, which should be given in x-y format and separated with spaces.

Test data:

0197 111796 0869 2140 9210 9624 6417 7208 4867 8902 8971 2164

The Code is as follows:

1 data1 = input (). split () 2 correct_num = list (data1 [0]) # Secret value, converted to list form 3 guess_nums = int (data1 [1]) # Number of prediction values 4 data2 = input (). split () 5 6 for n in data2: 7 guess = list (n) 8 first = 0 9 two = 010 for m in range (4 ): 11 if guess [m] = correct_num [m]: # compare the number of guesses. in the correct position, 12 first + = 113 elif guess [m] in correct_num: # number of digits to guess 14 two + = 115 print ('{}-{}'. format (first, two), end = '') 16 17 output: 1-2 1-1 1-1 0-3 0-1 1-1 0-2 1-0 0-2 0-3 1-0

 

2. Two Printers (Two printer problems)

Note: now we have a commercial agreement-we need to print a file containing N pages.

There are two printers, but it seems that the printer is working at different speeds. One page is printed within X seconds, and the other is printed within Y seconds.

The problem now is that it takes the least time to print the entire document with two printers.

Example:

input data:21 1 53 5 4answer:3 9

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

Then, each test case is in a separate row.

Each test case contains three integers, x y n, where N does not exceed 1 billion.

Answer should contain the printing time of each test case, separated by spaces.

Analysis: How many pages should be printed at the same time?

Input data:

16397 198 7990285 13 6449560111 2 69516005338 900 97262167 62 7644114886 73 974401124 9 639434020 33 152820741 1 61861565582 56 93008691248 427 5243381005 2674 98192121 66 6071269786 486 6010581283455 473995 26997484 57402 6470

The Code is as follows:

1 test_cases = int (input () # Number of test cases 2 for I in range (test_cases): 3 testcase = input (). split () 4 X = int (testcase [0]) 5 Y = int (testcase [1]) 6 N = int (testcase [2]) 7 time = N/(1/X + 1/Y) # convert to the number of x and y that can be printed in one second, and find the approximate time 8 numX = (time // X) + 1 # discuss the problem of printing the last page. X multiple times one page. 9 numY = (time // Y) + 1 # create one more page by Y 10 Ans = round (min (numX * X, numY * Y) # minimum values of the two values when X and Y are multiple times, the minimum time required for printing together is 11 print (Ans, end = '') 12 13 Output: 105560334 232900785 117642472 238991400 246152338 65716717 53654769 190305080 309307828 309489812 166816517 71725845 259278921 180504774 93377015 233766632

 

Related Article

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.