第一题 7A
题意:一个8*8的平板初始化每个点的颜色为白色,现在可以给平板涂色,但是每次只能够涂一行或者一列,现在给定已经涂好的平板,问最少需要涂几次
思路:直接暴力枚举第一行和第一列判断即可,但是要注意如果整个都是平板都是黑色只要8次
代码:
# input
matrix = []
for i in range(8):
matrix.append(raw_input())
# getAns
ans = 0
# row
for i in range(8):
if matrix[i][0] == 'B':
for j in range(8):
if matrix[i][j] == 'W':
break
if j == 7 and matrix[i][j] == 'B':
ans += 1
# col
for i in range(8):
if matrix[0][i] == 'B':
for j in range(8):
if matrix[j][i] == 'W':
break
if j == 7 and matrix[j][i] == 'B':
ans += 1
if ans == 16:
ans = 8
print ans
第二题 8A
Peter likes to travel by train. He likes it so much that on the train he falls asleep.
Once in summer Peter was going by train from city A to city B, and as usual, was sleeping. Then he woke up, started to look through the window and noticed that every railway station has a flag of a particular colour.
The boy started to memorize the order of the flags' colours that he had seen. But soon he fell asleep again. Unfortunately, he didn't sleep long, he woke up and went on memorizing the colours. Then he fell asleep again, and that time he slept till the end of the journey.
At the station he told his parents about what he was doing, and wrote two sequences of the colours that he had seen before and after his sleep, respectively.
Peter's parents know that their son likes to fantasize. They give you the list of the flags' colours at the stations that the train passes sequentially on the way from A to B, and ask you to find out if Peter could see those sequences on the way from A to B, or from B to A. Remember, please, that Peter had two periods of wakefulness.
Peter's parents put lowercase Latin letters for colours. The same letter stands for the same colour, different letters — for different colours.
The input data contains three lines. The first line contains a non-empty string, whose length does not exceed105, the string consists of lowercase Latin letters — the flags' colours at the stations on the way from A to B. On the way from B to A the train passes the same stations, but in reverse order.
The second line contains the sequence, written by Peter during the first period of wakefulness. The third line contains the sequence, written during the second period of wakefulness. Both sequences are non-empty, consist of lowercase Latin letters, and the length of each does not exceed 100 letters. Each of the sequences is written in chronological order.
Output one of the four words without inverted commas:
- «forward» — if Peter could see such sequences only on the way from A to B;
- «backward» — if Peter could see such sequences on the way from B to A;
- «both» — if Peter could see such sequences both on the way from A to B, and on the way from B to A;
- «fantasy» — if Peter could not see such sequences.
atob a b
forward
aaacaaa aca aa
both
It is assumed that the train moves all the time, so one flag cannot be seen twice. There are no flags at stations A and B.
题意:给定一个字符串,在给定两个子串,问两个子串存在于给定字符串是4种情况中的哪一种
思路:暴力判断
代码:
# input
normal_str = raw_input()
reverse_str = normal_str[::-1]
seq_one = raw_input()
seq_two = raw_input()
length = len(normal_str)
len_seq_one = len(seq_one)
len_seq_two = len(seq_two)
# judge
def isOk(str):
global seq_one
global seq_two
length = len(normal_str)
len_seq_one = len(seq_one)
len_seq_two = len(seq_two)
i = 0
while i < length:
if i+len_seq_one < length and str[i:i+len_seq_one:] == seq_one:
j = i+len_seq_one
while j < length:
if j+len_seq_two <= length and str[j:j+len_seq_two:] == seq_two:
return True
j += 1
i += 1
return False
# solve
forward = isOk(normal_str)
backward = isOk(reverse_str)
# output
if forward and backward:
print "both"
elif forward and backward == False:
print "forward"
elif forward == False and backward:
print "backward"
else:
print "fantasy"
第三题 9A
Yakko, Wakko and Dot, world-famous animaniacs, decided to rest from acting in cartoons, and take a leave to travel a bit. Yakko dreamt to go to Pennsylvania, his Motherland and the Motherland of his ancestors. Wakko thought about Tasmania, its beaches, sun and sea. Dot chose Transylvania as the most mysterious and unpredictable place.
But to their great regret, the leave turned to be very short, so it will be enough to visit one of the three above named places. That's why Yakko, as the cleverest, came up with a truly genius idea: let each of the three roll an ordinary six-sided die, and the one with the highest amount of points will be the winner, and will take the other two to the place of his/her dreams.
Yakko thrown a die and gotYpoints, Wakko —Wpoints. It was Dot's turn. But she didn't hurry. Dot wanted to know for sure what were her chances to visit Transylvania.
It is known that Yakko and Wakko are true gentlemen, that's why if they have the same amount of points with Dot, they will let Dot win.
The only line of the input file contains two natural numbersYandW— the results of Yakko's and Wakko's die rolls.
Output the required probability in the form of irreducible fraction in format «A/B», whereA— the numerator, andB— the denominator. If the required probability equals to zero, output «0/1». If the required probability equals to 1, output «1/1».
4 2
1/2
Dot will go to Transylvania, if she is lucky to roll 4, 5 or 6 points.
题意:三个人掷筛子,前面两个人扔出了y和w,问第三个人仍的分数x是最大的概率,输出的格式一定要按照A/B
思路:求出x的可能值,然后直接输出,注意要最简形式
代码:
# input
list = raw_input().split()
x = max(int(list[0]) , int(list[1]))
def gcd(a , b):
if b == 0:
return a
return gcd(b , a%b)
a = 6-x+1
b = 6
print "%d/%d" % (a/gcd(a,b) , b/gcd(a,b))