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

 

  

  

posted @ 2020-01-17 23:40  ckxkexing  阅读(154)  评论(0编辑  收藏  举报