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

      

      

  • 相关阅读:
    sql中not exists的用法
    jsp中target="_blank"的用法
    jsp 运用 session 登录输出
    jsp留言板
    编写JSP 实现用户登录并判断用户或密码
    jsp打印九九乘法表
    jsp输出当前时间
    jsp输出金字塔
    jsp输出5的阶乘
    jdbc练习3
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/12207920.html
Copyright © 2011-2022 走看看