2017 School recruit National Unified simulated written Examination (fourth) programming questions set--python

Source: Internet
Author: User
Tags repetition

Note: The question comes from the cattle net 1. cattle have n cards in a sequence. Each card is black on one side and white on the other. In the initial state, some cards are black facing up, and some cards are white facing up. Cows now want to turn some of the cards over and get an alternate form, that is, the color of each adjacent card is different. Niuniu want to know how many cards you need to flip at least to turn into alternating permutations.
Enter a description:

The input includes a string s, length of string (3≤LENGTH≤50), which contains only ' W ' and ' B ' strings, representing white and black, respectively. The entire string represents the initial state of the card sequence.


Output Description:
Output An integer that indicates the maximum number of times a bull needs to be flipped.

Enter Example 1:
Bbbw

Output Example 1:
1

Personal thinking:There are two ideas 1. According to the first place, get the right arrangement out. Then compare the string traversal with the input and get the result
2. Set a flag = BW and flag = WB, and sequentially two characters by slice, compared, not identical, Count = 1. Finally take the minimum value of two The correct answer (i && niu net Id:liberty_zheng):
flag1, Flag2, Flag3, Flag4, flag5, Flag6 = ' BW ', ' WB ', ' BB ', ' WW ', ' B ', ' W ' def bw_start (arr): I
            , j = 0, 2 bw = 0 while J <= Len (arr) +2:temp = '. Join (Arr[i:j]) If temp = Flag1:  i = 2 J = 2 Continue elif temp = FLAG2:BW + 2 Elif temp = Flag3 or temp = = FLAG4:BW = 1 Elif temp = FLAG6:BW + = 1 I + = 2 J + + 2 R
        Eturn bw def wb_start (arr): I, j = 0, 2 wb = 0 while J < Len (arr): temp = '. Join (ARR[I:J))  if temp = = Flag2:i = 2 J = 2 Continue elif temp = = FLAG1:WB
        + + 2 Elif temp = FLAG3 or temp = FLAG4:WB = 1 Elif temp = FLAG5:WB + 1 
