鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 新手·ing

[已解决]Python:每日一题 152(悬赏篇)

[复制链接]
发表于 2018-2-11 11:32:53 | 显示全部楼层
  1. def judge(L):
  2.     temp = 0
  3.     for i in range(1,len(L)-1):#忽略最后一个和第一个
  4.         if sum(L[:i]) == sum(L[i+1:]):
  5.             temp = 1
  6.             print(i)
  7.     if temp == 0:
  8.         print('None')
  9.         
  10. list_L= [[1, 3, 5, 7, 8, 25, 4, 20, 29],
  11. [6, 5, 7, 9, 0, 5, 43, 2, 17, 13, 16],
  12. [2, 31, 43, 234, 0, 10, 15, 310, 37, 43, 21],
  13. [1, 3, 2, 546, 123, 546, 98, 543, 22, 424, 56, 23, 199, 934, 4],
  14. [1, 2, 4, 34, 54, 27, 49, 14, 36, 25, 68, 13, 35, 78, 60, 25,
  15. 17, 5, 9, 23, 2, 1, 8, 1, 33, 26, 1, 9, 11, 5, 5, 253, 47, 65, 56, 10]]
  16. for L in list_L:
  17.     judge(L)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-11 12:45:39 | 显示全部楼层
def sum(list_name,list_lenth):
    sum_1=0
    sum_2=0
    for a in range(list_lenth):
        sum_1=sum_1+list_name[a]
        for b in range(a+1,list_lenth):
            sum_2=sum_2+list_name[b]
        if sum_1 == sum_2 :
            print('列表:{}\n 平衡点位序为:{} \n 平衡值为:{}  '.format(list_name,a,list_name[a]))
            break
        else:
            sum_2=0
    else:
        print('Nnone')
list1=[1, 3, 5, 7, 8, 25, 4, 20, 29]
list2=[6, 5, 7, 9, 0, 5, 43, 2, 17, 13, 16]
list3=[2, 31, 43, 234, 0, 10, 15, 310, 37, 43, 21]
list4=[1, 3, 2, 546, 123, 546, 98, 543, 22, 424, 56, 23, 199, 934, 4]
list5=[1, 2, 4, 34, 54, 27, 49, 14, 36, 25, 68, 13, 35, 78, 60, 25, 17, 5, 9, 23, 2, 1, 8, 1, 33, 26, 1, 9, 11, 5, 5, 253, 47, 65, 56, 10]
sum(list1,len(list1))
sum(list2,len(list2))
sum(list3,len(list3))
sum(list4,len(list4))
sum(list5,len(list5))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-11 13:41:07 | 显示全部楼层
  1. def seek_balance(lis):
  2.     n = 0
  3.     if len(lis) % 2 == 1:
  4.         while n < len(lis):
  5.             if sum(lis[:n]) != sum(lis[n+1:]):
  6.                 n += 1
  7.             else:
  8.                 break
  9.     else:
  10.         print('error')
  11.     return (n,lis[n])
  12. lis1 = eval(input('请输入一个数组:\n'))
  13. (index1,result1) = seek_balance(lis1)
  14. print('the index of the list: ', index1,'\n')
  15. print('the Balance Point of the list: ',result1)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-11 13:47:06 | 显示全部楼层
  1. a = eval(input("请输入列表:"))
  2. for i in range(1,len(a)-1):
  3.     if sum(a[:i]) == sum(a[i+1:]):print("平衡点为",i+1)
复制代码


我这算不算偷懒了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-11 19:21:12 | 显示全部楼层
from math import *
L = [[1, 3, 5, 7, 8, 25, 4, 20, 29],\
    [6, 5, 7, 9, 0, 5, 43, 2, 17, 13, 16],\
    [2, 31, 43, 234, 0, 10, 15, 310, 37, 43, 21],\
    [1, 3, 2, 546, 123, 546, 98, 543, 22, 424, 56, 23, 199, 934, 4],\
    [1, 2, 4, 34, 54, 27, 49, 14, 36, 25, 68, 13, 35, 78, 60, 25, 17, 5, 9, 23, 2, 1, 8, 1, 33, 26, 1, 9, 11, 5, 5, 253, 47, 65, 56, 10]]

