zoukankan      html  css  js  c++  java
  • 全国绿色计算大赛 模拟赛第一阶段(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)
    
    
  • 相关阅读:
    Hbase记录-Hbase shell使用
    Hbase记录-Hbase基础概念
    JAVA记录-SpringMVC集成redis
    JAVA记录-redis缓存机制介绍(四)
    JAVA记录-redis缓存机制介绍(三)
    JAVA记录-redis缓存机制介绍(二)
    JAVA记录-redis缓存机制介绍(一)
    JAVA记录-SpringMVC scope属性的两种模式
    JAVA记录-JDBC介绍
    鼠标拖动,改变列表宽度
  • 原文地址:https://www.cnblogs.com/somliy/p/9868944.html
Copyright © 2011-2022 走看看