ZOJ-3504 P-norm (py method good), zoj-3504p-norm

Source: Internet
Author: User

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.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.