首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

PTA-浙大版《Python 程序设计》AC解答汇总-第四章

  • 24-03-02 23:55
  • 2799
  • 13109
blog.csdn.net
def is_prime(x):
    if x<2:
        return False
    elif x==2:
        return True
    else :
        for i in range(2,int(math.sqrt(x))+1):
            if x%i==0:
                return False
    return True
    
# 第4章-1 生成3的乘方表 (15分)
def demo_4_1():
    n=int(input())
    for i in range(n+1):
        print("pow(3,%d) = %d"%(i,pow(3,i)))


# 第4章-2 统计素数并求和 (20分)
def demo_4_2():
    m,n=map(int,input().split())
    cnt,sum=0,0
    for i in range(m,n+1):
        if is_prime(i):
            cnt=cnt+1
            sum=sum+i 
    print(cnt,sum)


# 第4章-3 猴子吃桃问题 (15分)
def demo_4_3():
    n=int(input())
    res=1
    for i in range(n-1):
        res=(res+1)*2
    print(res)


# 第4章-4 验证“哥德巴赫猜想” (20分)
def demo_4_4():
    n=int(input())
    for i in range(2,1000000001):
        if is_prime(i) and is_prime(n-i):
            print("%d = %d + %d"%(n,i,n-i))
            break
    

# 第4章-5 求e的近似值 (15分)
def demo_4_5():
    n=int(input())
    print("%.8f"%(sum(1/math.factorial(x) for x in range(n+1))))


# 第4章-6 输出前 n 个Fibonacci数 (15分)
def demo_4_6():
    n=int(input())
    a,b=0,1
    if n<1:
        print("Invalid.")
    else:
        for i in range(1,n+1):
            print("{:>11}".format(b),end="")
            b=a+b 
            a=b-a
            if i%5==0:
                print("")
    

# 第4章-7 统计学生平均成绩与及格人数 (15分)
def demo_4_7():
    if int(input())>0:
        scores=list(map(int,input().split()))
        print("average = %.1f"%(sum(scores)/len(scores)))
        print("count = %d"%(len([x for x in scores if x >= 60])))
    else:    
        print("average = 0.0")
        print("count = 0")


# 第4章-8 求分数序列前N项和 (15分)
def demo_4_8():
    n=int(input())
    a,b,res=2,1,0.
    for i in range(n):
        res=res+a/b
        a=a+b 
        b=a-b
    print("%.2f"%res)


# 第4章-9 查询水果价格 (15分)
def demo_4_9():
    print("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit")
    price=[3.,2.5,4.1,10.2]
    nums=list(map(int,input().split()))
    for i in range(len(nums)):
        if nums[i]==0 or i>=5:
            break
        elif nums[i]>0 and nums[i]<5:
            print("price = %.2f"%price[nums[i]-1])
        else:
            print("price = 0.00")
    

# 第4章-10 最大公约数和最小公倍数 (15分)
def gcd(a,b):
    return b if a==0 else gcd(b%a,a)

def demo_4_10():
    m,n=map(int,input().split())
    print("%d %d"%(gcd(m,n),m*n/gcd(m,n)))


# 第4章-11 判断素数 (20分)
def demo_4_11():
    n=int(input())
    for i in range(n):
        if is_prime(int(input())):
            print("Yes")
        else:
            print("No")


# 第4章-12 求满足条件的斐波那契数 (30分)
def demo_4_12():
    n=int(input())
    a,b=0,1
    while b<n:    
        b=a+b 
        a=b-a
    print(b)


# 第4章-13 求误差小于输入值的e的近似值 (20分)
# import math 
def demo_4_13():
    err=float(input())
    a,b=0.,1.
    for i in range(1,10000):
        if b-a>err:
            a=b
            b=b+1/math.factorial(i) 
        else:
            break
    print("%.6f"%b)


# 第4章-14 统计字符 (15分)
# case 2是10个blank
def demo_4_14():
    letter,blank,digit,other,cnt=0,0,0,0,0
    while 1:
        a=list(input())
        for x in a:
            if x.isalpha():
                letter=letter+1
            elif x.isspace():
                blank=blank+1
            elif x.isdigit():
                digit=digit+1
            else:
                other=other+1
        cnt=cnt+len(a)+1
        blank=blank+1 #每一行有个回车 
        if cnt>10:
            break
    print("letter = %d, blank = %d, digit = %d, other = %d"%(letter,blank-1,digit,other))


