#title: Finds the sum of two elements in an array equal to a given integer#Ideas:#1, the array elements are sorted;#2, Array[i] and A[j] (the value of J: i+1 to Len_array-1) add;#3, such as 22 add the integer continuation, such as = integer is the output element value;#4, if you exit directly, i+1 start the next round to add a comparisondefaddData (Array, sumdata): Temp_array=Array Temp_sumdata=SumdataPrint "Sumdata:%d"%Temp_sumdata#If there are no negative numbers in the array, you can first delete the number of >sumdata#To avoid the problem of length change, you can assign a number of
#Temp_array = []#len_temp_array2 = Len (temp_array2)#For i in range (0,len_temp_array2):#if Temp_array2[i] < temp_sumdata:#temp_array.append (Temp_array2[i])Sorted (temp_array) Len_temp_array=Len (Temp_array)#count the number of groups that meet the criterianum =0 forIinchRange (0, len_temp_array-1): forJinchRange (i+1, Len_temp_array):ifTemp_array[i] + Temp_array[j] <Temp_sumdata:Continue elifTemp_array[i] + temp_array[j] = =Temp_sumdata:num+ = 1Print "Group%d:"%NumPrint "Subscript :%d, element value:%d"%(i, temp_array[i])Print "Subscript :%d, element value:%d"%(J, Temp_array[j])Else: Break if __name__=="__main__": Test_array= [ -1,1,2,3,4,5,6,7,8] Test_sumdata= 6AddData (Test_array, Test_sumdata)
Python Learning--two numbers in a tuple combined with and equal to a certain number