for i in range(5):
    x = 0
    for j in range(1,len(L[i]) - 1):
        c1 = 0
        for k in range(j):
            c1 = c1 + L[i][k]
        if c1 == sum(L[i]) - c1 - L[i][j]:
            print(L[i][j])
            x = x + 1
    if x == 0:
        print(None)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-12 13:54:36 | 显示全部楼层
def Check_num(list1):
       
        sum1 = 0
        sum2 = 0
        for i in range(1,len(list1)-1):
               
                a = list1[0:i]
                b = list1[i+1:]
               
                for x in a:
                        sum1 += x
                       
                for y in b:
                        sum2 += y
               
                #其实这一段可以用这一句话代替if sum(list1[:i]) == sum(list1[i+1:]):
                if sum1 == sum2:
                        return list1[i]
               
                sum1 = 0
                sum2 = 0
                       
print(Check_num([1, 3, 5, 7, 8, 25, 4, 20, 29]))
print(Check_num([6, 5, 7, 9, 0, 5, 43, 2, 17, 13, 16]))
print(Check_num([2, 31, 43, 234, 0, 10, 15, 310, 37, 43, 21]))
print(Check_num([1, 3, 2, 546, 123, 546, 98, 543, 22, 424, 56, 23, 199, 934, 4]))
print(Check_num([1, 2, 4, 34, 54, 27, 49, 14, 36, 25, 68, 13, 35, 78, 60, 25, 17, 5, 9, 23, 2, 1, 8, 1, 33, 26, 1, 9, 11, 5, 5, 253, 47, 65, 56, 10]))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-14 10:43:31 | 显示全部楼层
  1. def find(l):
  2.     sum_of_list = sum(l)
  3.     first_part = 0
  4.     for i in range(len(l)-1):
  5.         first_part += l[i]
  6.         if first_part == (sum_of_list -l[i+1])/2:
  7.             return(i+1)
  8.     return "Not Find"
  9. l1 = [1, 3, 5, 7, 8, 25, 4, 20, 29]
  10. l2 = [6, 5, 7, 9, 0, 5, 43, 2, 17, 13, 16]
  11. l3 = [2, 31, 43, 234, 0, 10, 15, 310, 37, 43, 21]
  12. l4 = [1, 3, 2, 546, 123, 546, 98, 543, 22, 424, 56, 23, 199, 934, 4]
  13. l5 = [1, 2, 4, 34, 54, 27, 49, 14, 36, 25, 68, 13, 35, 78, 60, 25, 17, 5, 9, 23, 2, 1, 8, 1, 33, 26, 1, 9, 11, 5, 5, 253, 47, 65, 56, 10]
  14. for each in (l1, l2, l3, l4, l5):
  15.     try:
  16.         print(find(each),each[find(each)])
  17.     except TypeError:
  18.         print(find(each))
复制代码

  1. 6 4
  2. Not Find
  3. Not Find
  4. Not Find
  5. Not Find
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-23 22:28:47 | 显示全部楼层
def main():
    a = [1, 3, 5, 7, 8, 25, 4, 20, 29]
    for x in range(len(a)):
        if sum(a[:x]) == sum(a[x+1:]):
            print(a[x])
            break
    else:
        print('None')



