第一题 16A
题意:给定一个n*m的矩阵,现在要判断这个矩阵是否满足“同一行的所有元素相同,相邻行之间是不同的”
思路:直接暴力枚举
代码:
# input
n , m = map(int , raw_input().split())
def getAns():
ans = "YES"
for i in range(n):
str = raw_input()
for j in range(1 , len(str)):
if str[0] != str[j]:
ans = "NO"
if i > 0 and pre == str:
ans = "NO"
pre = str
return ans
print getAns()
第二题 17A
Nick is interested in prime numbers. Once he read aboutGoldbach problem. It states that every even integer greater than2can be expressed as the sum of two primes. That got Nick's attention and he decided to invent a problem of his own and call itNoldbach problem. Since Nick is interested only in prime numbers, Noldbach problem states that at leastkprime numbers from2toninclusively can be expressed as the sum of three integer numbers: two neighboring prime numbers and1. For example,19=7+11+1, or13=5+7+1.
Two prime numbers are called neighboring if there are no other prime numbers between them.
You are to help Nick, and find out if he is right or wrong.
The first line of the input contains two integersn(2 ≤ n ≤ 1000) andk(0 ≤ k ≤ 1000).
OutputYESif at leastkprime numbers from2toninclusively can be expressed as it was described above. Otherwise outputNO.
27 2
YES
45 7
NO
In the first sample the answer isYESsince at least two numbers can be expressed as it was described (for example, 13 and 19). In the second sample the answer isNOsince it is impossible to express 7 prime numbers from 2 to 45 in the desired form.
题意:给定一个n和k,现在要判断2~n之间的数满足以下两个条件的个数是否大于等于k。1 数是质数 2 这个数 = 两个相邻的质数+1
思路:先暴力求出1~1100之间的所有质数,然后暴力从3~n直接去求出符合的个数再和k比较
代码:
from math import sqrt
# input
n , k = map(int , raw_input().split())
prime = []
# judge is prime
def isPrime(x):
for i in range(2,int(sqrt(x))+1):
if x%i == 0:
return False
return True
# get 1~1000 prime number
def getPrime():
for i in range(3,1100):
if isPrime(i):
prime.append(i)
# isok
def isOk(x):
if prime.count(x) != 0:
for j in range(prime.index(x)+1):
if x-1 == prime[j]+prime[j+1]:
return True;
return False
# get ans
def getAns():
cnt = 0
getPrime()
for i in range(3,n+1):
if isOk(i):
cnt += 1
if cnt >= k:
return "YES"
return "NO"
print getAns()
第三题 18A
题意:给定三个点,如果这三个点能够组成直角三角形输出"RIGHT",如果不能组成直角三角形但是我们可以移动某一个点到和它距离为1的位置,如果新的三个点能够组成三角形输出"ALMOST",其它所有情况全部输出"NEITHER"
思路:直接暴力枚举判断,但是要注意构成直角三角形的条件,已经构成三角形的条件
代码:
from math import sqrt
# input
input_list = map(int , raw_input().split())
# getDis
def getDis(point_list):
x1,y1,x2,y2,x3,y3 = point_list
list = []
list.append((x1-x2)**2+(y1-y2)**2)
list.append((x1-x3)**2+(y1-y3)**2)
list.append((x3-x2)**2+(y3-y2)**2)
list.sort()
return list
def isOk(list , i):
list[i] -= 1
# 如果有相同的两个点,直接返回false
if (list[0] == list[2] and list[1] == list[3]) or (list[0] == list[4] and list[1] == list[5]) or (list[2] == list[4] and list[3] == list[5]):
list[i] += 1
return False;
# 如果三个点在同一条直线,直接返回false
if (list[0] == list[2] and list[2] == list[4]) or (list[1] == list[3] and list[3] == list[5]):
list[i] += 1
return False
a,b,c = getDis(list)
if (a+b == c):
return True
list[i] += 2
# 如果有相同的两个点,直接返回false
if (list[0] == list[2] and list[1] == list[3]) or (list[0] == list[4] and list[1] == list[5]) or (list[2] == list[4] and list[3] == list[5]):
list[i] -= 1
return False;
# 如果三个点在同一条直线,直接返回false
if (list[0] == list[2] and list[2] == list[4]) or (list[1] == list[3] and list[3] == list[5]):
list[i] -= 1
return False
a,b,c = getDis(list)
if (a+b == c):
return True
list[i] -= 1
return False
# getAns
def getAns():
a,b,c = getDis(input_list)
# judge
if (a+b == c):
return "RIGHT"
# else
for i in range(6):
if isOk(input_list , i):
return "ALMOST"
return "NEITHER"
# output
print getAns()
本文解析了三道编程题目,包括旗子问题、Noldbach问题及三角形问题,提供了详细的解题思路与代码实现。
991

被折叠的 条评论
为什么被折叠?



