Algorithm Learning (8), algorithm Learning
1. Combinations Counting)
Note: We have a combined example: select different methods for several elements from a given set (without considering the sequence ). For example, if the boy has four sweets (different types of sweets) and takes only two of them, and the rest is for his sister, he has the following options:
A B C D - four sorts of candiesA+B, A+C, A+D, B+C, B+D, C+D - six way to choose a pair of them.
The number of K elements in the N set (assuming that all N elements are different ). It is easy to find that the mathematical formula is:
N!------------- = C(N, K) - the number of different combinationsK! * (N - K)!
X! Is the factorial of X, that is, product 1*2*3 · * X
Problem description: You need to calculate the values of N and K in C (N, K). Although some languages (such as Python and Java) have built-in long algorithms, if you can find a way to minimize the intermediate results in the calculation, it will be good. This is sometimes critical to c/c ++.
The input data will contain the number of test cases.
The second row contains a test case. Each test has two values (n k ).
For each case, the answer should include C (N, K ).
Analysis: first construct a factorial function and substitute the formula for solving C (N, K ).
Test data:
789 857 1093 86105 783 7568 8111 7
The Code is as follows:
1 cases = int (input () 2 3 def fact (n): 4 "constructor" 5 if n = 1 or n = 0: 6 return 1 7 elif n> 1: 8 return n * fact (n-1) 9 Ans = [] 10 for I in range (cases): 11 case = input (). split () 12 N = int (case [0]) 13 K = int (case [1]) 14 C = round (fact (N)/(fact (K) * fact (N-K) # substitute the formula for solving 15 Ans. append (C) 16 17 for n in Ans: 18 print (int (n), end = '') # formatted output 19 20 output: 70625252863 43183019880 9473622444 22760723700 39443226966 7392009768
2. Blackjack Counting (count)
Note: Blackjack's game rules are very simple: players collect more points than their opponents, but not more than 21 (refer to the complete Wikipedia rules ). All cards from 2 to 10 in the card are calculated based on their own values, and the value of each card is 10 points for the king, queen, and Jack, the Ace value can be regarded as 1 or 11 points. When the number of points plus 11 points is greater than 21 points, it is clear that Ace should be regarded as 1 point.
Let's learn how to program scoring algorithms for such games.
The first line of the input data contains the number of test cases.
The test cases are in different rows. Each test case consists of several cards represented by symbols:
2 3 4 5 6 7 8 9,
T j q k value is 10, Jack, queen, king,
Ace-
The answer should include the number of points for each test case. The answer should not exceed 21. If the total number is greater than 21 (that is, players lose immediately), the answer will be a failure (Bust ).
For example:
input data:4A T2 K 43 A Q 8A 3 3 3 Aanswer:21 16 Bust 21
Test data:
282 3 J 7A QK 7A KA 4 5A A J 94 3 6 78 6 8A A A K K7 6 52 A T A 76 6 5A 2 T A A AA A 75 7 8K A4 9 TT 7T A5 2 JT 9K 67 T5 AA 7J QA A 6K 7
The Code is as follows:
1 num1 = ['2', '3', '4', '5', '6', '7', '8 ', '9'] 2 num2 = ['T', 'J', 'Q', 'K'] 3 test_cases = int (input ()) # Number of test cases 4 5 for I in range (test_cases): 6 case = input (). split () # each test case 7 points = 0 # The initialization point value is 0 8 ans = [] 9 for n in range (len (case )): 10 if case [n] in num1: 11 points + = int (case [n]) 12 elif case [n] in num2: 13 points + = 1014 elif case [n] = 'A' and points + 11> 21: # When A and are included, the value is greater, A should be counted as 15 points + = 116 elif case [n] = 'A' and points + 11 <= 21: # When A and are included, when the value is not greater than 21 points, A should be counted as points + = 1118 if points> 21: # the total number of points is greater than 21 points, and 19 print ('bust ', end = '') 20 else: # the total number of points is not greater than 21. print the number of points 21 print (points, end ='') 22 23 output: bust 21 17 21 20 Bust 20 Bust 18 Bust 17 Bust 19 20 21 Bust 17 17 19 16 16 18 20 18 17