main()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-3-14 11:30:07 | 显示全部楼层
一个列表可能有多个平衡点,比如:[1, 2, 3, -3, 3, 2, 1],加上这种情况。

  1. def slice(list1):
  2.     balance = []
  3.     for i in range(2, len(list1)-1):
  4.         if sum(list1[:i]) == sum(list1[i+1:]):
  5.             balance.append([i, list1[i]])
  6.     return balance if len(balance) else None


  7. l1 = [[1, 3, 5, 7, 8, 25, 4, 20, 29], [6, 5, 7, 9, 0, 5, 43, 2, 17, 13, 16], [2, 31, 43, 234, 0, 10, 15, 310, 37, 43, 21],
  8.       [1, 3, 2, 546, 123, 546, 98, 543, 22, 424, 56, 23, 199, 934, 4], [1, 2, 4, 34, 54, 27, 49, 14, 36, 25, 68, 13, 35,
  9.     78, 60, 25, 17, 5, 9, 23, 2, 1, 8, 1, 33, 26, 1, 9, 11, 5, 5, 253, 47, 65, 56, 10], [1, 2, 3, -3, 3, 2, 1]]

  10. for i in l1:
  11.     result = slice(i)
  12.     if result is None:
  13.         print("该列表无平衡点!")
  14.     else:
  15.         print("该列表有%d个平衡点,平衡点为:" % len(result))
  16.         for s in result:
  17.             print("  第%d个元素:%d。" % (s[0], s[1]))
复制代码


结果:
该列表有1个平衡点,平衡点为:
  第6个元素:4。
该列表无平衡点!
该列表无平衡点!
该列表无平衡点!
该列表无平衡点!
该列表有3个平衡点,平衡点为:
  第2个元素:3。
  第3个元素:-3。
  第4个元素:3。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-20 10:50:06 From FishC Mobile | 显示全部楼层
哈哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-17 15:51:25 | 显示全部楼层
list1 = [1, 3, 5, 7, 8, 25, 4, 20, 29]
count = len(list1)//2
print(sum(list1[:count]))
print(sum(list1[count+1 :]))

if sum(list1[:count]) == sum(list1[count+1 :]):
    print('平衡點位置是:' , count)
else:
    print('None')

計算結果都沒有平衡點,而且第5個數組是36個,無法計算
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-1-14 12:06:36 | 显示全部楼层
list1 = [1, 3, 5, 7, 8, 25, 4, 20, 29]
list2 = [6, 5, 7, 9, 0, 5, 43, 2, 17, 13, 16]
list3 = [2, 31, 43, 234, 0, 10, 15, 310, 37, 43, 21]
list4 = [1, 3, 2, 546, 123, 546, 98, 543, 22, 424, 56, 23, 199, 934, 4]
list5 = [1, 2, 4, 34, 54, 27, 49, 14, 36, 25, 68, 13, 35, 78, 60, 25, 17, 5, 9, 23, 2, 1, 8, 1, 33, 26, 1, 9, 11, 5, 5,
         253, 47, 65, 56, 10]

def getBalancePoint(list) :
        flag, flagCommon, flagPoint = 0, 0, 0
        for i in list :
                flagCommon += i
        for j in range(len(list) - 1) :
                flag += list[j]
                if flag == flagCommon - flag - list[j + 1] :
                        print('列表%s平衡点的下标为:%d\n平衡的的值为:%d\n' % (list, j + 1, list[j + 1]))
                        flagPoint += 1
        if flagPoint == 0 :
                print('该列表无平衡点')

getBalancePoint(list1)
getBalancePoint(list2)
getBalancePoint(list3)
getBalancePoint(list4)
getBalancePoint(list5)
[/code]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-4-18 10:28:26 | 显示全部楼层
  1. def fun152(list1):
  2.     if not len(list1)%2:
  3.         return False

  4.     res = []
  5.     for i in range(1,len(list1)):
  6.         if sum(list1[:i]) == sum(list1[i+1:]):
  7.             res.append(list1[i])
  8.     if res:
  9.         return res
  10.     else:
  11.         return None
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-4 15:13:30 | 显示全部楼层
  1. def fun102(list1):
  2.     for i in range(1,len(list1)):
  3.         if sum(list1[:i])==sum(list1[i+1:]):
  4.             print(list1[i])
  5.         else:
  6.             continue
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 10:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表