# 第4章-15 换硬币 (20分)
def demo_4_15():
    n=int(input())
    cnt=0
    for fen5 in range(n//5,0,-1):
        for fen2 in range(n//2,0,-1):
            for fen1 in range(n,0,-1):
                if (fen5*5+fen2*2+fen1 == n):
                    print('fen5:%d, fen2:%d, fen1:%d, total:%d'%(fen5, fen2, fen1, fen1+fen2+fen5))
                    cnt=cnt+1
    print('count = %d'%cnt)


# 第4章-16 jmu-python-判断是否构成三角形 (10分)
def demo_4_16():
    nums=list(map(int,input().split()))
    if sum(nums)-2*max(nums)>0:
        print("yes")
    else:
        print("no")


# 第4章-17 水仙花数(20 分) (20分)
def demo_4_17():
    n=int(input())
    for i in range(pow(10,n-1),pow(10,n)):
        sum=0
        for x in str(i):
            sum=sum+pow(int(x),n)
        if sum==i:
            print(i)


# 第4章-18 猴子选大王 (20分)
def demo_4_17():
    n=int(input())
    a=list('0')*n
    index,cnt,last=0,0,0
    while '0' in a:
        if a[index]!='1':
            cnt=cnt+1
            if cnt>=3:
                cnt=0
                a[index]='1'
                last=index
        index=(index+1)%n   
    print(last+1)


# 第4章-19 矩阵运算 (20分)
def demo_4_19():
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(input().split())
        matrix.append(a)
    sum=0
    for i in range(n):
        for j in range(n):
            if i!=n-1 and j!=n-1 and i+j!=n-1:
                sum=sum+int(matrix[i][j])
    print(sum)    


# 第4章-20 求矩阵各行元素之和 (15分)
def demo_4_20():
    m,n=map(int,input().split())
    for i in range(m):
        print(sum([int(x) for x in input().split()]))


# 第4章-21 判断上三角矩阵 (15分)
def demo_4_21():
    n=int(input())
    for x in range(n):
        size=int(input())
        matrix=[]
        flag=True
        for i in range(size):
            a=list(input().split())
            matrix.append(a)
        for i in range(1,size):
            for j in range(i):
                if matrix[i][j]!='0':
                    flag=False
        print("YES"if flag else "NO")  


# 第4章-22 找鞍点 (20分)
def demo_4_22():
    import numpy as np
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(map(int,input().split()))
        matrix.append(a)
    npmat=np.array(matrix)
    for i in range(n):
        for j in range(n):
            if npmat.max(1)[i]==npmat.min(0)[j]:
                print(i,j)
                exit()
    print("NONE")


# 第4章-23 求矩阵的局部极大值 (15分)
def demo_4_23():
    m,n=map(int,input().split())
    matrix=[]
    for i in range(m):
        a=list(map(int,input().split()))
        matrix.append(a)
    flag=True
    for i in range(1,m-1):
        for j in range(1,n-1):
            if matrix[i][j]>matrix[i][j-1] and matrix[i][j]>matrix[i-1][j] and \
            matrix[i][j]>matrix[i+1][j] and matrix[i][j]>matrix[i][j+1] :
                print(matrix[i][j],i+1,j+1)
                flag=False
    if flag:
        print("None",m,n)


# 第4章-24 打印九九口诀表 (15分)
def demo_4_24():
    n=int(input())
    for i in range(1,n+1):
        for j in range(1,i+1):
            print("{:d}*{:d}={:<4d}".format(j,i,i*j),end="")
        print("")


# 第4章-25 输出三角形字符阵列 (15分)
def demo_4_25():
    n=int(input())
    c=ord("A")
    for i in range(n):
        for j in range(n-i):
            print("%c"%c,end=" ")   
            c=c+1     
        print("")


# 第4章-26 求1!+3!+5!+……+n! (10分)
def demo_4_26():
    import math
    n=int(input())
    sum=0
    for i in range(1,n+1,2):
        sum=sum+math.factorial(i) 
    print("n=%d,s=%d"%(n,sum))


# 第4章-27 二维数组中每行最大值和每行和 (10分)
def demo_4_27():
    nums=list(map(int,input().split()))
    for i in range(0,8,3):
        abc=nums[i:i+3]
        print("%4d%4d%4d%4d%4d"%(abc[0],abc[1],abc[2],max(abc),sum(abc)))


# 第4章-28 矩阵转置 (10分)
def demo_4_28():
    nums=list(map(int,input().split()))
    for i in range(3):
        print("%4d%4d%4d"%(nums[i+0],nums[i+3],nums[i+6]))


# 第4章-29 找出不是两个数组共有的元素 (20分)
def demo_4_29():
    nums1=list(input().split())
    nums2=list(input().split())
    nums1.pop(0)
    nums2.pop(0)
    out=[]
    for x in nums1:
        if x not in nums2 and x not in out:
            out.append(x)
    for x in nums2:
        if x not in nums1 and x not in out:  
            out.append(x)   
    print(" ".join(out))


# 第4章-30 找完数 (20分)
def demo_4_30():
    import math
    m,n=map(int,input().split())
    none=True
    for i in range(m,n+1):
        factor=[1,]
        for f in range(2,int(math.sqrt(i)+1)):#不然会超时
            if i//f==i/f:
                factor.append(int(f))
                factor.append(int(i/f))#同时记录对称因子
        if sum(factor)==i:
            none=False
            print("%d = "%i,end="")
            print(" + ".join("%s"%x for x in sorted(factor)))#对称因子导致不按顺序
    if none:
        print("None")
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
def is_prime(x):
    if x<2:
        return False
    elif x==2:
        return True
    else :
        for i in range(2,int(math.sqrt(x))+1):
            if x%i==0:
                return False
    return True
    
# 第4章-1 生成3的乘方表 (15分)
def demo_4_1():
    n=int(input())
    for i in range(n+1):
        print("pow(3,%d) = %d"%(i,pow(3,i)))


# 第4章-2 统计素数并求和 (20分)
def demo_4_2():
    m,n=map(int,input().split())
    cnt,sum=0,0
    for i in range(m,n+1):
        if is_prime(i):
            cnt=cnt+1
            sum=sum+i 
    print(cnt,sum)


# 第4章-3 猴子吃桃问题 (15分)
def demo_4_3():
    n=int(input())
    res=1
    for i in range(n-1):
        res=(res+1)*2
    print(res)


# 第4章-4 验证“哥德巴赫猜想” (20分)
def demo_4_4():
    n=int(input())
    for i in range(2,1000000001):
        if is_prime(i) and is_prime(n-i):
            print("%d = %d + %d"%(n,i,n-i))
            break
    

# 第4章-5 求e的近似值 (15分)
def demo_4_5():
    n=int(input())
    print("%.8f"%(sum(1/math.factorial(x) for x in range(n+1))))


# 第4章-6 输出前 n 个Fibonacci数 (15分)
def demo_4_6():
    n=int(input())
    a,b=0,1
    if n<1:
        print("Invalid.")
    else:
        for i in range(1,n+1):
            print("{:>11}".format(b),end="")
            b=a+b 
            a=b-a
            if i%5==0:
                print("")
    

# 第4章-7 统计学生平均成绩与及格人数 (15分)
def demo_4_7():
    if int(input())>0:
        scores=list(map(int,input().split()))
        print("average = %.1f"%(sum(scores)/len(scores)))
        print("count = %d"%(len([x for x in scores if x >= 60])))
    else:    
        print("average = 0.0")
        print("count = 0")


# 第4章-8 求分数序列前N项和 (15分)
def demo_4_8():
    n=int(input())
    a,b,res=2,1,0.
    for i in range(n):
        res=res+a/b
        a=a+b 
        b=a-b
    print("%.2f"%res)


# 第4章-9 查询水果价格 (15分)
def demo_4_9():
    print("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit")
    price=[3.,2.5,4.1,10.2]
    nums=list(map(int,input().split()))
    for i in range(len(nums)):
        if nums[i]==0 or i>=5:
            break
        elif nums[i]>0 and nums[i]<5:
            print("price = %.2f"%price[nums[i]-1])
        else:
            print("price = 0.00")
    

# 第4章-10 最大公约数和最小公倍数 (15分)
def gcd(a,b):
    return b if a==0 else gcd(b%a,a)

def demo_4_10():
    m,n=map(int,input().split())
    print("%d %d"%(gcd(m,n),m*n/gcd(m,n)))


# 第4章-11 判断素数 (20分)
def demo_4_11():
    n=int(input())
    for i in range(n):
        if is_prime(int(input())):
            print("Yes")
        else:
            print("No")


# 第4章-12 求满足条件的斐波那契数 (30分)
def demo_4_12():
    n=int(input())
    a,b=0,1
    while b<n:    
        b=a+b 
        a=b-a
    print(b)


# 第4章-13 求误差小于输入值的e的近似值 (20分)
# import math 
def demo_4_13():
    err=float(input())
    a,b=0.,1.
    for i in range(1,10000):
        if b-a>err:
            a=b
            b=b+1/math.factorial(i) 
        else:
            break
    print("%.6f"%b)


# 第4章-14 统计字符 (15分)
# case 2是10个blank
def demo_4_14():
    letter,blank,digit,other,cnt=0,0,0,0,0
    while 1:
        a=list(input())
        for x in a:
            if x.isalpha():
                letter=letter+1
            elif x.isspace():
                blank=blank+1
            elif x.isdigit():
                digit=digit+1
            else:
                other=other+1
        cnt=cnt+len(a)+1
        blank=blank+1 #每一行有个回车 
        if cnt>10:
            break
    print("letter = %d, blank = %d, digit = %d, other = %d"%(letter,blank-1,digit,other))


# 第4章-15 换硬币 (20分)
def demo_4_15():
    n=int(input())
    cnt=0
    for fen5 in range(n//5,0,-1):
        for fen2 in range(n//2,0,-1):
            for fen1 in range(n,0,-1):
                if (fen5*5+fen2*2+fen1 == n):
                    print('fen5:%d, fen2:%d, fen1:%d, total:%d'%(fen5, fen2, fen1, fen1+fen2+fen5))
                    cnt=cnt+1
    print('count = %d'%cnt)


# 第4章-16 jmu-python-判断是否构成三角形 (10分)
def demo_4_16():
    nums=list(map(int,input().split()))
    if sum(nums)-2*max(nums)>0:
        print("yes")
    else:
        print("no")


# 第4章-17 水仙花数(20 分) (20分)
def demo_4_17():
    n=int(input())
    for i in range(pow(10,n-1),pow(10,n)):
        sum=0
        for x in str(i):
            sum=sum+pow(int(x),n)
        if sum==i:
            print(i)


# 第4章-18 猴子选大王 (20分)
def demo_4_17():
    n=int(input())
    a=list('0')*n
    index,cnt,last=0,0,0
    while '0' in a:
        if a[index]!='1':
            cnt=cnt+1
            if cnt>=3:
                cnt=0
                a[index]='1'
                last=index
        index=(index+1)%n   
    print(last+1)


# 第4章-19 矩阵运算 (20分)
def demo_4_19():
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(input().split())
        matrix.append(a)
    sum=0
    for i in range(n):
        for j in range(n):
            if i!=n-1 and j!=n-1 and i+j!=n-1:
                sum=sum+int(matrix[i][j])
    print(sum)    


# 第4章-20 求矩阵各行元素之和 (15分)
def demo_4_20():
    m,n=map(int,input().split())
    for i in range(m):
        print(sum([int(x) for x in input().split()]))


# 第4章-21 判断上三角矩阵 (15分)
def demo_4_21():
    n=int(input())
    for x in range(n):
        size=int(input())
        matrix=[]
        flag=True
        for i in range(size):
            a=list(input().split())
            matrix.append(a)
        for i in range(1,size):
            for j in range(i):
                if matrix[i][j]!='0':
                    flag=False
        print("YES"if flag else "NO")  


# 第4章-22 找鞍点 (20分)
def demo_4_22():
    import numpy as np
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(map(int,input().split()))
        matrix.append(a)
    npmat=np.array(matrix)
    for i in range(n):
        for j in range(n):
            if npmat.max(1)[i]==npmat.min(0)[j]:
                print(i,j)
                exit()
    print("NONE")


# 第4章-23 求矩阵的局部极大值 (15分)
def demo_4_23():
    m,n=map(int,input().split())
    matrix=[]
    for i in range(m):
        a=list(map(int,input().split()))
        matrix.append(a)
    flag=True
    for i in range(1,m-1):
        for j in range(1,n-1):
            if matrix[i][j]>matrix[i][j-1] and matrix[i][j]>matrix[i-1][j] and \
            matrix[i][j]>matrix[i+1][j] and matrix[i][j]>matrix[i][j+1] :
                print(matrix[i][j],i+1,j+1)
                flag=False
    if flag:
        print("None",m,n)


# 第4章-24 打印九九口诀表 (15分)
def demo_4_24():
    n=int(input())
    for i in range(1,n+1):
        for j in range(1,i+1):
            print("{:d}*{:d}={:<4d}".format(j,i,i*j),end="")
        print("")


# 第4章-25 输出三角形字符阵列 (15分)
def demo_4_25():
    n=int(input())
    c=ord("A")
    for i in range(n):
        for j in range(n-i):
            print("%c"%c,end=" ")   
            c=c+1     
        print("")


# 第4章-26 求1!+3!+5!+……+n! (10分)
def demo_4_26():
    import math
    n=int(input())
    sum=0
    for i in range(1,n+1,2):
        sum=sum+math.factorial(i) 
    print("n=%d,s=%d"%(n,sum))


# 第4章-27 二维数组中每行最大值和每行和 (10分)
def demo_4_27():
    nums=list(map(int,input().split()))
    for i in range(0,8,3):
        abc=nums[i:i+3]
        print("%4d%4d%4d%4d%4d"%(abc[0],abc[1],abc[2],max(abc),sum(abc)))


# 第4章-28 矩阵转置 (10分)
def demo_4_28():
    nums=list(map(int,input().split()))
    for i in range(3):
        print("%4d%4d%4d"%(nums[i+0],nums[i+3],nums[i+6]))


# 第4章-29 找出不是两个数组共有的元素 (20分)
def demo_4_29():
    nums1=list(input().split())
    nums2=list(input().split())
    nums1.pop(0)
    nums2.pop(0)
    out=[]
    for x in nums1:
        if x not in nums2 and x not in out:
            out.append(x)
    for x in nums2:
        if x not in nums1 and x not in out:  
            out.append(x)   
    print(" ".join(out))


# 第4章-30 找完数 (20分)
def demo_4_30():
    import math
    m,n=map(int,input().split())
    none=True
    for i in range(m,n+1):
        factor=[1,]
        for f in range(2,int(math.sqrt(i)+1)):#不然会超时
            if i//f==i/f:
                factor.append(int(f))
                factor.append(int(i/f))#同时记录对称因子
        if sum(factor)==i:
            none=False
            print("%d = "%i,end="")
            print(" + ".join("%s"%x for x in sorted(factor)))#对称因子导致不按顺序
    if none:
        print("None")
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
def is_prime(x):
    if x<2:
        return False
    elif x==2:
        return True
    else :
        for i in range(2,int(math.sqrt(x))+1):
            if x%i==0:
                return False
    return True
    
# 第4章-1 生成3的乘方表 (15分)
def demo_4_1():
    n=int(input())
    for i in range(n+1):
        print("pow(3,%d) = %d"%(i,pow(3,i)))


# 第4章-2 统计素数并求和 (20分)
def demo_4_2():
    m,n=map(int,input().split())
    cnt,sum=0,0
    for i in range(m,n+1):
        if is_prime(i):
            cnt=cnt+1
            sum=sum+i 
    print(cnt,sum)


# 第4章-3 猴子吃桃问题 (15分)
def demo_4_3():
    n=int(input())
    res=1
    for i in range(n-1):
        res=(res+1)*2
    print(res)


# 第4章-4 验证“哥德巴赫猜想” (20分)
def demo_4_4():
    n=int(input())
    for i in range(2,1000000001):
        if is_prime(i) and is_prime(n-i):
            print("%d = %d + %d"%(n,i,n-i))
            break
    

# 第4章-5 求e的近似值 (15分)
def demo_4_5():
    n=int(input())
    print("%.8f"%(sum(1/math.factorial(x) for x in range(n+1))))


# 第4章-6 输出前 n 个Fibonacci数 (15分)
def demo_4_6():
    n=int(input())
    a,b=0,1
    if n<1:
        print("Invalid.")
    else:
        for i in range(1,n+1):
            print("{:>11}".format(b),end="")
            b=a+b 
            a=b-a
            if i%5==0:
                print("")
    

# 第4章-7 统计学生平均成绩与及格人数 (15分)
def demo_4_7():
    if int(input())>0:
        scores=list(map(int,input().split()))
        print("average = %.1f"%(sum(scores)/len(scores)))
        print("count = %d"%(len([x for x in scores if x >= 60])))
    else:    
        print("average = 0.0")
        print("count = 0")


# 第4章-8 求分数序列前N项和 (15分)
def demo_4_8():
    n=int(input())
    a,b,res=2,1,0.
    for i in range(n):
        res=res+a/b
        a=a+b 
        b=a-b
    print("%.2f"%res)


# 第4章-9 查询水果价格 (15分)
def demo_4_9():
    print("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit")
    price=[3.,2.5,4.1,10.2]
    nums=list(map(int,input().split()))
    for i in range(len(nums)):
        if nums[i]==0 or i>=5:
            break
        elif nums[i]>0 and nums[i]<5:
            print("price = %.2f"%price[nums[i]-1])
        else:
            print("price = 0.00")
    

# 第4章-10 最大公约数和最小公倍数 (15分)
def gcd(a,b):
    return b if a==0 else gcd(b%a,a)

def demo_4_10():
    m,n=map(int,input().split())
    print("%d %d"%(gcd(m,n),m*n/gcd(m,n)))


# 第4章-11 判断素数 (20分)
def demo_4_11():
    n=int(input())
    for i in range(n):
        if is_prime(int(input())):
            print("Yes")
        else:
            print("No")


# 第4章-12 求满足条件的斐波那契数 (30分)
def demo_4_12():
    n=int(input())
    a,b=0,1
    while b<n:    
        b=a+b 
        a=b-a
    print(b)


# 第4章-13 求误差小于输入值的e的近似值 (20分)
# import math 
def demo_4_13():
    err=float(input())
    a,b=0.,1.
    for i in range(1,10000):
        if b-a>err:
            a=b
            b=b+1/math.factorial(i) 
        else:
            break
    print("%.6f"%b)


# 第4章-14 统计字符 (15分)
# case 2是10个blank
def demo_4_14():
    letter,blank,digit,other,cnt=0,0,0,0,0
    while 1:
        a=list(input())
        for x in a:
            if x.isalpha():
                letter=letter+1
            elif x.isspace():
                blank=blank+1
            elif x.isdigit():
                digit=digit+1
            else:
                other=other+1
        cnt=cnt+len(a)+1
        blank=blank+1 #每一行有个回车 
        if cnt>10:
            break
    print("letter = %d, blank = %d, digit = %d, other = %d"%(letter,blank-1,digit,other))


# 第4章-15 换硬币 (20分)
def demo_4_15():
    n=int(input())
    cnt=0
    for fen5 in range(n//5,0,-1):
        for fen2 in range(n//2,0,-1):
            for fen1 in range(n,0,-1):
                if (fen5*5+fen2*2+fen1 == n):
                    print('fen5:%d, fen2:%d, fen1:%d, total:%d'%(fen5, fen2, fen1, fen1+fen2+fen5))
                    cnt=cnt+1
    print('count = %d'%cnt)


# 第4章-16 jmu-python-判断是否构成三角形 (10分)
def demo_4_16():
    nums=list(map(int,input().split()))
    if sum(nums)-2*max(nums)>0:
        print("yes")
    else:
        print("no")


# 第4章-17 水仙花数(20 分) (20分)
def demo_4_17():
    n=int(input())
    for i in range(pow(10,n-1),pow(10,n)):
        sum=0
        for x in str(i):
            sum=sum+pow(int(x),n)
        if sum==i:
            print(i)


# 第4章-18 猴子选大王 (20分)
def demo_4_17():
    n=int(input())
    a=list('0')*n
    index,cnt,last=0,0,0
    while '0' in a:
        if a[index]!='1':
            cnt=cnt+1
            if cnt>=3:
                cnt=0
                a[index]='1'
                last=index
        index=(index+1)%n   
    print(last+1)


# 第4章-19 矩阵运算 (20分)
def demo_4_19():
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(input().split())
        matrix.append(a)
    sum=0
    for i in range(n):
        for j in range(n):
            if i!=n-1 and j!=n-1 and i+j!=n-1:
                sum=sum+int(matrix[i][j])
    print(sum)    


# 第4章-20 求矩阵各行元素之和 (15分)
def demo_4_20():
    m,n=map(int,input().split())
    for i in range(m):
        print(sum([int(x) for x in input().split()]))


# 第4章-21 判断上三角矩阵 (15分)
def demo_4_21():
    n=int(input())
    for x in range(n):
        size=int(input())
        matrix=[]
        flag=True
        for i in range(size):
            a=list(input().split())
            matrix.append(a)
        for i in range(1,size):
            for j in range(i):
                if matrix[i][j]!='0':
                    flag=False
        print("YES"if flag else "NO")  


# 第4章-22 找鞍点 (20分)
def demo_4_22():
    import numpy as np
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(map(int,input().split()))
        matrix.append(a)
    npmat=np.array(matrix)
    for i in range(n):
        for j in range(n):
            if npmat.max(1)[i]==npmat.min(0)[j]:
                print(i,j)
                exit()
    print("NONE")


# 第4章-23 求矩阵的局部极大值 (15分)
def demo_4_23():
    m,n=map(int,input().split())
    matrix=[]
    for i in range(m):
        a=list(map(int,input().split()))
        matrix.append(a)
    flag=True
    for i in range(1,m-1):
        for j in range(1,n-1):
            if matrix[i][j]>matrix[i][j-1] and matrix[i][j]>matrix[i-1][j] and \
            matrix[i][j]>matrix[i+1][j] and matrix[i][j]>matrix[i][j+1] :
                print(matrix[i][j],i+1,j+1)
                flag=False
    if flag:
        print("None",m,n)


# 第4章-24 打印九九口诀表 (15分)
def demo_4_24():
    n=int(input())
    for i in range(1,n+1):
        for j in range(1,i+1):
            print("{:d}*{:d}={:<4d}".format(j,i,i*j),end="")
        print("")


# 第4章-25 输出三角形字符阵列 (15分)
def demo_4_25():
    n=int(input())
    c=ord("A")
    for i in range(n):
        for j in range(n-i):
            print("%c"%c,end=" ")   
            c=c+1     
        print("")


# 第4章-26 求1!+3!+5!+……+n! (10分)
def demo_4_26():
    import math
    n=int(input())
    sum=0
    for i in range(1,n+1,2):
        sum=sum+math.factorial(i) 
    print("n=%d,s=%d"%(n,sum))


# 第4章-27 二维数组中每行最大值和每行和 (10分)
def demo_4_27():
    nums=list(map(int,input().split()))
    for i in range(0,8,3):
        abc=nums[i:i+3]
        print("%4d%4d%4d%4d%4d"%(abc[0],abc[1],abc[2],max(abc),sum(abc)))


# 第4章-28 矩阵转置 (10分)
def demo_4_28():
    nums=list(map(int,input().split()))
    for i in range(3):
        print("%4d%4d%4d"%(nums[i+0],nums[i+3],nums[i+6]))


# 第4章-29 找出不是两个数组共有的元素 (20分)
def demo_4_29():
    nums1=list(input().split())
    nums2=list(input().split())
    nums1.pop(0)
    nums2.pop(0)
    out=[]
    for x in nums1:
        if x not in nums2 and x not in out:
            out.append(x)
    for x in nums2:
        if x not in nums1 and x not in out:  
            out.append(x)   
    print(" ".join(out))


# 第4章-30 找完数 (20分)
def demo_4_30():
    import math
    m,n=map(int,input().split())
    none=True
    for i in range(m,n+1):
        factor=[1,]
        for f in range(2,int(math.sqrt(i)+1)):#不然会超时
            if i//f==i/f:
                factor.append(int(f))
                factor.append(int(i/f))#同时记录对称因子
        if sum(factor)==i:
            none=False
            print("%d = "%i,end="")
            print(" + ".join("%s"%x for x in sorted(factor)))#对称因子导致不按顺序
    if none:
        print("None")
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
def is_prime(x):
    if x<2:
        return False
    elif x==2:
        return True
    else :
        for i in range(2,int(math.sqrt(x))+1):
            if x%i==0:
                return False
    return True
    
# 第4章-1 生成3的乘方表 (15分)
def demo_4_1():
    n=int(input())
    for i in range(n+1):
        print("pow(3,%d) = %d"%(i,pow(3,i)))


# 第4章-2 统计素数并求和 (20分)
def demo_4_2():
    m,n=map(int,input().split())
    cnt,sum=0,0
    for i in range(m,n+1):
        if is_prime(i):
            cnt=cnt+1
            sum=sum+i 
    print(cnt,sum)


# 第4章-3 猴子吃桃问题 (15分)
def demo_4_3():
    n=int(input())
    res=1
    for i in range(n-1):
        res=(res+1)*2
    print(res)


# 第4章-4 验证“哥德巴赫猜想” (20分)
def demo_4_4():
    n=int(input())
    for i in range(2,1000000001):
        if is_prime(i) and is_prime(n-i):
            print("%d = %d + %d"%(n,i,n-i))
            break
    

# 第4章-5 求e的近似值 (15分)
def demo_4_5():
    n=int(input())
    print("%.8f"%(sum(1/math.factorial(x) for x in range(n+1))))


# 第4章-6 输出前 n 个Fibonacci数 (15分)
def demo_4_6():
    n=int(input())
    a,b=0,1
    if n<1:
        print("Invalid.")
    else:
        for i in range(1,n+1):
            print("{:>11}".format(b),end="")
            b=a+b 
            a=b-a
            if i%5==0:
                print("")
    

# 第4章-7 统计学生平均成绩与及格人数 (15分)
def demo_4_7():
    if int(input())>0:
        scores=list(map(int,input().split()))
        print("average = %.1f"%(sum(scores)/len(scores)))
        print("count = %d"%(len([x for x in scores if x >= 60])))
    else:    
        print("average = 0.0")
        print("count = 0")


# 第4章-8 求分数序列前N项和 (15分)
def demo_4_8():
    n=int(input())
    a,b,res=2,1,0.
    for i in range(n):
        res=res+a/b
        a=a+b 
        b=a-b
    print("%.2f"%res)


# 第4章-9 查询水果价格 (15分)
def demo_4_9():
    print("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit")
    price=[3.,2.5,4.1,10.2]
    nums=list(map(int,input().split()))
    for i in range(len(nums)):
        if nums[i]==0 or i>=5:
            break
        elif nums[i]>0 and nums[i]<5:
            print("price = %.2f"%price[nums[i]-1])
        else:
            print("price = 0.00")
    

# 第4章-10 最大公约数和最小公倍数 (15分)
def gcd(a,b):
    return b if a==0 else gcd(b%a,a)

def demo_4_10():
    m,n=map(int,input().split())
    print("%d %d"%(gcd(m,n),m*n/gcd(m,n)))


# 第4章-11 判断素数 (20分)
def demo_4_11():
    n=int(input())
    for i in range(n):
        if is_prime(int(input())):
            print("Yes")
        else:
            print("No")


# 第4章-12 求满足条件的斐波那契数 (30分)
def demo_4_12():
    n=int(input())
    a,b=0,1
    while b<n:    
        b=a+b 
        a=b-a
    print(b)


# 第4章-13 求误差小于输入值的e的近似值 (20分)
# import math 
def demo_4_13():
    err=float(input())
    a,b=0.,1.
    for i in range(1,10000):
        if b-a>err:
            a=b
            b=b+1/math.factorial(i) 
        else:
            break
    print("%.6f"%b)


# 第4章-14 统计字符 (15分)
# case 2是10个blank
def demo_4_14():
    letter,blank,digit,other,cnt=0,0,0,0,0
    while 1:
        a=list(input())
        for x in a:
            if x.isalpha():
                letter=letter+1
            elif x.isspace():
                blank=blank+1
            elif x.isdigit():
                digit=digit+1
            else:
                other=other+1
        cnt=cnt+len(a)+1
        blank=blank+1 #每一行有个回车 
        if cnt>10:
            break
    print("letter = %d, blank = %d, digit = %d, other = %d"%(letter,blank-1,digit,other))


# 第4章-15 换硬币 (20分)
def demo_4_15():
    n=int(input())
    cnt=0
    for fen5 in range(n//5,0,-1):
        for fen2 in range(n//2,0,-1):
            for fen1 in range(n,0,-1):
                if (fen5*5+fen2*2+fen1 == n):
                    print('fen5:%d, fen2:%d, fen1:%d, total:%d'%(fen5, fen2, fen1, fen1+fen2+fen5))
                    cnt=cnt+1
    print('count = %d'%cnt)


# 第4章-16 jmu-python-判断是否构成三角形 (10分)
def demo_4_16():
    nums=list(map(int,input().split()))
    if sum(nums)-2*max(nums)>0:
        print("yes")
    else:
        print("no")


# 第4章-17 水仙花数(20 分) (20分)
def demo_4_17():
    n=int(input())
    for i in range(pow(10,n-1),pow(10,n)):
        sum=0
        for x in str(i):
            sum=sum+pow(int(x),n)
        if sum==i:
            print(i)


# 第4章-18 猴子选大王 (20分)
def demo_4_17():
    n=int(input())
    a=list('0')*n
    index,cnt,last=0,0,0
    while '0' in a:
        if a[index]!='1':
            cnt=cnt+1
            if cnt>=3:
                cnt=0
                a[index]='1'
                last=index
        index=(index+1)%n   
    print(last+1)


# 第4章-19 矩阵运算 (20分)
def demo_4_19():
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(input().split())
        matrix.append(a)
    sum=0
    for i in range(n):
        for j in range(n):
            if i!=n-1 and j!=n-1 and i+j!=n-1:
                sum=sum+int(matrix[i][j])
    print(sum)    


# 第4章-20 求矩阵各行元素之和 (15分)
def demo_4_20():
    m,n=map(int,input().split())
    for i in range(m):
        print(sum([int(x) for x in input().split()]))


# 第4章-21 判断上三角矩阵 (15分)
def demo_4_21():
    n=int(input())
    for x in range(n):
        size=int(input())
        matrix=[]
        flag=True
        for i in range(size):
            a=list(input().split())
            matrix.append(a)
        for i in range(1,size):
            for j in range(i):
                if matrix[i][j]!='0':
                    flag=False
        print("YES"if flag else "NO")  


# 第4章-22 找鞍点 (20分)
def demo_4_22():
    import numpy as np
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(map(int,input().split()))
        matrix.append(a)
    npmat=np.array(matrix)
    for i in range(n):
        for j in range(n):
            if npmat.max(1)[i]==npmat.min(0)[j]:
                print(i,j)
                exit()
    print("NONE")


# 第4章-23 求矩阵的局部极大值 (15分)
def demo_4_23():
    m,n=map(int,input().split())
    matrix=[]
    for i in range(m):
        a=list(map(int,input().split()))
        matrix.append(a)
    flag=True
    for i in range(1,m-1):
        for j in range(1,n-1):
            if matrix[i][j]>matrix[i][j-1] and matrix[i][j]>matrix[i-1][j] and \
            matrix[i][j]>matrix[i+1][j] and matrix[i][j]>matrix[i][j+1] :
                print(matrix[i][j],i+1,j+1)
                flag=False
    if flag:
        print("None",m,n)


# 第4章-24 打印九九口诀表 (15分)
def demo_4_24():
    n=int(input())
    for i in range(1,n+1):
        for j in range(1,i+1):
            print("{:d}*{:d}={:<4d}".format(j,i,i*j),end="")
        print("")


# 第4章-25 输出三角形字符阵列 (15分)
def demo_4_25():
    n=int(input())
    c=ord("A")
    for i in range(n):
        for j in range(n-i):
            print("%c"%c,end=" ")   
            c=c+1     
        print("")


# 第4章-26 求1!+3!+5!+……+n! (10分)
def demo_4_26():
    import math
    n=int(input())
    sum=0
    for i in range(1,n+1,2):
        sum=sum+math.factorial(i) 
    print("n=%d,s=%d"%(n,sum))


# 第4章-27 二维数组中每行最大值和每行和 (10分)
def demo_4_27():
    nums=list(map(int,input().split()))
    for i in range(0,8,3):
        abc=nums[i:i+3]
        print("%4d%4d%4d%4d%4d"%(abc[0],abc[1],abc[2],max(abc),sum(abc)))


# 第4章-28 矩阵转置 (10分)
def demo_4_28():
    nums=list(map(int,input().split()))
    for i in range(3):
        print("%4d%4d%4d"%(nums[i+0],nums[i+3],nums[i+6]))


# 第4章-29 找出不是两个数组共有的元素 (20分)
def demo_4_29():
    nums1=list(input().split())
    nums2=list(input().split())
    nums1.pop(0)
    nums2.pop(0)
    out=[]
    for x in nums1:
        if x not in nums2 and x not in out:
            out.append(x)
    for x in nums2:
        if x not in nums1 and x not in out:  
            out.append(x)   
    print(" ".join(out))


# 第4章-30 找完数 (20分)
def demo_4_30():
    import math
    m,n=map(int,input().split())
    none=True
    for i in range(m,n+1):
        factor=[1,]
        for f in range(2,int(math.sqrt(i)+1)):#不然会超时
            if i//f==i/f:
                factor.append(int(f))
                factor.append(int(i/f))#同时记录对称因子
        if sum(factor)==i:
            none=False
            print("%d = "%i,end="")
            print(" + ".join("%s"%x for x in sorted(factor)))#对称因子导致不按顺序
    if none:
        print("None")
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
def is_prime(x):
    if x<2:
        return False
    elif x==2:
        return True
    else :
        for i in range(2,int(math.sqrt(x))+1):
            if x%i==0:
                return False
    return True
    
# 第4章-1 生成3的乘方表 (15分)
def demo_4_1():
    n=int(input())
    for i in range(n+1):
        print("pow(3,%d) = %d"%(i,pow(3,i)))


# 第4章-2 统计素数并求和 (20分)
def demo_4_2():
    m,n=map(int,input().split())
    cnt,sum=0,0
    for i in range(m,n+1):
        if is_prime(i):
            cnt=cnt+1
            sum=sum+i 
    print(cnt,sum)


# 第4章-3 猴子吃桃问题 (15分)
def demo_4_3():
    n=int(input())
    res=1
    for i in range(n-1):
        res=(res+1)*2
    print(res)


# 第4章-4 验证“哥德巴赫猜想” (20分)
def demo_4_4():
    n=int(input())
    for i in range(2,1000000001):
        if is_prime(i) and is_prime(n-i):
            print("%d = %d + %d"%(n,i,n-i))
            break
    

# 第4章-5 求e的近似值 (15分)
def demo_4_5():
    n=int(input())
    print("%.8f"%(sum(1/math.factorial(x) for x in range(n+1))))


# 第4章-6 输出前 n 个Fibonacci数 (15分)
def demo_4_6():
    n=int(input())
    a,b=0,1
    if n<1:
        print("Invalid.")
    else:
        for i in range(1,n+1):
            print("{:>11}".format(b),end="")
            b=a+b 
            a=b-a
            if i%5==0:
                print("")
    

# 第4章-7 统计学生平均成绩与及格人数 (15分)
def demo_4_7():
    if int(input())>0:
        scores=list(map(int,input().split()))
        print("average = %.1f"%(sum(scores)/len(scores)))
        print("count = %d"%(len([x for x in scores if x >= 60])))
    else:    
        print("average = 0.0")
        print("count = 0")


# 第4章-8 求分数序列前N项和 (15分)
def demo_4_8():
    n=int(input())
    a,b,res=2,1,0.
    for i in range(n):
        res=res+a/b
        a=a+b 
        b=a-b
    print("%.2f"%res)


# 第4章-9 查询水果价格 (15分)
def demo_4_9():
    print("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit")
    price=[3.,2.5,4.1,10.2]
    nums=list(map(int,input().split()))
    for i in range(len(nums)):
        if nums[i]==0 or i>=5:
            break
        elif nums[i]>0 and nums[i]<5:
            print("price = %.2f"%price[nums[i]-1])
        else:
            print("price = 0.00")
    

# 第4章-10 最大公约数和最小公倍数 (15分)
def gcd(a,b):
    return b if a==0 else gcd(b%a,a)

def demo_4_10():
    m,n=map(int,input().split())
    print("%d %d"%(gcd(m,n),m*n/gcd(m,n)))


# 第4章-11 判断素数 (20分)
def demo_4_11():
    n=int(input())
    for i in range(n):
        if is_prime(int(input())):
            print("Yes")
        else:
            print("No")


# 第4章-12 求满足条件的斐波那契数 (30分)
def demo_4_12():
    n=int(input())
    a,b=0,1
    while b<n:    
        b=a+b 
        a=b-a
    print(b)


# 第4章-13 求误差小于输入值的e的近似值 (20分)
# import math 
def demo_4_13():
    err=float(input())
    a,b=0.,1.
    for i in range(1,10000):
        if b-a>err:
            a=b
            b=b+1/math.factorial(i) 
        else:
            break
    print("%.6f"%b)


# 第4章-14 统计字符 (15分)
# case 2是10个blank
def demo_4_14():
    letter,blank,digit,other,cnt=0,0,0,0,0
    while 1:
        a=list(input())
        for x in a:
            if x.isalpha():
                letter=letter+1
            elif x.isspace():
                blank=blank+1
            elif x.isdigit():
                digit=digit+1
            else:
                other=other+1
        cnt=cnt+len(a)+1
        blank=blank+1 #每一行有个回车 
        if cnt>10:
            break
    print("letter = %d, blank = %d, digit = %d, other = %d"%(letter,blank-1,digit,other))


# 第4章-15 换硬币 (20分)
def demo_4_15():
    n=int(input())
    cnt=0
    for fen5 in range(n//5,0,-1):
        for fen2 in range(n//2,0,-1):
            for fen1 in range(n,0,-1):
                if (fen5*5+fen2*2+fen1 == n):
                    print('fen5:%d, fen2:%d, fen1:%d, total:%d'%(fen5, fen2, fen1, fen1+fen2+fen5))
                    cnt=cnt+1
    print('count = %d'%cnt)


# 第4章-16 jmu-python-判断是否构成三角形 (10分)
def demo_4_16():
    nums=list(map(int,input().split()))
    if sum(nums)-2*max(nums)>0:
        print("yes")
    else:
        print("no")


# 第4章-17 水仙花数(20 分) (20分)
def demo_4_17():
    n=int(input())
    for i in range(pow(10,n-1),pow(10,n)):
        sum=0
        for x in str(i):
            sum=sum+pow(int(x),n)
        if sum==i:
            print(i)


# 第4章-18 猴子选大王 (20分)
def demo_4_17():
    n=int(input())
    a=list('0')*n
    index,cnt,last=0,0,0
    while '0' in a:
        if a[index]!='1':
            cnt=cnt+1
            if cnt>=3:
                cnt=0
                a[index]='1'
                last=index
        index=(index+1)%n   
    print(last+1)


# 第4章-19 矩阵运算 (20分)
def demo_4_19():
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(input().split())
        matrix.append(a)
    sum=0
    for i in range(n):
        for j in range(n):
            if i!=n-1 and j!=n-1 and i+j!=n-1:
                sum=sum+int(matrix[i][j])
    print(sum)    


# 第4章-20 求矩阵各行元素之和 (15分)
def demo_4_20():
    m,n=map(int,input().split())
    for i in range(m):
        print(sum([int(x) for x in input().split()]))


# 第4章-21 判断上三角矩阵 (15分)
def demo_4_21():
    n=int(input())
    for x in range(n):
        size=int(input())
        matrix=[]
        flag=True
        for i in range(size):
            a=list(input().split())
            matrix.append(a)
        for i in range(1,size):
            for j in range(i):
                if matrix[i][j]!='0':
                    flag=False
        print("YES"if flag else "NO")  


# 第4章-22 找鞍点 (20分)
def demo_4_22():
    import numpy as np
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(map(int,input().split()))
        matrix.append(a)
    npmat=np.array(matrix)
    for i in range(n):
        for j in range(n):
            if npmat.max(1)[i]==npmat.min(0)[j]:
                print(i,j)
                exit()
    print("NONE")


# 第4章-23 求矩阵的局部极大值 (15分)
def demo_4_23():
    m,n=map(int,input().split())
    matrix=[]
    for i in range(m):
        a=list(map(int,input().split()))
        matrix.append(a)
    flag=True
    for i in range(1,m-1):
        for j in range(1,n-1):
            if matrix[i][j]>matrix[i][j-1] and matrix[i][j]>matrix[i-1][j] and \
            matrix[i][j]>matrix[i+1][j] and matrix[i][j]>matrix[i][j+1] :
                print(matrix[i][j],i+1,j+1)
                flag=False
    if flag:
        print("None",m,n)


# 第4章-24 打印九九口诀表 (15分)
def demo_4_24():
    n=int(input())
    for i in range(1,n+1):
        for j in range(1,i+1):
            print("{:d}*{:d}={:<4d}".format(j,i,i*j),end="")
        print("")


# 第4章-25 输出三角形字符阵列 (15分)
def demo_4_25():
    n=int(input())
    c=ord("A")
    for i in range(n):
        for j in range(n-i):
            print("%c"%c,end=" ")   
            c=c+1     
        print("")


# 第4章-26 求1!+3!+5!+……+n! (10分)
def demo_4_26():
    import math
    n=int(input())
    sum=0
    for i in range(1,n+1,2):
        sum=sum+math.factorial(i) 
    print("n=%d,s=%d"%(n,sum))


# 第4章-27 二维数组中每行最大值和每行和 (10分)
def demo_4_27():
    nums=list(map(int,input().split()))
    for i in range(0,8,3):
        abc=nums[i:i+3]
        print("%4d%4d%4d%4d%4d"%(abc[0],abc[1],abc[2],max(abc),sum(abc)))


# 第4章-28 矩阵转置 (10分)
def demo_4_28():
    nums=list(map(int,input().split()))
    for i in range(3):
        print("%4d%4d%4d"%(nums[i+0],nums[i+3],nums[i+6]))


# 第4章-29 找出不是两个数组共有的元素 (20分)
def demo_4_29():
    nums1=list(input().split())
    nums2=list(input().split())
    nums1.pop(0)
    nums2.pop(0)
    out=[]
    for x in nums1:
        if x not in nums2 and x not in out:
            out.append(x)
    for x in nums2:
        if x not in nums1 and x not in out:  
            out.append(x)   
    print(" ".join(out))


# 第4章-30 找完数 (20分)
def demo_4_30():
    import math
    m,n=map(int,input().split())
    none=True
    for i in range(m,n+1):
        factor=[1,]
        for f in range(2,int(math.sqrt(i)+1)):#不然会超时
            if i//f==i/f:
                factor.append(int(f))
                factor.append(int(i/f))#同时记录对称因子
        if sum(factor)==i:
            none=False
            print("%d = "%i,end="")
            print(" + ".join("%s"%x for x in sorted(factor)))#对称因子导致不按顺序
    if none:
        print("None")
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
def is_prime(x):
    if x<2:
        return False
    elif x==2:
        return True
    else :
        for i in range(2,int(math.sqrt(x))+1):
            if x%i==0:
                return False
    return True
    
# 第4章-1 生成3的乘方表 (15分)
def demo_4_1():
    n=int(input())
    for i in range(n+1):
        print("pow(3,%d) = %d"%(i,pow(3,i)))


# 第4章-2 统计素数并求和 (20分)
def demo_4_2():
    m,n=map(int,input().split())
    cnt,sum=0,0
    for i in range(m,n+1):
        if is_prime(i):
            cnt=cnt+1
            sum=sum+i 
    print(cnt,sum)


# 第4章-3 猴子吃桃问题 (15分)
def demo_4_3():
    n=int(input())
    res=1
    for i in range(n-1):
        res=(res+1)*2
    print(res)


# 第4章-4 验证“哥德巴赫猜想” (20分)
def demo_4_4():
    n=int(input())
    for i in range(2,1000000001):
        if is_prime(i) and is_prime(n-i):
            print("%d = %d + %d"%(n,i,n-i))
            break
    

# 第4章-5 求e的近似值 (15分)
def demo_4_5():
    n=int(input())
    print("%.8f"%(sum(1/math.factorial(x) for x in range(n+1))))


# 第4章-6 输出前 n 个Fibonacci数 (15分)
def demo_4_6():
    n=int(input())
    a,b=0,1
    if n<1:
        print("Invalid.")
    else:
        for i in range(1,n+1):
            print("{:>11}".format(b),end="")
            b=a+b 
            a=b-a
            if i%5==0:
                print("")
    

# 第4章-7 统计学生平均成绩与及格人数 (15分)
def demo_4_7():
    if int(input())>0:
        scores=list(map(int,input().split()))
        print("average = %.1f"%(sum(scores)/len(scores)))
        print("count = %d"%(len([x for x in scores if x >= 60])))
    else:    
        print("average = 0.0")
        print("count = 0")


# 第4章-8 求分数序列前N项和 (15分)
def demo_4_8():
    n=int(input())
    a,b,res=2,1,0.
    for i in range(n):
        res=res+a/b
        a=a+b 
        b=a-b
    print("%.2f"%res)


# 第4章-9 查询水果价格 (15分)
def demo_4_9():
    print("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit")
    price=[3.,2.5,4.1,10.2]
    nums=list(map(int,input().split()))
    for i in range(len(nums)):
        if nums[i]==0 or i>=5:
            break
        elif nums[i]>0 and nums[i]<5:
            print("price = %.2f"%price[nums[i]-1])
        else:
            print("price = 0.00")
    

# 第4章-10 最大公约数和最小公倍数 (15分)
def gcd(a,b):
    return b if a==0 else gcd(b%a,a)

def demo_4_10():
    m,n=map(int,input().split())
    print("%d %d"%(gcd(m,n),m*n/gcd(m,n)))


# 第4章-11 判断素数 (20分)
def demo_4_11():
    n=int(input())
    for i in range(n):
        if is_prime(int(input())):
            print("Yes")
        else:
            print("No")


# 第4章-12 求满足条件的斐波那契数 (30分)
def demo_4_12():
    n=int(input())
    a,b=0,1
    while b<n:    
        b=a+b 
        a=b-a
    print(b)


# 第4章-13 求误差小于输入值的e的近似值 (20分)
# import math 
def demo_4_13():
    err=float(input())
    a,b=0.,1.
    for i in range(1,10000):
        if b-a>err:
            a=b
            b=b+1/math.factorial(i) 
        else:
            break
    print("%.6f"%b)


# 第4章-14 统计字符 (15分)
# case 2是10个blank
def demo_4_14():
    letter,blank,digit,other,cnt=0,0,0,0,0
    while 1:
        a=list(input())
        for x in a:
            if x.isalpha():
                letter=letter+1
            elif x.isspace():
                blank=blank+1
            elif x.isdigit():
                digit=digit+1
            else:
                other=other+1
        cnt=cnt+len(a)+1
        blank=blank+1 #每一行有个回车 
        if cnt>10:
            break
    print("letter = %d, blank = %d, digit = %d, other = %d"%(letter,blank-1,digit,other))


# 第4章-15 换硬币 (20分)
def demo_4_15():
    n=int(input())
    cnt=0
    for fen5 in range(n//5,0,-1):
        for fen2 in range(n//2,0,-1):
            for fen1 in range(n,0,-1):
                if (fen5*5+fen2*2+fen1 == n):
                    print('fen5:%d, fen2:%d, fen1:%d, total:%d'%(fen5, fen2, fen1, fen1+fen2+fen5))
                    cnt=cnt+1
    print('count = %d'%cnt)


# 第4章-16 jmu-python-判断是否构成三角形 (10分)
def demo_4_16():
    nums=list(map(int,input().split()))
    if sum(nums)-2*max(nums)>0:
        print("yes")
    else:
        print("no")


# 第4章-17 水仙花数(20 分) (20分)
def demo_4_17():
    n=int(input())
    for i in range(pow(10,n-1),pow(10,n)):
        sum=0
        for x in str(i):
            sum=sum+pow(int(x),n)
        if sum==i:
            print(i)


# 第4章-18 猴子选大王 (20分)
def demo_4_17():
    n=int(input())
    a=list('0')*n
    index,cnt,last=0,0,0
    while '0' in a:
        if a[index]!='1':
            cnt=cnt+1
            if cnt>=3:
                cnt=0
                a[index]='1'
                last=index
        index=(index+1)%n   
    print(last+1)


# 第4章-19 矩阵运算 (20分)
def demo_4_19():
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(input().split())
        matrix.append(a)
    sum=0
    for i in range(n):
        for j in range(n):
            if i!=n-1 and j!=n-1 and i+j!=n-1:
                sum=sum+int(matrix[i][j])
    print(sum)    


# 第4章-20 求矩阵各行元素之和 (15分)
def demo_4_20():
    m,n=map(int,input().split())
    for i in range(m):
        print(sum([int(x) for x in input().split()]))


# 第4章-21 判断上三角矩阵 (15分)
def demo_4_21():
    n=int(input())
    for x in range(n):
        size=int(input())
        matrix=[]
        flag=True
        for i in range(size):
            a=list(input().split())
            matrix.append(a)
        for i in range(1,size):
            for j in range(i):
                if matrix[i][j]!='0':
                    flag=False
        print("YES"if flag else "NO")  


# 第4章-22 找鞍点 (20分)
def demo_4_22():
    import numpy as np
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(map(int,input().split()))
        matrix.append(a)
    npmat=np.array(matrix)
    for i in range(n):
        for j in range(n):
            if npmat.max(1)[i]==npmat.min(0)[j]:
                print(i,j)
                exit()
    print("NONE")


# 第4章-23 求矩阵的局部极大值 (15分)
def demo_4_23():
    m,n=map(int,input().split())
    matrix=[]
    for i in range(m):
        a=list(map(int,input().split()))
        matrix.append(a)
    flag=True
    for i in range(1,m-1):
        for j in range(1,n-1):
            if matrix[i][j]>matrix[i][j-1] and matrix[i][j]>matrix[i-1][j] and \
            matrix[i][j]>matrix[i+1][j] and matrix[i][j]>matrix[i][j+1] :
                print(matrix[i][j],i+1,j+1)
                flag=False
    if flag:
        print("None",m,n)


# 第4章-24 打印九九口诀表 (15分)
def demo_4_24():
    n=int(input())
    for i in range(1,n+1):
        for j in range(1,i+1):
            print("{:d}*{:d}={:<4d}".format(j,i,i*j),end="")
        print("")


# 第4章-25 输出三角形字符阵列 (15分)
def demo_4_25():
    n=int(input())
    c=ord("A")
    for i in range(n):
        for j in range(n-i):
            print("%c"%c,end=" ")   
            c=c+1     
        print("")


# 第4章-26 求1!+3!+5!+……+n! (10分)
def demo_4_26():
    import math
    n=int(input())
    sum=0
    for i in range(1,n+1,2):
        sum=sum+math.factorial(i) 
    print("n=%d,s=%d"%(n,sum))


# 第4章-27 二维数组中每行最大值和每行和 (10分)
def demo_4_27():
    nums=list(map(int,input().split()))
    for i in range(0,8,3):
        abc=nums[i:i+3]
        print("%4d%4d%4d%4d%4d"%(abc[0],abc[1],abc[2],max(abc),sum(abc)))


# 第4章-28 矩阵转置 (10分)
def demo_4_28():
    nums=list(map(int,input().split()))
    for i in range(3):
        print("%4d%4d%4d"%(nums[i+0],nums[i+3],nums[i+6]))


# 第4章-29 找出不是两个数组共有的元素 (20分)
def demo_4_29():
    nums1=list(input().split())
    nums2=list(input().split())
    nums1.pop(0)
    nums2.pop(0)
    out=[]
    for x in nums1:
        if x not in nums2 and x not in out:
            out.append(x)
    for x in nums2:
        if x not in nums1 and x not in out:  
            out.append(x)   
    print(" ".join(out))


# 第4章-30 找完数 (20分)
def demo_4_30():
    import math
    m,n=map(int,input().split())
    none=True
    for i in range(m,n+1):
        factor=[1,]
        for f in range(2,int(math.sqrt(i)+1)):#不然会超时
            if i//f==i/f:
                factor.append(int(f))
                factor.append(int(i/f))#同时记录对称因子
        if sum(factor)==i:
            none=False
            print("%d = "%i,end="")
            print(" + ".join("%s"%x for x in sorted(factor)))#对称因子导致不按顺序
    if none:
        print("None")
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
def is_prime(x):
    if x<2:
        return False
    elif x==2:
        return True
    else :
        for i in range(2,int(math.sqrt(x))+1):
            if x%i==0:
                return False
    return True
    
# 第4章-1 生成3的乘方表 (15分)
def demo_4_1():
    n=int(input())
    for i in range(n+1):
        print("pow(3,%d) = %d"%(i,pow(3,i)))


# 第4章-2 统计素数并求和 (20分)
def demo_4_2():
    m,n=map(int,input().split())
    cnt,sum=0,0
    for i in range(m,n+1):
        if is_prime(i):
            cnt=cnt+1
            sum=sum+i 
    print(cnt,sum)


# 第4章-3 猴子吃桃问题 (15分)
def demo_4_3():
    n=int(input())
    res=1
    for i in range(n-1):
        res=(res+1)*2
    print(res)


# 第4章-4 验证“哥德巴赫猜想” (20分)
def demo_4_4():
    n=int(input())
    for i in range(2,1000000001):
        if is_prime(i) and is_prime(n-i):
            print("%d = %d + %d"%(n,i,n-i))
            break
    

# 第4章-5 求e的近似值 (15分)
def demo_4_5():
    n=int(input())
    print("%.8f"%(sum(1/math.factorial(x) for x in range(n+1))))


# 第4章-6 输出前 n 个Fibonacci数 (15分)
def demo_4_6():
    n=int(input())
    a,b=0,1
    if n<1:
        print("Invalid.")
    else:
        for i in range(1,n+1):
            print("{:>11}".format(b),end="")
            b=a+b 
            a=b-a
            if i%5==0:
                print("")
    

# 第4章-7 统计学生平均成绩与及格人数 (15分)
def demo_4_7():
    if int(input())>0:
        scores=list(map(int,input().split()))
        print("average = %.1f"%(sum(scores)/len(scores)))
        print("count = %d"%(len([x for x in scores if x >= 60])))
    else:    
        print("average = 0.0")
        print("count = 0")


# 第4章-8 求分数序列前N项和 (15分)
def demo_4_8():
    n=int(input())
    a,b,res=2,1,0.
    for i in range(n):
        res=res+a/b
        a=a+b 
        b=a-b
    print("%.2f"%res)


# 第4章-9 查询水果价格 (15分)
def demo_4_9():
    print("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit")
    price=[3.,2.5,4.1,10.2]
    nums=list(map(int,input().split()))
    for i in range(len(nums)):
        if nums[i]==0 or i>=5:
            break
        elif nums[i]>0 and nums[i]<5:
            print("price = %.2f"%price[nums[i]-1])
        else:
            print("price = 0.00")
    

# 第4章-10 最大公约数和最小公倍数 (15分)
def gcd(a,b):
    return b if a==0 else gcd(b%a,a)

def demo_4_10():
    m,n=map(int,input().split())
    print("%d %d"%(gcd(m,n),m*n/gcd(m,n)))


# 第4章-11 判断素数 (20分)
def demo_4_11():
    n=int(input())
    for i in range(n):
        if is_prime(int(input())):
            print("Yes")
        else:
            print("No")


# 第4章-12 求满足条件的斐波那契数 (30分)
def demo_4_12():
    n=int(input())
    a,b=0,1
    while b<n:    
        b=a+b 
        a=b-a
    print(b)


# 第4章-13 求误差小于输入值的e的近似值 (20分)
# import math 
def demo_4_13():
    err=float(input())
    a,b=0.,1.
    for i in range(1,10000):
        if b-a>err:
            a=b
            b=b+1/math.factorial(i) 
        else:
            break
    print("%.6f"%b)


# 第4章-14 统计字符 (15分)
# case 2是10个blank
def demo_4_14():
    letter,blank,digit,other,cnt=0,0,0,0,0
    while 1:
        a=list(input())
        for x in a:
            if x.isalpha():
                letter=letter+1
            elif x.isspace():
                blank=blank+1
            elif x.isdigit():
                digit=digit+1
            else:
                other=other+1
        cnt=cnt+len(a)+1
        blank=blank+1 #每一行有个回车 
        if cnt>10:
            break
    print("letter = %d, blank = %d, digit = %d, other = %d"%(letter,blank-1,digit,other))


# 第4章-15 换硬币 (20分)
def demo_4_15():
    n=int(input())
    cnt=0
    for fen5 in range(n//5,0,-1):
        for fen2 in range(n//2,0,-1):
            for fen1 in range(n,0,-1):
                if (fen5*5+fen2*2+fen1 == n):
                    print('fen5:%d, fen2:%d, fen1:%d, total:%d'%(fen5, fen2, fen1, fen1+fen2+fen5))
                    cnt=cnt+1
    print('count = %d'%cnt)


# 第4章-16 jmu-python-判断是否构成三角形 (10分)
def demo_4_16():
    nums=list(map(int,input().split()))
    if sum(nums)-2*max(nums)>0:
        print("yes")
    else:
        print("no")


# 第4章-17 水仙花数(20 分) (20分)
def demo_4_17():
    n=int(input())
    for i in range(pow(10,n-1),pow(10,n)):
        sum=0
        for x in str(i):
            sum=sum+pow(int(x),n)
        if sum==i:
            print(i)


# 第4章-18 猴子选大王 (20分)
def demo_4_17():
    n=int(input())
    a=list('0')*n
    index,cnt,last=0,0,0
    while '0' in a:
        if a[index]!='1':
            cnt=cnt+1
            if cnt>=3:
                cnt=0
                a[index]='1'
                last=index
        index=(index+1)%n   
    print(last+1)


# 第4章-19 矩阵运算 (20分)
def demo_4_19():
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(input().split())
        matrix.append(a)
    sum=0
    for i in range(n):
        for j in range(n):
            if i!=n-1 and j!=n-1 and i+j!=n-1:
                sum=sum+int(matrix[i][j])
    print(sum)    


# 第4章-20 求矩阵各行元素之和 (15分)
def demo_4_20():
    m,n=map(int,input().split())
    for i in range(m):
        print(sum([int(x) for x in input().split()]))


# 第4章-21 判断上三角矩阵 (15分)
def demo_4_21():
    n=int(input())
    for x in range(n):
        size=int(input())
        matrix=[]
        flag=True
        for i in range(size):
            a=list(input().split())
            matrix.append(a)
        for i in range(1,size):
            for j in range(i):
                if matrix[i][j]!='0':
                    flag=False
        print("YES"if flag else "NO")  


# 第4章-22 找鞍点 (20分)
def demo_4_22():
    import numpy as np
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(map(int,input().split()))
        matrix.append(a)
    npmat=np.array(matrix)
    for i in range(n):
        for j in range(n):
            if npmat.max(1)[i]==npmat.min(0)[j]:
                print(i,j)
                exit()
    print("NONE")


# 第4章-23 求矩阵的局部极大值 (15分)
def demo_4_23():
    m,n=map(int,input().split())
    matrix=[]
    for i in range(m):
        a=list(map(int,input().split()))
        matrix.append(a)
    flag=True
    for i in range(1,m-1):
        for j in range(1,n-1):
            if matrix[i][j]>matrix[i][j-1] and matrix[i][j]>matrix[i-1][j] and \
            matrix[i][j]>matrix[i+1][j] and matrix[i][j]>matrix[i][j+1] :
                print(matrix[i][j],i+1,j+1)
                flag=False
    if flag:
        print("None",m,n)


# 第4章-24 打印九九口诀表 (15分)
def demo_4_24():
    n=int(input())
    for i in range(1,n+1):
        for j in range(1,i+1):
            print("{:d}*{:d}={:<4d}".format(j,i,i*j),end="")
        print("")


# 第4章-25 输出三角形字符阵列 (15分)
def demo_4_25():
    n=int(input())
    c=ord("A")
    for i in range(n):
        for j in range(n-i):
            print("%c"%c,end=" ")   
            c=c+1     
        print("")


# 第4章-26 求1!+3!+5!+……+n! (10分)
def demo_4_26():
    import math
    n=int(input())
    sum=0
    for i in range(1,n+1,2):
        sum=sum+math.factorial(i) 
    print("n=%d,s=%d"%(n,sum))


# 第4章-27 二维数组中每行最大值和每行和 (10分)
def demo_4_27():
    nums=list(map(int,input().split()))
    for i in range(0,8,3):
        abc=nums[i:i+3]
        print("%4d%4d%4d%4d%4d"%(abc[0],abc[1],abc[2],max(abc),sum(abc)))


# 第4章-28 矩阵转置 (10分)
def demo_4_28():
    nums=list(map(int,input().split()))
    for i in range(3):
        print("%4d%4d%4d"%(nums[i+0],nums[i+3],nums[i+6]))


# 第4章-29 找出不是两个数组共有的元素 (20分)
def demo_4_29():
    nums1=list(input().split())
    nums2=list(input().split())
    nums1.pop(0)
    nums2.pop(0)
    out=[]
    for x in nums1:
        if x not in nums2 and x not in out:
            out.append(x)
    for x in nums2:
        if x not in nums1 and x not in out:  
            out.append(x)   
    print(" ".join(out))


# 第4章-30 找完数 (20分)
def demo_4_30():
    import math
    m,n=map(int,input().split())
    none=True
    for i in range(m,n+1):
        factor=[1,]
        for f in range(2,int(math.sqrt(i)+1)):#不然会超时
            if i//f==i/f:
                factor.append(int(f))
                factor.append(int(i/f))#同时记录对称因子
        if sum(factor)==i:
            none=False
            print("%d = "%i,end="")
            print(" + ".join("%s"%x for x in sorted(factor)))#对称因子导致不按顺序
    if none:
        print("None")
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
def is_prime(x):
    if x<2:
        return False
    elif x==2:
        return True
    else :
        for i in range(2,int(math.sqrt(x))+1):
            if x%i==0:
                return False
    return True
    
# 第4章-1 生成3的乘方表 (15分)
def demo_4_1():
    n=int(input())
    for i in range(n+1):
        print("pow(3,%d) = %d"%(i,pow(3,i)))


# 第4章-2 统计素数并求和 (20分)
def demo_4_2():
    m,n=map(int,input().split())
    cnt,sum=0,0
    for i in range(m,n+1):
        if is_prime(i):
            cnt=cnt+1
            sum=sum+i 
    print(cnt,sum)


# 第4章-3 猴子吃桃问题 (15分)
def demo_4_3():
    n=int(input())
    res=1
    for i in range(n-1):
        res=(res+1)*2
    print(res)


# 第4章-4 验证“哥德巴赫猜想” (20分)
def demo_4_4():
    n=int(input())
    for i in range(2,1000000001):
        if is_prime(i) and is_prime(n-i):
            print("%d = %d + %d"%(n,i,n-i))
            break
    

# 第4章-5 求e的近似值 (15分)
def demo_4_5():
    n=int(input())
    print("%.8f"%(sum(1/math.factorial(x) for x in range(n+1))))


# 第4章-6 输出前 n 个Fibonacci数 (15分)
def demo_4_6():
    n=int(input())
    a,b=0,1
    if n<1:
        print("Invalid.")
    else:
        for i in range(1,n+1):
            print("{:>11}".format(b),end="")
            b=a+b 
            a=b-a
            if i%5==0:
                print("")
    

# 第4章-7 统计学生平均成绩与及格人数 (15分)
def demo_4_7():
    if int(input())>0:
        scores=list(map(int,input().split()))
        print("average = %.1f"%(sum(scores)/len(scores)))
        print("count = %d"%(len([x for x in scores if x >= 60])))
    else:    
        print("average = 0.0")
        print("count = 0")


# 第4章-8 求分数序列前N项和 (15分)
def demo_4_8():
    n=int(input())
    a,b,res=2,1,0.
    for i in range(n):
        res=res+a/b
        a=a+b 
        b=a-b
    print("%.2f"%res)


# 第4章-9 查询水果价格 (15分)
def demo_4_9():
    print("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit")
    price=[3.,2.5,4.1,10.2]
    nums=list(map(int,input().split()))
    for i in range(len(nums)):
        if nums[i]==0 or i>=5:
            break
        elif nums[i]>0 and nums[i]<5:
            print("price = %.2f"%price[nums[i]-1])
        else:
            print("price = 0.00")
    

# 第4章-10 最大公约数和最小公倍数 (15分)
def gcd(a,b):
    return b if a==0 else gcd(b%a,a)

def demo_4_10():
    m,n=map(int,input().split())
    print("%d %d"%(gcd(m,n),m*n/gcd(m,n)))


# 第4章-11 判断素数 (20分)
def demo_4_11():
    n=int(input())
    for i in range(n):
        if is_prime(int(input())):
            print("Yes")
        else:
            print("No")


# 第4章-12 求满足条件的斐波那契数 (30分)
def demo_4_12():
    n=int(input())
    a,b=0,1
    while b<n:    
        b=a+b 
        a=b-a
    print(b)


# 第4章-13 求误差小于输入值的e的近似值 (20分)
# import math 
def demo_4_13():
    err=float(input())
    a,b=0.,1.
    for i in range(1,10000):
        if b-a>err:
            a=b
            b=b+1/math.factorial(i) 
        else:
            break
    print("%.6f"%b)


# 第4章-14 统计字符 (15分)
# case 2是10个blank
def demo_4_14():
    letter,blank,digit,other,cnt=0,0,0,0,0
    while 1:
        a=list(input())
        for x in a:
            if x.isalpha():
                letter=letter+1
            elif x.isspace():
                blank=blank+1
            elif x.isdigit():
                digit=digit+1
            else:
                other=other+1
        cnt=cnt+len(a)+1
        blank=blank+1 #每一行有个回车 
        if cnt>10:
            break
    print("letter = %d, blank = %d, digit = %d, other = %d"%(letter,blank-1,digit,other))


# 第4章-15 换硬币 (20分)
def demo_4_15():
    n=int(input())
    cnt=0
    for fen5 in range(n//5,0,-1):
        for fen2 in range(n//2,0,-1):
            for fen1 in range(n,0,-1):
                if (fen5*5+fen2*2+fen1 == n):
                    print('fen5:%d, fen2:%d, fen1:%d, total:%d'%(fen5, fen2, fen1, fen1+fen2+fen5))
                    cnt=cnt+1
    print('count = %d'%cnt)


# 第4章-16 jmu-python-判断是否构成三角形 (10分)
def demo_4_16():
    nums=list(map(int,input().split()))
    if sum(nums)-2*max(nums)>0:
        print("yes")
    else:
        print("no")


# 第4章-17 水仙花数(20 分) (20分)
def demo_4_17():
    n=int(input())
    for i in range(pow(10,n-1),pow(10,n)):
        sum=0
        for x in str(i):
            sum=sum+pow(int(x),n)
        if sum==i:
            print(i)


# 第4章-18 猴子选大王 (20分)
def demo_4_17():
    n=int(input())
    a=list('0')*n
    index,cnt,last=0,0,0
    while '0' in a:
        if a[index]!='1':
            cnt=cnt+1
            if cnt>=3:
                cnt=0
                a[index]='1'
                last=index
        index=(index+1)%n   
    print(last+1)


# 第4章-19 矩阵运算 (20分)
def demo_4_19():
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(input().split())
        matrix.append(a)
    sum=0
    for i in range(n):
        for j in range(n):
            if i!=n-1 and j!=n-1 and i+j!=n-1:
                sum=sum+int(matrix[i][j])
    print(sum)    


# 第4章-20 求矩阵各行元素之和 (15分)
def demo_4_20():
    m,n=map(int,input().split())
    for i in range(m):
        print(sum([int(x) for x in input().split()]))


# 第4章-21 判断上三角矩阵 (15分)
def demo_4_21():
    n=int(input())
    for x in range(n):
        size=int(input())
        matrix=[]
        flag=True
        for i in range(size):
            a=list(input().split())
            matrix.append(a)
        for i in range(1,size):
            for j in range(i):
                if matrix[i][j]!='0':
                    flag=False
        print("YES"if flag else "NO")  


# 第4章-22 找鞍点 (20分)
def demo_4_22():
    import numpy as np
    n=int(input())
    matrix=[]
    for i in range(n):
        a=list(map(int,input().split()))
        matrix.append(a)
    npmat=np.array(matrix)
    for i in range(n):
        for j in range(n):
            if npmat.max(1)[i]==npmat.min(0)[j]:
                print(i,j)
                exit()
    print("NONE")


# 第4章-23 求矩阵的局部极大值 (15分)
def demo_4_23():
    m,n=map(int,input().split())
    matrix=[]
    for i in range(m):
        a=list(map(int,input().split()))
        matrix.append(a)
    flag=True
    for i in range(1,m-1):
        for j in range(1,n-1):
            if matrix[i][j]>matrix[i][j-1] and matrix[i][j]>matrix[i-1][j] and \
            matrix[i][j]>matrix[i+1][j] and matrix[i][j]>matrix[i][j+1] :
                print(matrix[i][j],i+1,j+1)
                flag=False
    if flag:
        print("None",m,n)


# 第4章-24 打印九九口诀表 (15分)
def demo_4_24():
    n=int(input())
    for i in range(1,n+1):
        for j in range(1,i+1):
            print("{:d}*{:d}={:<4d}".format(j,i,i*j),end="")
        print("")


# 第4章-25 输出三角形字符阵列 (15分)
def demo_4_25():
    n=int(input())
    c=ord("A")
    for i in range(n):
        for j in range(n-i):
            print("%c"%c,end=" ")   
            c=c+1     
        print("")


# 第4章-26 求1!+3!+5!+……+n! (10分)
def demo_4_26():
    import math
    n=int(input())
    sum=0
    for i in range(1,n+1,2):
        sum=sum+math.factorial(i) 
    print("n=%d,s=%d"%(n,sum))


# 第4章-27 二维数组中每行最大值和每行和 (10分)
def demo_4_27():
    nums=list(map(int,input().split()))
    for i in range(0,8,3):
        abc=nums[i:i+3]
        print("%4d%4d%4d%4d%4d"%(abc[0],abc[1],abc[2],max(abc),sum(abc)))


# 第4章-28 矩阵转置 (10分)
def demo_4_28():
    nums=list(map(int,input().split()))
    for i in range(3):
        print("%4d%4d%4d"%(nums[i+0],nums[i+3],nums[i+6]))


# 第4章-29 找出不是两个数组共有的元素 (20分)
def demo_4_29():
    nums1=list(input().split())
    nums2=list(input().split())
    nums1.pop(0)
    nums2.pop(0)
    out=[]
    for x in nums1:
        if x not in nums2 and x not in out:
            out.append(x)
    for x in nums2:
        if x not in nums1 and x not in out:  
            out.append(x)   
    print(" ".join(out))


# 第4章-30 找完数 (20分)
def demo_4_30():
    import math
    m,n=map(int,input().split())
    none=True
    for i in range(m,n+1):
        factor=[1,]
        for f in range(2,int(math.sqrt(i)+1)):#不然会超时
            if i//f==i/f:
                factor.append(int(f))
                factor.append(int(i/f))#同时记录对称因子
        if sum(factor)==i:
            none=False
            print("%d = "%i,end="")
            print(" + ".join("%s"%x for x in sorted(factor)))#对称因子导致不按顺序
    if none:
        print("None")
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
注:本文转载自blog.csdn.net的邂逅模拟卷的文章"https://blog.csdn.net/qq_34451909/article/details/107982884"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top