ZOJ-3504 P-norm (py method good), zoj-3504p-norm
Yesterday afternoon's training was based on a question from the watashi Team of the ZOJ monthly competition (I remember that when I first came into contact with watashi, I learned how to write oj and submit the robot. Although I cannot understand it, however, The py method is used to solve the problem perfectly .)
Question Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? ProblemCode = 3504.
I think it is very difficult to write this question using c ++. In the end, it is to calculate the p-norm.
There are three rows of input, such
(1,0) (0,1) (-1,0) (0,-1)(1,0) (0,-1) (-1,0) (0,1)1
We calculate:
1. the first two rows correspond to the modulo of the vectors formed by two parentheses, for example (a, B) corresponds to (c, d), and its modulo is sqrt (a-c) ^ 2 + (B-d) ^ 2)
2. calculate each modulo to the power of p (corresponding to the integer of the third row), and sum
3. Open p root for the sum
The most troublesome thing is to deal with these parentheses. Because of the uncertain number, it is better to use c/c ++ as the reading Mark (% lf, % lf.
However, zoj supports py, which is really nice. py can use very simple code to complete this step. It only needs to read three rows and perform regular expressions on one or two rows, at last, you only need 20 rows and more to complete the question.
from re import compilefrom math import pow,sqrtimport syspt=compile(r'\((.*?),(.*?)\)')while 1: line=sys.stdin.readline() if len(line)>1: line2=sys.stdin.readline() p=float(sys.stdin.readline()) res1=pt.findall(line) res2=pt.findall(line2) x=[] y=[] ans=0.0 for i in range(len(res1)): x.append(float(res1[i][0])-float(res2[i][0])) y.append(float(res1[i][1])-float(res2[i][1])) for i in range(len(x)): ans+=pow(sqrt(x[i]**2+y[i]**2),p) print '%.08f'%pow(ans,1.0/p) else: break
My life is short. I use py.