设为首页 收藏本站
查看: 440|回复: 0

[经验分享] Python解决codeforces ---- 6

[复制链接]

尚未签到

发表于 2017-5-1 07:51:34 | 显示全部楼层 |阅读模式
  

  第一题 16A


A. Flag

time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output



[size=1em]
According to a new ISO standard, a flag of every country should have a chequered fieldn × m, each square should be of one of 10 colours, and the
flag should be «striped»: each horizontal row of the flag should contain squares of the same colour, and the colours of adjacent horizontal rows should be different. Berland's government asked you to find out whether their flag meets the new ISO standard.



Input

[size=1em]
The first line of the input contains numbersnandm(1 ≤ n, m ≤ 100),n
the amount of rows,m— the amount of columns on the flag of Berland. Then there follows the description of the flag: each of the followingnlines
containmcharacters. Each character is a digit between0and9,
and stands for the colour of the corresponding square.



Output

[size=1em]
OutputYES, if the flag meets the new ISO standard, andNOotherwise.



Sample test(s)

input

3 3
000
111
222



output

YES



input

3 3
000
000
111



output

NO



input

3 3
000
111
002



output

NO

















  

  题意:给定一个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

A. Noldbach problem

time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output



[size=1em]
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.
[size=1em]
Two prime numbers are called neighboring if there are no other prime numbers between them.
[size=1em]
You are to help Nick, and find out if he is right or wrong.



Input

[size=1em]
The first line of the input contains two integersn(2 ≤ n ≤ 1000)
andk(0 ≤ k ≤ 1000).



Output

[size=1em]
OutputYESif at leastkprime numbers from2toninclusively
can be expressed as it was described above. Otherwise outputNO.



Sample test(s)

input

27 2



output

YES


input

45 7



output

NO






Note

[size=1em]
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


A. Triangle

time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output



[size=1em]
At a geometry lesson Bob learnt that a triangle is called right-angled if it is nondegenerate and one of its angles is right. Bob decided to draw such a triangle immediately: on a sheet of paper he drew three points with integer coordinates, and joined them
with segments of straight lines, then he showed the triangle to Peter. Peter said that Bob's triangle is not right-angled, but isalmostright-angled: the triangle itself is not right-angled,
but it is possible to move one of the points exactly by distance 1 so, that all the coordinates remain integer, and the triangle become right-angled. Bob asks you to help him and find out if Peter tricks him. By the given coordinates of the triangle you should
find out if it is right-angled, almost right-angled, or neither of these.



Input

[size=1em]
The first input line contains 6 space-separated integersx1, y1, x2, y2, x3, y3
coordinates of the triangle's vertices. All the coordinates are integer and don't exceed 100 in absolute value. It's guaranteed that the triangle is nondegenerate, i.e. its total area is not zero.



Output

[size=1em]
If the given triangle is right-angled, outputRIGHT, if it is almost right-angled, outputALMOST,
and if it is neither of these, outputNEITHER.



Sample test(s)

input

0 0 2 0 0 1



output

RIGHT



input

2 3 4 5 6 6



output

NEITHER



input

-1 0 2 0 0 1



output

ALMOST



















  题意:给定三个点,如果这三个点能够组成直角三角形输出"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 -= 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 += 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 += 1
return False
a,b,c = getDis(list)
if (a+b == c):
return True
list += 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 -= 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 -= 1
return False
a,b,c = getDis(list)
if (a+b == c):
return True
list -= 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()


  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-371433-1-1.html 上篇帖子: python 发个邮件 下篇帖子: Python解决codeforces ---- 4
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表