i + 2 j = 2 return WB string = Raw_input () arr = list (string) result = (Bw_start (arr), Wb_start (arr)) Print min (result) 
I used the second idea, that is, flag a little bit more.
Data=raw_input ()
counts1=0
counts2=0 for
i in range (len (data):
    if i%2==0:
        if data[i]== ' B ':
            continue
        Else:
            counts1+=1
    Else:
        if data[i]== ' W ':
            continue
        else:
            counts1+=1
Print min (counts1,len (data)-counts1)
Online students are using the equivalent of the first train of thought


2.Cows and cows become blackened and want to destroy the Earth. But he forgot the password to unlock the Earth Destroyer. Niuniu has a string in his hand. S, cows remember to remove a character from S is exactly the correct password, please help Niu Niu to find out how many times he needs to try the password.
As the example shows S = "ABA", 3 possible passwords are "BA", "AA", "AB".
When s = "A", the only password an ox can try is an empty password, so the output is 1.
Enter a description:
The input includes a string s, length of string (1≤LENGTH≤50), which are all uppercase letters from ' A ' to ' Z '.


Output Description:
Output an integer indicating the maximum number of password attempts to be attempted by cattle.

Enter Example 1:
ABA

Output Example 1:
3

Personal thinking:My idea is too simple, the understanding of the problem is that a string to remove a character after all the permutations and combinations. I used Python's powerful library itertools.permutations (arr, num) for a minute to see the permutations and combinations. But the result, the memory of the students to see the idea, if the adjacent two strings the same, then remove any one, the effect is the same without repetition, the number is the length of the string. This sentence is the key. correct answer (cattle net Id:liberty_zheng):
Data=raw_input ()
Count=len (data) for
I in range (1,len (data):
    if data[i]==data[i-1]:
        count-=1
Print Count
It's just that I'm too dependent on something.


3.Niuniu feeds on forage. Cattle and cows one day in turn encountered n heap of magic of forage, cattle as long as he met a pile of grass and his current size, it will eat up the grass, and make their size expanded by one times. At first, the size of a cow is a, and then the size of the N-pile forage that cattle and cows encounter in turn is given. Please calculate the final size of the cattle and cows.
Enter a description:
The input consists of two rows, the first row contains two integers n and a (1≤n≤200, 1≤a≤1,000,000,000) The
second row consists of n integers, indicating the size of the forage heap that cows encounter in turn a_i (1≤a_i≤1,000,000,000)


Output Description:
Output An integer representing the final size of the cow and ox.

Enter Example 1:
5 1
2 1 3 1 2

Output Example 1:
4

Personal thinking:A very simple question, meet the same doubling can be. correct answer (i):
AB = [Int (i) for I, Raw_input (). Strip (). Split ()]
arr = [Int (i) for I in Raw_input (). Strip (). Split ()]
A = ab[1]
  for i in range (Ab[0]):
    if arr[i] = = A:
        a = a * 2
print a


4.Cattle have an integer sequence of n, which is to select two different positions in the cattle sequence, and then swap the elements on both positions. Now we need to find out the number of different sequences that can be obtained after the sheep exchange. (Note that the two element values that are exchanged may be the same).
such as sequence {1, 47}, Output 1. Sheep must exchange only two elements to get the sequence {47, 1}. Sheep and goats must be exchanged and cannot retain the original sequence.
{1, 2, 1}, Output 3. Sheep and goats by Exchange can get {2, 1, 1},{1, 1, 2},{1, 2, 1} These three sequences.
Enter a description:
The input consists of two lines, the first behavior an integer n (2≤n≤50), i.e. the length of the sequence. The
second row n integers that represent the a_i (1≤a_i≤50) of each element of the sequence, separated by a space.


Output Description:
Output An integer that indicates the number of different sequences that a goat can get

Enter Example 1:
3
1 2 1

Output Example 1:
3

Personal thinking:Rare this question I actually used the mathematics to look for the law, thought also did not want to put on to find the law directly, 40% case
n = Int (raw_input ())
arr = [Int (i) for I and Raw_input (). Strip (). Split ()] result
= n * (n-1)/2
Then a look, I was writing to repeat the question. However, I have considered the question of repetition and have set the rules. Just blinded by the rules and tests.
correct answer (cattle net Id:saharayu):
n = Int (raw_input ())
a_i = Map (Int,raw_input (). Split ())
m = 0
If Len (set (a_i)) = = 1:
    print 1
else:< C5/>for I in range (n): for
        J in Range (I+1,n):
            if a_i[i] = = A_i[j]:
                m = m + 1
    if M!= 0:
        print n (n-1) /2-m + 1
    else:
        print n (n-1)/2
Find the number of repetitions, and finally-M + 1.


5.Niuniu likes strings, but he hates ugly strings. For Lais, the ugly value of a string is the number of identical consecutive character pairs in the string. For example, the ugly value of the string "ABABAABBB" is 3 because there is a pair of "AA" and two pairs of overlapping "BB". Now give a string that contains the characters ' A ', ' B ' and '? '. Cattle cows can now change the question mark in the string to ' A ' or ' B '. Niu Niu now wants to minimize the ugly value of the string, and I hope you can help him.
Enter a description:
The input includes a string s, length of string (1≤LENGTH≤50), and the string contains only ' a ', ' B ', '? ' Three of characters.


Output Description:
Output An integer that represents the smallest ugly value

Enter Example 1:
A? A

Output Example 1:
0

Personal thinking:Also thought of judgment. Before and after AB, and then how to operate. But I was hindered by myself again. Finally no longer think. The correct answer (cow Net ID: Bug amuse you to play):
Import sys 
str = raw_input ()
arr = list (str)
length = Len (arr)
count = 0 for
i in range (1,len (arr)): 
  if Arr[i] = = "?": 
        if arr[i-1] = = "a":
            arr[i] = "B"  
        elif arr[i-1] = = "B":
            arr[i] = "A" for    
 
I In Range (0,length-1):
    if arr[i] = = arr[i+1] and arr[i]!= "?":
        count = count + 1  
This idea is simple ah, do not think too much. The previous one of the question mark to set. Value, and finally to determine the ugly value. But how do you determine the smallest one? Perhaps there is no need to consider the smallest.
6.Niu Jia Zhuang Kindergarten celebrates 61 children's Day with a program in which children dance in a circle. Niu Teacher picked out n a little friend to participate in the dance program, known to each child's height h_i. In order to make the dance look harmonious, the cow teacher needs to let the dancing circle formation the neighbor child's height difference maximum value is smallest, the cow teacher makes the difficulty, hoped you can help him.
As shown in the sample example:
When the Circle line is 100,98,103,105 clockwise, the maximum height difference is 5, and the other arrangement does not get a better solution.
Enter a description:
The input consists of two lines, the first act a positive integer n (3≤n≤20) The
second behavior n integers h_i (80≤h_i≤140), representing the height of each child.


Output Description:
Output An integer representing the maximum height difference of the adjacent children under the condition.

Enter Example 1:
4
100 103 98 105

Output Example 1:
5

Personal thinking:The idea is too simple to think of arrays of permutations and combinations to list the situation next year. Not to think, what kind of arrangement can be asked. After the order, a word: from high to low diffusion, that is the answer
The correct answer (cow Net ID: Bug amuse you to play):
n = Int (raw_input ()) 
a = [Int (s) for S-raw_input (). Split ()] 
a.sort ()
max_v = 0 
for i in range (2, N): 
  v = A[i]-a[i-2] 
    if v > max_v: 
        max_v = v 
print Max_v
So, to transform and know, is to find the difference between the order after the interval of 2.

7.There is an infinite length of paper tape, divided into a series of lattice, at the beginning of all the initial lattice is white. Now put on a lattice of a Meng Meng robot (put on the lattice will also be dyed red), once the robot went to a lattice, it will paint the lattice red. Now given an integer n, the robot will now take n steps on the paper tape. Each step, the robot will go to the left or to the right of a lattice, the two cases of equal probability. All random choices made by a robot are independent. Now you need to calculate the expected value of the red plaid on the last paper tape.
Enter a description:
The input includes an integer n (0≤n≤500), which is the number of steps the robot walks.


Output Description:
Outputs a real number that represents the desired count of the red lattice and retains a decimal.

Enter Example 1:
4

Output Example 1:
3.4

Personal thinking:See the expectations, but also specifically to search for the expectations of how to ask. Sure enough, the world is all math. Students are asked to review their own expectations. correct answer (cow Net ID: Strawberry ice cream):
def main ():
    n = Int (raw_input ())
    print round (trans (n), 1)
 
 
def trans (n):
    if n = 0: return
        1.0
    elif n = = 1: return
        2.0;
    elif n%2 = = 1: return
        trans (n-1) * (2*n)/(2*n-1);
    elif n%2 = = 0: return
        trans (n-1) * (2*n+1)/(2*n);
 
Main ()
with recursion. My problem now is that I don't know how the recursion ends.


8.Niuniu kept n cows on the farm, sequentially numbered 0 to n-1, Niu Niu's good friend sheep and sheep to help New look at the farm. One day the goat saw the farm escape K-cow, but he would only tell the cattle to escape the K-cow number and can be divisible by N. You need to help cattle and cows figure out how many different groups of cows are escaping. Because the result can be very large, the output result is 1,000,000,007 modulo.
For example n = 7 k = 4:
7 cows numbered 0 to 6 in sequence, and fled 4.
Numbers and 7 are: {0, 1, 2, 4}
Numbers and 14 are: {0, 3, 5, 6}, {1, 2, 5, 6}, {1, 3, 4, 6},{2, 3, 4, 5}
4 cattle are numbered and will not be greater than 18, so output is 5.
Enter a description:
The input includes a row, two integers n and K (1≤n≤1000), (1≤k≤50), separated by a space.


Output Description:
Output An integer representing the number of species to ask for the caption.

Enter Example 1:
7 4

Output Example 1:
5

Personal thinking:The question is very understood very good understanding, but also has the big data problem. Then looked at the following students thought, a bitter tears correct answer (cattle net ID: New Ket 4808019):
 #include <iostream> using namespace std; const int MOD=1E9+7;/See the Code and comments of the great God upstairs, and write a little vulgar view. This is through the form of state compression, the initial form should be dp[i][j][s], the meaning is to take the number of J from [0,i], so that their and modulo n of the remainder of S, so that the number of J number//state transfer equation for the set of dp[i][j][s]=dp[i-1][
j][s]+dp[i-1][j-1][(S-i+n)%n]; The transfer process is for the number I, take or not take, if not taken, then the number of schemes and i-1 when the number of the same//if I, then divided into two situations, the first situation is i<=s, at this time we need to ask the previous i-1 number, take j-1 number, So that the remainder of their and modulo n is s-i, so that when you join I, and modulo n is S//The second case is I>s, then s-i is a negative number, noting that the subject requires a multiple of the composition and the N, and that is, all modulo n is 0, so in this case (S-i)%n is represented (s-i+n
)%n can meet the requirements because ((s-i+n)%n+i)%n=s;
This formula is also true for the first case, so it's good to be together with int dp[55][1005];
    int main () {int n,k;
        while (cin>>n>>k) {dp[0][0]=1; for (int i=0;i<n;i++) {for (int j=k;j>=1;j--)//state compression, just like 01 backpacks {for (i
                NT s=0;s<n;s++) {dp[j][s]= (dp[j][s]+dp[j-1][(n+s-i)%n])%mod;
}} cout<<dp[k][0]<<endl;//the number of all sets that have a remainder of 0 for their and modulo n by the last output} return 0; }


I am a little little bird.


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.