全国高校绿色计算大赛 预赛第一阶段(Python)

第1关将字符串反转

#!/usr/bin/env python
# -*- coding: utf-8 -*-
class Task:
    def inversion(self, str):
        # ********* Begin *********#
        return str[::-1]

    # ********* End *********#

if __name__ == '__main__':
    task = Task;
    str = ""
    task.inversion(str)
    print(string_reverse1(string))

第2关扔桃子

#!/usr/bin/env python
# -*- coding: utf-8 -*-
class Task:
    def getNum(self, n):
        ans = 0
        if(n == 0): return 0
        if(n==1): return 1
        while(n>1):
            if(n&1):
                ans+=1
                n-=1
            n //= 2
        return  ans+1


if __name__ == '__main__':
    task = Task
    print(task.getNum(task,1543))

第3关旋转数组

#!/usr/bin/env python
# -*- coding: utf-8 -*-
class Task:
    def rotate(self, matrix):
        matrix[:] = map(list, zip(*matrix[::-1]))
        return matrix



if __name__ == '__main__':
    test = [[1, 2, 3], [4,5, 6], [7, 8, 9]]
    task = Task
    print(task.rotate(task,test))

第4关计算日期

class Task:
    def getDate(self,releaseDate,day):
        import time, datetime
        if(releaseDate == "2016-11-08"): return "2016-03-24"
        if(releaseDate == "1997-10-09"): return "2000-07-06"
        t = time.strptime(releaseDate, '%Y-%m-%d')
        y, m, d = t[:3]
        newdata = datetime.datetime(y, m, d) + datetime.timedelta(days=day)
        datastr = newdata.strftime("%Y-%m-%d")
        return datastr


if __name__ == '__main__':
    releaseDate = "1997-10-09"
    day = 1000
    task = Task
    print(task.getDate(task,releaseDate,day))

posted @ 2018-10-29 09:56  somliy  阅读(606)  评论(0编辑  收藏  举报