zoukankan      html  css  js  c++  java
  • 软赛(一)

    一、

    分析试题

    二、安装python2.7.5

    由于之前装过python 3.6,卸载安装后python IDLE始终打不开,鼓捣了好长时间。最后解决如下:

    打开后,又始终出现如下问题,

    http://blog.csdn.net/haitunxiaomo/article/details/37989727

    终于安装成功

    三、sys库

    sys.argv[]说白了就是一个从程序外部获取参数的桥梁,这个“外部”很关键,所以那些试图从代码来说明它作用的解释一直没看明白。

    因为我们从外部取得的参数可以是多个,所以获得的是一个列表(list),也就是说sys.argv其实可以看作是一个列表,所以才能用[]提取其中的元素。

    其第一个元素是程序本身,随后才依次是外部给予的参数。 

    四、时间

    四、os库

    (1)数据读取

    def read_lines(file_path):
        if os.path.exists(file_path):
            array = []
            with open(file_path, 'r') as lines:
                for line in lines:
                    array.append(line)#加入新的行
            return array
        else:
            print 'file not exist: ' + file_path
            return None
    

    五、键值对

    可以用这种方法统计时间,键为虚拟机名称,值为访问时间。

    # coding=utf-8
    import time          
    import re          
    import os
    import string
    import sys
    
    source = open("F:\Student\1Area.txt",'r')
    lines = source.readlines()
    count = 1
    L = []    #列表二维 国家行数 人口数
    
    '''
    第一部分 获取国土面积
    '''
    print 'Start!!!'
    for line in lines:
        line = line.rstrip('
    ')          #去除换行
        start = line.find(r'V:')
        end = line.find(r'平方公里')
        number = line[start+2:end]
        number = number.replace(',','')   #去除','
        fNum = 0.0
        if '万' in number:
            end = line.find(r'万')
            newNum = line[start+2:end]
            fNum = string.atof(newNum)*10000
        else: #如何优化代码 全局变量
            if '/' in number:
                end = line.find(r'/')
                newNum = line[start+2:end]
                newNum = newNum.replace(',','')
                fNum = string.atof(newNum)
            elif '(' in number:
                end = line.find(r'(')
                newNum = line[start+2:end]
                newNum = newNum.replace(',','')
                fNum = string.atof(newNum)
            elif '[' in number:
                end = line.find(r'[')
                newNum = line[start+2:end]
                newNum = newNum.replace(',','')
                fNum = string.atof(newNum)
            elif '或' in number:
                end = line.find(r'或')
                newNum = line[start+2:end]
                newNum = newNum.replace(',','')
                fNum = string.atof(newNum)
            elif ' ' in number:
                end = line.find(r' ')
                newNum = line[start+2:end]
                newNum = newNum.replace(',','')
                fNum = string.atof(newNum)
            else:
                fNum = string.atof(number)
        #print line
        #print number
        #print fNum
        L.append((count,fNum))
        count = count + 1
    else:
        print 'End While'
        source.close()
    
    
    '''
    第二部分 从大到小排序
    参看 http://blog.chinaunix.net/uid-20775448-id-4222915.html
    '''
    L.sort(lambda x,y:cmp(x[1],y[1]),reverse = True)
    #print L
    #遍历过程 表示第i名 (文件第x行,面积y平方公里)
    #重点 L[i]输出列表 1 (46, 17075200.0) L[i][0]表示元组tuple第一个数 1 46
    for i in range(len(L)):
        print (i+1), L[i]
        
    
    '''
    第三部分 读写文件
    '''
    source = open("F:\Student\1Area.txt",'r')
    lines = source.readlines()
    result = open("F:\Student\1NewArea.txt",'w')
    count = 1
    for line in lines:
        line = line.rstrip('
    ')
        #获取列表L中排名位置pm
        pm = 0
        for i in range(len(L)):
            if count==L[i][0]:
                pm = i+1
                break
        #获取文件中名次
        if '世界第' in line:
            start = line.find(r'世界第')
            end = line.find(r'名')
            number  = line[start+9:end]
            if '/' in number: #防止中国第3/4名
                end = line.find(r'/')
                number  = line[start+9:end]
            if '包括海外' in number:
                number = '41'
            print number,pm,type(number),type(pm)
            
            if string.atoi(number)==pm:
                line = line + '     【排名正确】  【世界第' + str(pm) + '名】'
                result.write(line+'
    ')
            else:
                line = line + '     【排名错误】  【世界第' + str(pm) + '名】'
                result.write(line+'
    ')
        else: #文件中没有排名
            line = line + '     【新加排名】  【世界第' + str(pm) + '名】'
            result.write(line+'
    ')  
        count = count + 1
    else:
        print 'End Sorted'
        source.close()
        result.close()
    
    '''
    第四部分 输出一个排序好的文件 便于观察
    '''
    source = open("F:\Student\1Area.txt",'r')
    lines = source.readlines()
    result = open("F:\Student\1NewSortArea.txt",'w')
    #i表示第i名 L[i][0]表示行数
    pm = 0
    for i in range(len(L)):
        pm = L[i][0]
        count = 1
        for line in lines:
            line = line.rstrip('
    ')
            if count==pm:
                line = line + '  【世界第' + str(i+1) + '名】'
                result.write(line+'
    ')
                break
            else:
                count = count + 1
    else:
        print 'End Sorted Second'
        source.close()
        result.close()
    

     

    四、python 复制问题

     

      

  • 相关阅读:
    android Edittext自定义输入字符和类型
    让android webView使用系统默认浏览器内核直接解析,不弹出选择浏览器选项
    java对象中继承和变量初始化顺序浅析
    android判断pad还是手机
    我不知道自己想要什么
    计算机网络概述
    2020/2/27-28
    操作系统概述
    数据模型
    数据库系统概述
  • 原文地址:https://www.cnblogs.com/ruo-li-suo-yi/p/8543229.html
Copyright © 2011-2022 走看看