• 全国绿色计算大赛 模拟赛第一阶段(Python)


    第1关求和

    class Task:
        def getSum(self, num1, num2):
            sum = 0
            for i in range(num1, num2 + 1):
                while (i != 0):
                    sum += i % 10
                    i /= 10
                    i = int(i)
            return sum
    
    
    if __name__ == '__main__':
        task = Task
        a = task.getSum(task, 15, 19)
        print(a)
    
    

    第2关文件查看器

    import os, sys
    
    
    class Task:
        def showDirTree(self, path):
            print("+--" + path.split('\')[-1])
            for dir in os.listdir(path):
                if os.path.isdir(path):
                    print(" " * 2 + "+--" + dir)
                    self.dealDir(task,path + "\" + dir, 4)
                else:
                    print(" " * 2 + "--" + dir)
    
        def dealDir(self, path, step):
            if (os.listdir(path) != None):
                for dir in os.listdir(path):
                    new_path = path + "\" + dir
                    if os.path.isdir(new_path):
                        print(" " * step + "+--" + dir)
                        self.dealDir(self, path + "\" + dir, step + 2)
                    else:
                        print(" " * step + "--" + dir)
    
    
    if __name__ == '__main__':
        path = "C:\Users\486\Desktop\test\root"
        task = Task
        task.showDirTree(task, path)
    
    

    第3关图片查看器

    import os, sys
    
    
    class Task:
        def showDirTree(self, path):
            print("+--" + path.split('\')[-1])
            for dir in os.listdir(path):
    
                if os.path.isdir(path + "\" + dir):
                    print(" " * 2 + "+--" + dir)
                    self.dealDir(task, path + "\" + dir, 4)
                else:
                    if (self.judge(self, dir)):
                        print(" " * 2 + "--" + dir)
    
        def dealDir(self, path, step):
            if os.path.isfile(path):
                if (self.judge(self, os.path.basename(path))):
                    print(" " * step + "--" + os.path.basename(path))
            else:
                if (os.listdir(path) != None):
                    for dir in os.listdir(path):
                        new_path = path + "\" + dir
    
                        if os.path.isdir(new_path):
                            print(" " * step + "+--" + dir)
                            self.dealDir(self, path + "\" + dir, step + 2)
                        else:
                            if (task.judge(self, dir)):
                                print(" " * step + "--" + dir)
    
        def judge(self, dir):
            if os.path.splitext(dir)[1] == ".jpg" or os.path.splitext(dir)[1] == ".png" or os.path.splitext(dir)[
                1] == ".bmp":
                return True
            else:
                return False
    
    
    if __name__ == '__main__':
        path = "C:\Users\486\Desktop\test\dir"
        task = Task
    
        task.showDirTree(task, path)
    
    
  • 相关阅读:
    BZOJ4569 : [Scoi2016]萌萌哒
    2016浙江省赛过山车记
    BZOJ4546(原) : 三元组
    BZOJ4539 : [Hnoi2016]树
    BZOJ4537 : [Hnoi2016]最小公倍数
    BZOJ4538 : [Hnoi2016]网络
    BZOJ4527 : K-D-Sequence
    BZOJ4504 : K个串
    BZOJ4471 : 随机数生成器Ⅱ
    BZOJ3659 : Which Dreamed It
  • 原文地址:https://www.cnblogs.com/somliy/p/9868944.html
Copyright © 2020-2023  润新知