完美立方数一道题9行代码如何写出?
看题目- - from sys import stdoutcount=int(input())+1
for a in range(1,count):
for b in range(1,count):
for c in range(1,count):
for d in range(1,count):
if a*a*a==b*b*b+c*c*c+d*d*d:
stdout.write(f'Cube = {a},Triple = ({b},{c},{d})\n') #include<stdio.h>
int main(){
int a,b,c,d,count;
scanf("%d",&count);
for(a=1;a<=count;a++){
for(b=a;b<=count;b++){
for(c=b;c<=count;c++){
for(d=c;d<=count;d++){
if(a*a*a==b*b*b+c*c*c+d*d*d){
printf('Cube = %d,Triple = (%d,%d,%d)\n',a,b,c,d);
}
}
}
}
}
return 0;
} n = int(input())
n += 1
iden = []
for a in range(2, n):
for b in range(2, n):
for c in range(2, n):
for d in range(2, n):
(print(f'Cube = {a},Triple=({b},{c},{d})'), iden.append(sorted((a, b, c, d)))) if (a ** 3 == b ** 3 + c ** 3 + d ** 3) and (sorted((a, b, c, d)) not in iden) else 0
或者精简一点:
n, iden = int(input()) + 1, []
for a in range(2, n):
for b in range(2, n):
for c in range(2, n):
for d in range(2, n):
(print(f'Cube = {a},Triple=({b},{c},{d})'), iden.append(sorted((a, b, c, d)))) if (a ** 3 == b ** 3 + c ** 3 + d ** 3) and (sorted((a, b, c, d)) not in iden) else 0 zltzlt 发表于 2020-4-30 18:24
或者精简一点:
大佬,你是神!!! from itertools import combinations as cb
def f(N):
L=[]
for i in cb(range(2,N+1),4):
if i**3+i**3+i**3==i**3:
L.append((i,(i,i,i)))
L.sort(key=lambda i:i)
for i in L:
print('Cube =',i,',', 'Triple =', (i,i,i)) 简化一下from itertools import combinations as cb
def f(N):
for i in sorted([(i,(i,i,i)) for i in cb(range(2,N+1),4) if i**3+i**3+i**3==i**3]):
print('Cube =',i,',', 'Triple =', (i,i,i))
f(24) 不用函数from itertools import combinations as cb
for i in sorted([(i,(i,i,i)) for i in cb(range(2,24+1),4) if i**3+i**3+i**3==i**3]):
print('Cube =',i,',', 'Triple =', (i,i,i)) 本帖最后由 李子豪 于 2020-4-30 21:39 编辑
x = int(input())
for a in range(2,x+1):
for b in range(2,a):
for c in range(b,a):
for d in range(c,a):
if pow(a,3) == pow(b,3)+pow(c,3)+pow(d,3):
print("Cube = {},Triple = ({},{},{})".format(a,b,c,d)) zltzlt 发表于 2020-4-30 18:24
或者精简一点:
……只是把赋值的部分精简了一下
好叭
页:
[1]