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

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

[复制链接]

尚未签到

发表于 2017-4-29 10:19:37 | 显示全部楼层 |阅读模式
  

  第一题 13A


A. Numbers

time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard output



[size=1em]
Little Petya likes numbers a lot. He found that number 123 in base 16 consists of two digits: the first is 7 and the second is 11. So the sum of digits of 123 in base 16 is equal to 18.
[size=1em]
Now he wonders what is an average value of sum of digits of the numberAwritten in all bases from2toA - 1.
[size=1em]
Note that all computations should be done in base 10. You should find the result as an irreducible fraction, written in base 10.



Input

[size=1em]
Input contains one integer numberA(3 ≤ A ≤ 1000).



Output

[size=1em]
Output should contain required average value in format «X/Y», whereXis
the numerator andYis the denominator.



Sample test(s)

input

5



output

7/3



input

3



output

2/1







Note

[size=1em]
In the first sample number 5 written in all bases from 2 to 4 looks so: 101, 12, 11. Sums of digits are 2, 3 and 2, respectively.













  题意:给定一个数n,求2~(n-1)进制的所有数的位数总和/n-2,输出的格式应该是最简
  思路:直接暴力求解
  代码:


#!/bin/python
n = int(raw_input())
# getAns
def gcd(a , b):
if b == 0:
return a
return gcd(b , a%b)
sum = 0
i = 2
while i < n:
tmp = n
while tmp:
sum += tmp%i
tmp /= i
i += 1
# output
print "%d/%d" % (sum/gcd(sum,n-2) , (n-2)/gcd(sum,n-2))


  

  第二题 14A


A. Letter

time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard output



[size=1em]
A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet withnrows andmcolumns.
Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants
to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain
all the shaded squares. The rectangle's sides should be parallel to the sheet's sides.



Input

[size=1em]
The first line of the input data contains numbersnandm(1 ≤ n, m ≤ 50),n
amount of lines, andm— amount of columns on Bob's sheet. The followingnlines
containmcharacters each. Character «.» stands for a
non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square.



Output

[size=1em]
Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better.



Sample test(s)

input

6 7
.......
..***..
..*....
..***..
..*....
..***..



output

***
*..
***
*..
***



input

3 3
***
*.*
***



output

***
*.*
***

















  题意:给定一个n*m矩形矩形的每一个元素是*或者.,由于这个矩形可能会有很多的没用的.,因此要求把这个矩形话到最简

  思路:直接求出左上角和又上角,然后输出即可
  代码:
  

#!/bin/python
# input
n,m = map(int , raw_input().split())
mat = []
for i in range(n):
mat.append(raw_input())
# getx1
def getx1():
for i in range(n):
for j in range(m):
if mat[j] == '*':
return i
# gety1
def gety1():
for i in range(m):
for j in range(n):
if mat[j] == '*':
return i
# getx2
def getx2():
i = n-1
while i >= 0:
j = m-1
while j >= 0:
if mat[j] == '*':
return i
j -= 1
i -= 1
# gety2              
def gety2():
j = m-1
while j >= 0:
i = n-1
while i >= 0:
if mat[j] == '*':
return j
i -= 1
j -= 1
# output
x1 = getx1()
x2 = getx2()
y1 = gety1()
y2 = gety2()
for i in range(x1,x2+1):
print mat[y1:y2+1]




  

  第三题 15A

A. Cottage Village

time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output



[size=1em]
A new cottage village called «Flatville» is being built in Flatland. By now they have already built in «Flatville»nsquare houses with the centres on theОx-axis.
The houses' sides are parallel to the coordinate axes. It's known that no two houses overlap, but they can touch each other.
[size=1em]
The architect bureau, where Peter works, was commissioned to build a new house in «Flatville». The customer wants his future house to be on theОx-axis,
to be square in shape, have a sidet, and touch at least one of the already built houses. For sure, its sides should be parallel to the coordinate axes,
its centre should be on theOx-axis and it shouldn't overlap any of the houses in the village.
[size=1em]
Peter was given a list of all the houses in «Flatville». Would you help him find the amount of possible positions of the new house?



Input

[size=1em]
The first line of the input data contains numbersnandt(1 ≤ n, t ≤ 1000).
Then there follownlines, each of them contains two space-separated integer numbers:xiai,
wherexix-coordinate
of the centre of thei-th house, andai
length of its side ( - 1000 ≤ xi ≤ 1000,1 ≤ ai ≤ 1000).



Output

[size=1em]
Output the amount of possible positions of the new house.



Sample test(s)

input

2 2
0 4
6 2



output

4



input

2 2
0 4
5 2



output

3



input

2 3
0 4
5 2



output

2







Note

[size=1em]
It is possible for thex-coordinate of the new house to have non-integer value.


  
  题意:有一个人想要建房子,这些房子都是建在x轴上面的,现在已经有n个房子是建好的。给定新建的房子的长为t,问总共能在几个位置建新房子
  思路:利用字典保存n个已经建好的房子的位置,然后枚举一遍即可
  代码:



# input
n,t = map(int , raw_input().split())
dict = {}
for i in range(n):
x,a = map(int , raw_input().split())
dict[x] = a
# getAns
ans = 2
list = dict.keys()
list.sort()
pre = -(1<<30)
for key in list:
a = float(dict[key])/2
if pre != -(1<<30):
dis = abs((key-a)-pre)
if dis > t:
ans += 2
elif dis == t:
ans += 1
pre = key+a
# output
print ans

运维网声明 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-370674-1-1.html 上篇帖子: Python-内置数据类型3 下篇帖子: python对XML 操作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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