zoukankan      html  css  js  c++  java
  • Python 编程练习

    https://acm.ecnu.edu.cn/contest/33/

    # 进制转换

    import math
    
    def main():
        T = int(input())
        while T > 0:
            T -= 1
            a, b = map(int, input().split())
    
            if a < 0:
                print('-', end='')
                a = -a
            if a == 0:
                print('0', end='')
    
            t = []
            while a > 0:
                t.append(a % b)
                a = a // b
            for i in range(len(t) - 1, -1, -1):
                if t[i] >= 10:
                    print(chr(t[i] - 10 + ord('A')), end = '') # ascii转换
                else :
                    print(t[i], end='')
            print()
    
    
    if __name__ == '__main__':
        main()
    View Code

    # IP地址的转化

    import math
    
    def main():
        T = int(input())
        while T > 0:
            T -= 1
            str = input()
            a = []
            id = 0
            for i in range(0,4):
                a.append(0)
                for j in range(0, 8):
                    a[i] = a[i] * 2
                    if str[id] == '1':
                        a[i] += 1
                    id+=1
            print("%d.%d.%d.%d"%(a[0], a[1], a[2], a[3]))
    
    
    if __name__ == '__main__':
        main()
    View Code

     # 大小写转换

    (string 类型不能用str[i] = 赋值)

    import math
    
    def main():
        str = input()
        for i in range(len(str)):
    
            if ord(str[i]) >= ord('a') and ord(str[i]) <= ord('z') :
            # string是不可变的,只能用列表分割
                str = str[:i] + chr(ord(str[i]) - ord('a') + ord('A')) + str[i + 1:]
            elif ord(str[i]) >= ord('A') and ord(str[i]) <= ord('Z') :
                str = str[:i] + chr(ord(str[i]) - ord('A') + ord('a')) + str[i + 1:]
        print(str)
    
    
    if __name__ == '__main__':
        main()
    View Code

    # 1018. 位运算

    这道题要注意,p值比较大的情况,要自己先补0,不然会RE.

    import math
    
    def main():
        a, b, c = map(int, input().split())
        chang = 0
        v = []
        while a > 0:
            v.append(a % 2)
            chang =chang + 1
            a = a // 2
        while b >= chang:
            v.append(0)
            chang += 1
    
        for i in range(b,  b-c, -1):
                v[i] = 1-v[i]
    
        ans = 0
    
        for i in range(chang):
                ans = ans * 2 + v[chang - i - 1]
        print(ans)
    
    
    if __name__ == '__main__':
        main()
    View Code

      

      

  • 相关阅读:
    福大软工 · 第七次作业
    git分支重命名
    vscode左侧文件不同颜色标识含义
    正则表达式匹配指定字符区间的内容,且不包含指定字符
    git如何撤销工作区的修改
    git查看某个文件的提交历史
    扫盲篇--远程桌面连接
    git merge后如何撤销
    element-ui+vue-treeselect校验
    typeof 踩坑总结
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/12207920.html
Copyright © 2011-2022 走看看