您的位置:首页 > 编程语言 > Python开发

python 提交题目

2016-01-02 13:59 483 查看
刚学了python,上codeforces上找个简单的不能再简单的题目练习一下如果用python提交题目。

原题:

A. Beautiful Matrix

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

You’ve got a 5 × 5 matrix, consisting of 24 zeroes and a single number one. Let’s index the matrix rows by numbers from 1 to 5 from top to bottom, let’s index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix:

Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≤ i < 5).

Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≤ j < 5).

You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful.

Input

The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one.

Output

Print a single integer — the minimum number of moves needed to make the matrix beautiful.

Sample test(s)

input

0 0 0 0 0

0 0 0 0 1

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

output

3

input

0 0 0 0 0

0 0 0 0 0

0 1 0 0 0

0 0 0 0 0

0 0 0 0 0

output

1

题目大意:

就是给你一个5*5的矩阵,这个矩阵里面只有一个数字1,剩下的全是0。让你交换相邻的两行或者两列,最少交换几次,使得能够把1放到中间的位置 =_=

就是简单的交换就可以了,刚开始交题目的时候出现了re。不明白怎么回事,后来看别人的提交的代码,发现他们用input()函数的时候后面都会加一个split()的函数,其实input()函数会读入一个带空格字符串。这里split()是把读进来的字符串分割成列表,如下所示:

y=input()
print (y)
x=input().split()
print (x)
#输入a b c d
#输入a b c d
#得到的结果分别是a b c d
#             ['a','b','c','d']


此题的代码如下:

import math
for x in range(5):
i=input().split()
if '1' in i:
print (abs(2-x)+abs(2-i.index('1')))


这里一次只读取一行,即5个元素,并且分割成一个列表。

其中i代表一个列表,x代表多少行。i.index(‘1’)表示此行中1所在的下标

第二个题目是一个字符串的题目:

原题

A. Translation

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

The translation from the Berland language into the Birland language is not an easy task. Those languages are very similar: a berlandish word differs from a birlandish word with the same meaning a little: it is spelled (and pronounced) reversely. For example, a Berlandish word code corresponds to a Birlandish word edoc. However, it’s easy to make a mistake during the «translation». Vasya translated word s from Berlandish into Birlandish as t. Help him: find out if he translated the word correctly.

Input

The first line contains word s, the second line contains word t. The words consist of lowercase Latin letters. The input data do not consist unnecessary spaces. The words are not empty and their lengths do not exceed 100 symbols.

Output

If the word t is a word s, written reversely, print YES, otherwise print NO.

Sample test(s)

input

code

edoc

output

YES

input

abb

aba

output

NO

input

code

code

output

NO

题目大意:

就是给你两个字符串,问你第二个字符串是不是第一个颠倒过来的。=_=

python这语言用着还真是挺奇葩的,由于接触的时间比较短,不知道翻转字符串的东西应该怎么写。自己照着语法规则蒙了一个,结果还对了~

import math
x=input()
y=input()
t=str(x[::-1])#把x的字符串反着存到t中
if t!=y:
print('NO')
else:
print ('YES')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: