python调用golang 从指定序列中找出一组与目标值最接近的子序列

  • 编写go代码
  • 生成so库
  • python代码调用

编写go代码

写入hello.go文件

package main

import (
	"C"
	"encoding/json"
	"fmt"
	"time"
)

// go build --buildmode=c-shared -o find_target.so hello.go
type Result struct {
	diff_value int
	arrs       []int
}

func main() {
	a := time.Now()
	// fmt.Println(find_target_go_run())
	target_value := 18000000
	strRes := FindTarget(target_value)
	fmt.Println(strRes)
	fmt.Println(time.Now().Sub(a))
}

//export FindTarget
func FindTarget(target_value int) *C.char {
	arrs := ProcessFind(target_value)
	data, err := json.Marshal(arrs)
	if err != nil {
		fmt.Println(err)
	}
	//fmt.Printf("%v\n", data)
	return C.CString(string(data))
	// fmt.Println(s)
}

func ProcessFind(target_value int) []int {

	arrs := []int{420, 423, 424, 460, 491, 491, 633, 634, 634, 636, 859, 872, 928, 985, 1013, 1061, 1064, 1064, 1096, 1106, 1133,
		1135, 1144, 1176, 1208, 1218, 1228, 1228, 1246, 1247, 1265, 1270, 1278, 1352, 1378, 1542, 1561, 1570, 1677,
		1709, 1777, 2020, 2022, 2126, 2225, 2400, 2486, 2772, 2856, 2867, 2905, 2938, 2944, 3338, 3448, 3464, 3476,
		3483, 3817, 4112, 4482, 4482, 4599, 4681, 4686, 4707, 4888, 5185, 5419, 5428, 5447, 5507, 5976, 5977, 6264,
		6278, 6315, 6402, 6406, 6631, 6633, 6698, 7014, 7058, 7762, 7792, 7919, 8203, 8203, 8236, 8250, 8406, 8444,
		8451, 8460, 8564, 8565, 8575, 8577, 8580, 8598, 8614, 8630, 8669, 8669, 8686, 8704, 9027, 9062, 9065, 9069,
		9080, 9082, 9088, 9089, 9095, 9102, 9108, 9133, 9144, 9152, 9154, 9154, 9155, 9160, 9161, 9171, 9173, 9195,
		9198, 9229, 9229, 9231, 9247, 9264, 9269, 9298, 9553, 9874, 10018, 10073, 10377, 10485, 10708, 10740, 11166,
		11375, 11824, 12353, 12360, 12828, 13059, 13066, 13081, 13136, 13227, 13234, 13244, 13282, 13283, 13527, 13565,
		13569, 13576, 13581, 13633, 13640, 13650, 13667, 13681, 13708, 14763, 14998, 15066, 15703, 15748, 15778, 15836,
		15906, 16077, 16158, 16278, 16311, 16382, 16453, 16700, 16839, 17046, 17562, 17620, 17856, 17859, 17893, 17908,
		17912, 17914, 17914, 17920, 17929, 17931, 17933, 17953, 17973, 17977, 17984, 18000, 18003, 18044, 18063, 18067,
		18104, 18107, 18109, 18112, 18118, 18150, 18216, 18226, 18249, 18258, 18260, 18268, 18274, 18277, 18308, 18459,
		18581, 18741, 18774, 18789, 18804, 18850, 18851, 18851, 18851, 18854, 18854, 18854, 18854, 18854, 18854, 18957,
		18961, 18969, 19212, 19478, 19525, 19537, 19539, 19550, 19552, 19557, 19611, 19688, 19792, 19911, 20256, 20361,
		20514, 20532, 20693, 20695, 20742, 21041, 21164, 21813, 21865, 21988, 22065, 22087, 22131, 22172, 22186, 22199,
		22222, 22271, 22430, 22566, 22615, 22618, 22977, 23069, 23069, 23316, 23661, 23715, 23734, 23789, 23910, 24012,
		24022, 24084, 24427, 24547, 24789, 24819, 25060, 25589, 25871, 25894, 25915, 25915, 26051, 26101, 26105, 26112,
		26115, 26117, 26117, 26117, 26119, 26134, 26175, 26215, 26238, 26367, 26490, 26631, 26734, 26845, 26863, 27059,
		27061, 27178, 27191, 27205, 27211, 27265, 27396, 27405, 27411, 27489, 27493, 27504, 27513, 27537, 27571, 27704,
		27727, 27767, 27884, 27895, 27898, 27905, 27980, 28013, 28020, 28092, 28101, 28117, 28166, 28241, 28244, 28246,
		28293, 28389, 28408, 28458, 28473, 28667, 28676, 28800, 28800, 28812, 28850, 28968, 28969, 29145, 29180, 29248,
		29271, 29703, 29721, 29736, 29869, 30033, 30103, 30150, 30427, 30555, 30895, 30990, 31095, 31259, 31330, 31514,
		31516, 31529, 31529, 31552, 31575, 31576, 31587, 31601, 31606, 31619, 31630, 31650, 31651, 31656, 31660, 31673,
		31681, 31702, 31744, 31748, 31752, 31770, 31780, 31801, 31808, 31809, 31873, 31877, 32042, 32184, 32296, 32344,
		32370, 32385, 32453, 32527, 32534, 32552, 32562, 32577, 32588, 32588, 32611, 32614, 32619, 32628, 32629, 32633,
		32645, 32650, 32651, 32651, 32661, 32661, 32673, 32675, 32675, 32681, 32724, 32740, 32745, 32756, 32777, 32781,
		32813, 32877, 32914, 32994, 33105, 33186, 33300, 33357, 33408, 33646, 33649, 33760, 33987, 34008, 34117, 34354,
		34386, 34397, 34479, 34518, 34663, 34696, 34766, 34905, 35432, 35564, 35566, 35625, 35810, 35857, 36176, 36240,
		36268, 36408, 36446, 36655, 36815, 36858, 37004, 37087, 37143, 37177, 37179, 37274, 37339, 37431, 37477, 37708,
		37771, 38152, 38316, 38771, 38805, 38926, 39029, 39072, 39110, 39140, 39390, 39419, 39477, 39573, 40067, 40391,
		40487, 40639, 41011, 41135, 41239, 41245, 41317, 41329, 41630, 41957, 41985, 41987, 42020, 42048, 42216, 42279,
		42466, 42473, 42642, 42679, 42705, 42781, 43241, 43492, 43635, 43701, 43719, 43892, 43911, 43961, 44096, 44171,
		44172, 44199, 44240, 44240, 44242, 44446, 44585, 44633, 44879, 44929, 45150, 45482, 45809, 46303, 46993, 47555,
		48150, 48231, 48336, 48424, 48576, 48581, 48596, 48602, 48606, 48645, 48809, 48866, 48930, 48981, 48994, 49261,
		49691, 49740, 49771, 49842, 49846, 50057, 50187, 50262, 50338, 50436, 50477, 50956, 51053, 51317, 51410, 51753,
		51783, 51949, 52101, 52109, 52133, 52138, 52239, 52294, 52440, 52451, 52499, 52788, 52900, 52987, 52999, 53009,
		53023, 53386, 53677, 53830, 54057, 54201, 54262, 54285, 54817, 55063, 55337, 55703, 55922, 56099, 56159, 56163,
		56344, 56477, 56486, 56776, 56837, 56925, 57031, 57107, 57309, 57412, 57541, 57670, 57855, 58036, 58058, 58279,
		58320, 58359, 58419, 58506, 58668, 58825, 59202, 59295, 59541, 59557, 59974, 60149, 60285, 60330, 60368, 60510,
		60530, 60610, 60613, 60642, 60871, 60955, 60959, 61392, 61755, 61779, 61837, 61943, 62034, 62039, 62089, 62190,
		62231, 62257, 62496, 62800, 62822, 62859, 62860, 63025, 63209, 63231, 63263, 64322, 64560, 64975, 65020, 65181,
		65593, 65956, 66190, 66332, 66403, 66545, 66731, 66823, 66987, 67064, 67299, 67510, 67804, 67896, 68255, 68318,
		68419, 68429, 68467, 68484, 68674, 68740, 68837, 69016, 69044, 69203, 69350, 69542, 70286, 70321, 70579, 70704,
		70898, 70950, 70988, 71003, 71112, 71114, 71122, 71266, 71294, 71444, 71477, 71630, 71837, 71853, 71993, 72249,
		72637, 72691, 72715, 72792, 72894, 72987, 73062, 73348, 73904, 74014, 74347, 74868, 74903, 74938, 75031, 75078,
		75428, 76079, 76317, 76425, 76503, 76510, 76651, 76696, 76710, 76750, 76892, 76903, 76955, 76977, 76987, 76987,
		76987, 76995, 77057, 77181, 77189, 77336, 77404, 77531, 77594, 77684, 77689, 77838, 77840, 77875, 77876, 77918,
		77925, 77933, 78101, 78102, 78142, 78164, 78188, 78253, 78297, 78305, 78328, 78365, 78406, 78779, 78801, 78812,
		78861, 78899, 78911, 78976, 78982, 78982, 79031, 79071, 79121, 79162, 79214, 79349, 79499, 79514, 79515, 79517,
		79542, 79550, 79552, 79573, 79579, 79582, 79606, 79608, 79621, 79645, 79646, 79649, 79659, 79670, 79674, 79684,
		79686, 79694, 79752, 79755, 79764, 79769, 79780, 79781, 79782, 79796, 79802, 79803, 79809, 79810, 79836, 79851,
		79881, 79882, 79895, 79915, 79931, 79937, 79939, 79953, 79960, 79970, 79981, 80008, 80013, 80031, 80032, 80034,
		80039, 80068, 80071, 80118, 80122, 80194, 80215, 80254, 80293, 80310, 80321, 80368, 80376, 80389, 80421, 80449,
		80463, 80477, 80490, 80548, 80584, 80645, 80696, 80720, 80735, 80770, 80915, 80919, 80961, 80974, 81000, 81021,
		81024, 81067, 81069, 81089, 81104, 81199, 81215, 81344, 81352, 81391, 81508, 81540, 81547, 81586, 81952, 81967,
		82312, 82347, 82533, 82786, 82872, 83088, 83122, 83180, 83447, 83532, 83555, 83596, 83921, 83934, 83940, 84133,
		84189, 84257, 84715, 84755, 84846, 84866, 85110, 85129, 85135, 85169, 85223, 85640, 85664, 85935, 85944, 85991,
		86098, 86313, 86411, 86485, 86614, 86668, 87101, 87372, 87700, 87786, 87994, 88048, 88281, 88316, 88336, 88373,
		88558, 88698, 88709, 88721, 88803, 88806, 89010, 89062, 89147, 89163, 89324, 89454, 89544, 89628, 90050, 90238,
		90688, 90776, 90979, 91054, 91134, 91409, 91482, 91597, 91620, 91626, 91667, 91940, 92028, 92133, 92352, 92570,
		92708, 92744, 92802, 92807, 92816, 92858, 93318, 93364, 94273, 94417, 94515, 94584, 94950, 95122, 95234, 95643,
		95860, 95937, 96005, 96168, 96425, 97040, 97181, 97820, 98052, 98404, 98468, 98475, 98511, 98553, 98656, 98848,
		98888, 98996, 98997, 99021, 99190, 99191, 99345, 99384, 99673, 99848, 99987, 100001, 100534, 100741, 100808,
		100943, 101215, 101266, 101325, 101568, 101571, 101596, 101606, 101695, 101716, 101835, 101909, 101919, 102076,
		102116, 102149, 102187, 102511, 102550, 102790, 102969, 103056, 103190, 103455, 103480, 103564, 103610, 103664,
		103824, 103988, 104013, 104045, 104187, 104336, 104391, 104460, 104492, 104533, 104539, 104545, 104657, 104883,
		104997, 105043, 105459, 105608, 105612, 105905, 105935, 105981, 106191, 106205, 106253, 106352, 106450, 106504,
		106529, 106577, 106579, 107017, 107084, 107131, 107230, 107286, 107328, 107389, 108056, 108188, 108221, 108623,
		108641, 109012, 109023, 109042, 109049, 109287, 109403, 109500, 109541, 109603, 110011, 110481, 110668, 110690,
		110821, 110906, 111109, 111258, 111408, 111430, 111582, 111584, 111746, 111967, 111977, 112349, 112352, 112382,
		112399, 112430, 112444, 112613, 112709, 112815, 112852, 112876, 113005, 113153, 113191, 113267, 113317, 113422,
		113488, 113597, 113815, 113866, 113959, 113988, 114076, 114190, 114194, 114434, 114536, 114673, 114755, 114914,
		114957, 115004, 115009, 115044, 115090, 115105, 115148, 115288, 115402, 115449, 115518, 115571, 115822, 115847,
		115987, 116001, 116031, 116035, 116049, 116099, 116136, 116195, 116198, 116201, 116217, 116362, 116371, 116424,
		116455, 116468, 116486, 116736, 116778, 116779, 116853, 116915, 116917, 116936, 116976, 116999, 117012, 117054,
		117082, 117269, 117308, 117323, 117352, 117368, 117763, 117922, 117978, 118185, 118271, 118299, 118522, 118663,
		118710, 118739, 118782, 118846, 118866, 118920, 118930, 118978, 119030, 119033, 119177, 119236, 119361, 119463,
		119653, 119683, 120278, 120426, 120745, 120836, 120904, 120936, 120944, 121132, 121260, 121383, 121640, 121888,
		122051, 122065, 122112, 122269, 122313, 122503, 122507, 122841, 122852, 122890, 123129, 123148, 123264, 123275,
		123316, 123389, 123613, 123749, 124271, 125081, 125416, 125461, 125582, 125634, 125726, 125780, 125961, 126079,
		126102, 126244, 126295, 126312, 126408, 126519, 126924, 127023, 127065, 127234, 127274, 127281, 127440, 127474,
		127651, 127751, 127920, 127974, 128434, 129076, 129159, 129189, 129353, 129373, 129453, 129519, 129673, 129680,
		129764, 129803, 130092, 130170, 130194, 130266, 130374, 130487, 130600, 130656, 130829, 130983, 131069, 131432,
		131642, 131662, 131707, 131743, 131757, 131886, 131920, 132003, 132078, 132091, 132106, 132320, 132323, 132680,
		132694, 133124, 133202, 133271, 133361, 133377, 133387, 133403, 133468, 133733, 134080, 134211, 134312, 134357,
		134443, 134643, 134771, 134812, 134984, 135439, 135869, 136055, 136475, 136592, 136648, 136751, 136871, 136886,
		136900, 137040, 137463, 137689, 137700, 138027, 138484, 138704, 138862, 138990, 139056, 139171, 139333, 139526,
		139866, 139872, 140141, 140234, 140237, 140243, 140281, 140622, 140650, 140824, 141158, 141178, 141392, 141551,
		141974, 142082, 142130, 142423, 142610, 142697, 142984, 143136, 143335, 143806, 143843, 144076, 144164, 144707,
		145375, 145379, 145421, 145424, 145536, 145613, 145675, 145806, 145817, 145892, 146335, 146577, 146695, 146799,
		146854, 147094, 147206, 147571, 147644, 147702, 147708, 147750, 147770, 147896, 148044, 148110, 148124, 148150,
		148195, 148205, 148472, 148489, 148645, 148711, 148774, 148778, 148912, 148923, 148992, 149010, 149018, 149114,
		149195, 149244, 149282, 149591, 149788, 149797, 149974, 150031, 150068, 150145, 150243, 150264, 150275, 150277,
		150354, 150477, 150484, 150723, 150881, 150917, 150936, 150981, 151068, 151073, 151093, 151157, 151178, 151602,
		151609, 151626, 151635, 151688, 151760, 151783, 151838, 151881, 151889, 152029, 152088, 152108, 152113, 152125,
		152191, 152282, 152290, 152312, 152799, 153145, 153157, 153236, 153299, 153345, 153582, 153586, 153633, 153658,
		153904, 154072, 154166, 154189, 154563, 154606, 154968, 154998, 155168, 155230, 155247, 155299, 155398, 155551,
		155586, 155588, 155772, 155807, 155818, 155893, 156031, 156206, 156346, 156515, 156600, 156602, 156706, 156760,
		156817, 156850, 156851, 157150, 157208, 157421, 157607, 157954, 157976, 157983, 158009, 158010, 158035, 158227,
		158269, 158368, 158384, 158496, 158681, 158874, 159083, 159101, 159157, 159195, 159740, 159863, 160197, 160335,
		160425, 160498, 161090, 161383, 161398, 161467, 161501, 162051, 162387, 162401, 162581, 162870, 162908, 162916,
		163005, 163114, 163393, 163520, 163582, 163682, 163829, 163837, 163849, 163865, 164083, 164122, 164136, 164142,
		164230, 164238, 164292, 164312, 164451, 164543, 164551, 164635, 164644, 164647, 164938, 165028, 165140, 165248,
		165407, 165452, 165641, 165720, 165854, 165974, 166009, 166228, 166257, 166314, 166368, 166470, 166633, 166640,
		166667, 166672, 166782, 166927, 166998, 167003, 167127, 167547, 167684, 167909, 168090, 168491, 168713, 169337,
		169416, 169447, 169451, 169555, 169605, 170049, 170125, 170236, 170270, 171182, 171219, 171307, 171458, 171695,
		171773, 172040, 172398, 172489, 172785, 172870, 172965, 172980, 173001, 173425, 173479, 173541, 173719, 173795,
		173869, 174188, 174708, 174758, 174868, 174920, 175191, 175654, 176017, 176087, 176098, 176169, 176472, 176594,
		176632, 176635, 176686, 176692, 176711, 176819, 176897, 176900, 176975, 177080, 177189, 177284, 177288, 177376,
		177397, 177453, 177464, 177602, 177649, 177684, 178034, 178051, 178052, 178363, 178420, 179260, 179604, 180046,
		180064, 180279, 180389, 180981, 181764, 182177, 182300, 182366, 182628, 182869, 182966, 183085, 183100, 183405,
		183485, 183913, 184477, 184672, 184799, 184850, 184867, 185255, 185394, 185809, 185872, 186620, 186801, 186985,
		188368, 189227, 189572, 190491, 191866, 192388, 193037, 193550, 193773, 194160, 194186, 194289, 194296, 194299,
		194624, 195106, 195500, 195531, 196291, 196464, 196563, 196606, 196607, 196616, 196696, 196701, 196818, 197241,
		197247, 197314, 197390, 197479, 197486, 197578, 197581, 197649, 197792, 197962, 198058, 198117, 198376, 198546,
		198707, 198768, 198843, 199045, 199152, 199496, 199654, 200110, 200283, 201403, 201731, 201752, 201814, 202008,
		202016, 202362, 202381, 202472, 202607, 202807, 202996, 203250, 203279, 203472, 203647, 203823, 203978, 204727,
		204943, 204962, 205476, 205570, 205719, 206299, 206433, 206436, 206585, 206608, 206882, 207007, 207081, 207345,
		208542, 209338, 210558, 210652, 211098, 211587, 212055, 212205, 212323, 212720, 212842, 213312, 213420, 214159,
		214163, 214334, 214534, 214553, 214572, 215153, 215283, 216156, 216191, 216341, 216399, 217003, 217047, 217254,
		217407, 217567, 218071, 218192, 218214, 218260, 218456, 218680, 218891, 218962, 218974, 219023, 219065, 219424,
		219434, 219520, 219601, 219856, 219996, 220044, 220181, 220523, 220546, 220764, 220868, 221137, 221217, 221346,
		221376, 221392, 221536, 221835, 221987, 222011, 222157, 222190, 222241, 222754, 223173, 223285, 223318, 223723,
		224163, 224322, 224515, 224516, 224694, 224876, 224883, 225244, 225249, 225426, 225528, 225577, 225628, 225876,
		226465, 226569, 226631, 227164, 227365, 227712, 228026, 228058, 228078, 228310, 228491, 228669, 228711, 228809,
		229307, 229659, 229864, 230486, 230614, 231082, 231221, 231354, 231519, 231950, 232157, 233827, 233929, 234255,
		234704, 234867, 235131, 235160, 235225, 235305, 235441, 236069, 236161, 236170, 236293, 236306, 236347, 236351,
		236472, 236572, 237156, 237219, 237373, 239227, 239856, 241770, 242454, 244000, 244107, 246068, 247390, 256324,
		265148, 266636, 273227, 291459, 293413, 301047, 301424, 302001, 309915, 320948}

	arr_total_value := 0
	for _, value := range arrs {
		arr_total_value += value
	}
	if arr_total_value < target_value {
		return arrs
	}
	diff_value := arr_total_value

	var res_arrs []int
	for step := 0; step < len(arrs); step++ {
		start_idx := 0
		end_idx := start_idx + step

		is_find := false
		for end_idx <= len(arrs) {
			var find_arrs []int
			find_arrs = append(find_arrs, arrs[:start_idx]...)
			find_arrs = append(find_arrs, arrs[end_idx:]...)

			total_value := 0
			_temp_total_value := 0
			// fmt.Println(find_arrs)
			for idx, value := range find_arrs {
				_temp_total_value = total_value + value

				if _temp_total_value > target_value {
					is_find = true
					prev_abs_diff_value := target_value - total_value
					now_abs_diff_value := _temp_total_value - target_value

					temp_diff_value := 0
					var temp_arrs []int
					if prev_abs_diff_value < now_abs_diff_value {
						temp_diff_value = prev_abs_diff_value
						temp_arrs = find_arrs[:idx]
					} else {
						temp_diff_value = now_abs_diff_value
						temp_arrs = find_arrs[:idx+1]
					}

					if temp_diff_value < diff_value {
						diff_value = temp_diff_value
						res_arrs = temp_arrs
					}

					break

				} else if _temp_total_value == target_value {
					// fmt.Println(find_arrs[:idx+1])
					return find_arrs[:idx+1]
				}

				total_value += value
			}

			if step == 0 {

				break
			}
			start_idx += 1
			end_idx = start_idx + step
		}

		if !is_find {
			break
		}

	}

	return res_arrs
}

func find_target_go_run() []int {

	arrs := []int{420, 423, 424, 460, 491, 491, 633, 634, 634, 636, 859, 872, 928, 985, 1013, 1061, 1064, 1064, 1096, 1106, 1133,
		1135, 1144, 1176, 1208, 1218, 1228, 1228, 1246, 1247, 1265, 1270, 1278, 1352, 1378, 1542, 1561, 1570, 1677,
		1709, 1777, 2020, 2022, 2126, 2225, 2400, 2486, 2772, 2856, 2867, 2905, 2938, 2944, 3338, 3448, 3464, 3476,
		3483, 3817, 4112, 4482, 4482, 4599, 4681, 4686, 4707, 4888, 5185, 5419, 5428, 5447, 5507, 5976, 5977, 6264,
		6278, 6315, 6402, 6406, 6631, 6633, 6698, 7014, 7058, 7762, 7792, 7919, 8203, 8203, 8236, 8250, 8406, 8444,
		8451, 8460, 8564, 8565, 8575, 8577, 8580, 8598, 8614, 8630, 8669, 8669, 8686, 8704, 9027, 9062, 9065, 9069,
		9080, 9082, 9088, 9089, 9095, 9102, 9108, 9133, 9144, 9152, 9154, 9154, 9155, 9160, 9161, 9171, 9173, 9195,
		9198, 9229, 9229, 9231, 9247, 9264, 9269, 9298, 9553, 9874, 10018, 10073, 10377, 10485, 10708, 10740, 11166,
		11375, 11824, 12353, 12360, 12828, 13059, 13066, 13081, 13136, 13227, 13234, 13244, 13282, 13283, 13527, 13565,
		13569, 13576, 13581, 13633, 13640, 13650, 13667, 13681, 13708, 14763, 14998, 15066, 15703, 15748, 15778, 15836,
		15906, 16077, 16158, 16278, 16311, 16382, 16453, 16700, 16839, 17046, 17562, 17620, 17856, 17859, 17893, 17908,
		17912, 17914, 17914, 17920, 17929, 17931, 17933, 17953, 17973, 17977, 17984, 18000, 18003, 18044, 18063, 18067,
		18104, 18107, 18109, 18112, 18118, 18150, 18216, 18226, 18249, 18258, 18260, 18268, 18274, 18277, 18308, 18459,
		18581, 18741, 18774, 18789, 18804, 18850, 18851, 18851, 18851, 18854, 18854, 18854, 18854, 18854, 18854, 18957,
		18961, 18969, 19212, 19478, 19525, 19537, 19539, 19550, 19552, 19557, 19611, 19688, 19792, 19911, 20256, 20361,
		20514, 20532, 20693, 20695, 20742, 21041, 21164, 21813, 21865, 21988, 22065, 22087, 22131, 22172, 22186, 22199,
		22222, 22271, 22430, 22566, 22615, 22618, 22977, 23069, 23069, 23316, 23661, 23715, 23734, 23789, 23910, 24012,
		24022, 24084, 24427, 24547, 24789, 24819, 25060, 25589, 25871, 25894, 25915, 25915, 26051, 26101, 26105, 26112,
		26115, 26117, 26117, 26117, 26119, 26134, 26175, 26215, 26238, 26367, 26490, 26631, 26734, 26845, 26863, 27059,
		27061, 27178, 27191, 27205, 27211, 27265, 27396, 27405, 27411, 27489, 27493, 27504, 27513, 27537, 27571, 27704,
		27727, 27767, 27884, 27895, 27898, 27905, 27980, 28013, 28020, 28092, 28101, 28117, 28166, 28241, 28244, 28246,
		28293, 28389, 28408, 28458, 28473, 28667, 28676, 28800, 28800, 28812, 28850, 28968, 28969, 29145, 29180, 29248,
		29271, 29703, 29721, 29736, 29869, 30033, 30103, 30150, 30427, 30555, 30895, 30990, 31095, 31259, 31330, 31514,
		31516, 31529, 31529, 31552, 31575, 31576, 31587, 31601, 31606, 31619, 31630, 31650, 31651, 31656, 31660, 31673,
		31681, 31702, 31744, 31748, 31752, 31770, 31780, 31801, 31808, 31809, 31873, 31877, 32042, 32184, 32296, 32344,
		32370, 32385, 32453, 32527, 32534, 32552, 32562, 32577, 32588, 32588, 32611, 32614, 32619, 32628, 32629, 32633,
		32645, 32650, 32651, 32651, 32661, 32661, 32673, 32675, 32675, 32681, 32724, 32740, 32745, 32756, 32777, 32781,
		32813, 32877, 32914, 32994, 33105, 33186, 33300, 33357, 33408, 33646, 33649, 33760, 33987, 34008, 34117, 34354,
		34386, 34397, 34479, 34518, 34663, 34696, 34766, 34905, 35432, 35564, 35566, 35625, 35810, 35857, 36176, 36240,
		36268, 36408, 36446, 36655, 36815, 36858, 37004, 37087, 37143, 37177, 37179, 37274, 37339, 37431, 37477, 37708,
		37771, 38152, 38316, 38771, 38805, 38926, 39029, 39072, 39110, 39140, 39390, 39419, 39477, 39573, 40067, 40391,
		40487, 40639, 41011, 41135, 41239, 41245, 41317, 41329, 41630, 41957, 41985, 41987, 42020, 42048, 42216, 42279,
		42466, 42473, 42642, 42679, 42705, 42781, 43241, 43492, 43635, 43701, 43719, 43892, 43911, 43961, 44096, 44171,
		44172, 44199, 44240, 44240, 44242, 44446, 44585, 44633, 44879, 44929, 45150, 45482, 45809, 46303, 46993, 47555,
		48150, 48231, 48336, 48424, 48576, 48581, 48596, 48602, 48606, 48645, 48809, 48866, 48930, 48981, 48994, 49261,
		49691, 49740, 49771, 49842, 49846, 50057, 50187, 50262, 50338, 50436, 50477, 50956, 51053, 51317, 51410, 51753,
		51783, 51949, 52101, 52109, 52133, 52138, 52239, 52294, 52440, 52451, 52499, 52788, 52900, 52987, 52999, 53009,
		53023, 53386, 53677, 53830, 54057, 54201, 54262, 54285, 54817, 55063, 55337, 55703, 55922, 56099, 56159, 56163,
		56344, 56477, 56486, 56776, 56837, 56925, 57031, 57107, 57309, 57412, 57541, 57670, 57855, 58036, 58058, 58279,
		58320, 58359, 58419, 58506, 58668, 58825, 59202, 59295, 59541, 59557, 59974, 60149, 60285, 60330, 60368, 60510,
		60530, 60610, 60613, 60642, 60871, 60955, 60959, 61392, 61755, 61779, 61837, 61943, 62034, 62039, 62089, 62190,
		62231, 62257, 62496, 62800, 62822, 62859, 62860, 63025, 63209, 63231, 63263, 64322, 64560, 64975, 65020, 65181,
		65593, 65956, 66190, 66332, 66403, 66545, 66731, 66823, 66987, 67064, 67299, 67510, 67804, 67896, 68255, 68318,
		68419, 68429, 68467, 68484, 68674, 68740, 68837, 69016, 69044, 69203, 69350, 69542, 70286, 70321, 70579, 70704,
		70898, 70950, 70988, 71003, 71112, 71114, 71122, 71266, 71294, 71444, 71477, 71630, 71837, 71853, 71993, 72249,
		72637, 72691, 72715, 72792, 72894, 72987, 73062, 73348, 73904, 74014, 74347, 74868, 74903, 74938, 75031, 75078,
		75428, 76079, 76317, 76425, 76503, 76510, 76651, 76696, 76710, 76750, 76892, 76903, 76955, 76977, 76987, 76987,
		76987, 76995, 77057, 77181, 77189, 77336, 77404, 77531, 77594, 77684, 77689, 77838, 77840, 77875, 77876, 77918,
		77925, 77933, 78101, 78102, 78142, 78164, 78188, 78253, 78297, 78305, 78328, 78365, 78406, 78779, 78801, 78812,
		78861, 78899, 78911, 78976, 78982, 78982, 79031, 79071, 79121, 79162, 79214, 79349, 79499, 79514, 79515, 79517,
		79542, 79550, 79552, 79573, 79579, 79582, 79606, 79608, 79621, 79645, 79646, 79649, 79659, 79670, 79674, 79684,
		79686, 79694, 79752, 79755, 79764, 79769, 79780, 79781, 79782, 79796, 79802, 79803, 79809, 79810, 79836, 79851,
		79881, 79882, 79895, 79915, 79931, 79937, 79939, 79953, 79960, 79970, 79981, 80008, 80013, 80031, 80032, 80034,
		80039, 80068, 80071, 80118, 80122, 80194, 80215, 80254, 80293, 80310, 80321, 80368, 80376, 80389, 80421, 80449,
		80463, 80477, 80490, 80548, 80584, 80645, 80696, 80720, 80735, 80770, 80915, 80919, 80961, 80974, 81000, 81021,
		81024, 81067, 81069, 81089, 81104, 81199, 81215, 81344, 81352, 81391, 81508, 81540, 81547, 81586, 81952, 81967,
		82312, 82347, 82533, 82786, 82872, 83088, 83122, 83180, 83447, 83532, 83555, 83596, 83921, 83934, 83940, 84133,
		84189, 84257, 84715, 84755, 84846, 84866, 85110, 85129, 85135, 85169, 85223, 85640, 85664, 85935, 85944, 85991,
		86098, 86313, 86411, 86485, 86614, 86668, 87101, 87372, 87700, 87786, 87994, 88048, 88281, 88316, 88336, 88373,
		88558, 88698, 88709, 88721, 88803, 88806, 89010, 89062, 89147, 89163, 89324, 89454, 89544, 89628, 90050, 90238,
		90688, 90776, 90979, 91054, 91134, 91409, 91482, 91597, 91620, 91626, 91667, 91940, 92028, 92133, 92352, 92570,
		92708, 92744, 92802, 92807, 92816, 92858, 93318, 93364, 94273, 94417, 94515, 94584, 94950, 95122, 95234, 95643,
		95860, 95937, 96005, 96168, 96425, 97040, 97181, 97820, 98052, 98404, 98468, 98475, 98511, 98553, 98656, 98848,
		98888, 98996, 98997, 99021, 99190, 99191, 99345, 99384, 99673, 99848, 99987, 100001, 100534, 100741, 100808,
		100943, 101215, 101266, 101325, 101568, 101571, 101596, 101606, 101695, 101716, 101835, 101909, 101919, 102076,
		102116, 102149, 102187, 102511, 102550, 102790, 102969, 103056, 103190, 103455, 103480, 103564, 103610, 103664,
		103824, 103988, 104013, 104045, 104187, 104336, 104391, 104460, 104492, 104533, 104539, 104545, 104657, 104883,
		104997, 105043, 105459, 105608, 105612, 105905, 105935, 105981, 106191, 106205, 106253, 106352, 106450, 106504,
		106529, 106577, 106579, 107017, 107084, 107131, 107230, 107286, 107328, 107389, 108056, 108188, 108221, 108623,
		108641, 109012, 109023, 109042, 109049, 109287, 109403, 109500, 109541, 109603, 110011, 110481, 110668, 110690,
		110821, 110906, 111109, 111258, 111408, 111430, 111582, 111584, 111746, 111967, 111977, 112349, 112352, 112382,
		112399, 112430, 112444, 112613, 112709, 112815, 112852, 112876, 113005, 113153, 113191, 113267, 113317, 113422,
		113488, 113597, 113815, 113866, 113959, 113988, 114076, 114190, 114194, 114434, 114536, 114673, 114755, 114914,
		114957, 115004, 115009, 115044, 115090, 115105, 115148, 115288, 115402, 115449, 115518, 115571, 115822, 115847,
		115987, 116001, 116031, 116035, 116049, 116099, 116136, 116195, 116198, 116201, 116217, 116362, 116371, 116424,
		116455, 116468, 116486, 116736, 116778, 116779, 116853, 116915, 116917, 116936, 116976, 116999, 117012, 117054,
		117082, 117269, 117308, 117323, 117352, 117368, 117763, 117922, 117978, 118185, 118271, 118299, 118522, 118663,
		118710, 118739, 118782, 118846, 118866, 118920, 118930, 118978, 119030, 119033, 119177, 119236, 119361, 119463,
		119653, 119683, 120278, 120426, 120745, 120836, 120904, 120936, 120944, 121132, 121260, 121383, 121640, 121888,
		122051, 122065, 122112, 122269, 122313, 122503, 122507, 122841, 122852, 122890, 123129, 123148, 123264, 123275,
		123316, 123389, 123613, 123749, 124271, 125081, 125416, 125461, 125582, 125634, 125726, 125780, 125961, 126079,
		126102, 126244, 126295, 126312, 126408, 126519, 126924, 127023, 127065, 127234, 127274, 127281, 127440, 127474,
		127651, 127751, 127920, 127974, 128434, 129076, 129159, 129189, 129353, 129373, 129453, 129519, 129673, 129680,
		129764, 129803, 130092, 130170, 130194, 130266, 130374, 130487, 130600, 130656, 130829, 130983, 131069, 131432,
		131642, 131662, 131707, 131743, 131757, 131886, 131920, 132003, 132078, 132091, 132106, 132320, 132323, 132680,
		132694, 133124, 133202, 133271, 133361, 133377, 133387, 133403, 133468, 133733, 134080, 134211, 134312, 134357,
		134443, 134643, 134771, 134812, 134984, 135439, 135869, 136055, 136475, 136592, 136648, 136751, 136871, 136886,
		136900, 137040, 137463, 137689, 137700, 138027, 138484, 138704, 138862, 138990, 139056, 139171, 139333, 139526,
		139866, 139872, 140141, 140234, 140237, 140243, 140281, 140622, 140650, 140824, 141158, 141178, 141392, 141551,
		141974, 142082, 142130, 142423, 142610, 142697, 142984, 143136, 143335, 143806, 143843, 144076, 144164, 144707,
		145375, 145379, 145421, 145424, 145536, 145613, 145675, 145806, 145817, 145892, 146335, 146577, 146695, 146799,
		146854, 147094, 147206, 147571, 147644, 147702, 147708, 147750, 147770, 147896, 148044, 148110, 148124, 148150,
		148195, 148205, 148472, 148489, 148645, 148711, 148774, 148778, 148912, 148923, 148992, 149010, 149018, 149114,
		149195, 149244, 149282, 149591, 149788, 149797, 149974, 150031, 150068, 150145, 150243, 150264, 150275, 150277,
		150354, 150477, 150484, 150723, 150881, 150917, 150936, 150981, 151068, 151073, 151093, 151157, 151178, 151602,
		151609, 151626, 151635, 151688, 151760, 151783, 151838, 151881, 151889, 152029, 152088, 152108, 152113, 152125,
		152191, 152282, 152290, 152312, 152799, 153145, 153157, 153236, 153299, 153345, 153582, 153586, 153633, 153658,
		153904, 154072, 154166, 154189, 154563, 154606, 154968, 154998, 155168, 155230, 155247, 155299, 155398, 155551,
		155586, 155588, 155772, 155807, 155818, 155893, 156031, 156206, 156346, 156515, 156600, 156602, 156706, 156760,
		156817, 156850, 156851, 157150, 157208, 157421, 157607, 157954, 157976, 157983, 158009, 158010, 158035, 158227,
		158269, 158368, 158384, 158496, 158681, 158874, 159083, 159101, 159157, 159195, 159740, 159863, 160197, 160335,
		160425, 160498, 161090, 161383, 161398, 161467, 161501, 162051, 162387, 162401, 162581, 162870, 162908, 162916,
		163005, 163114, 163393, 163520, 163582, 163682, 163829, 163837, 163849, 163865, 164083, 164122, 164136, 164142,
		164230, 164238, 164292, 164312, 164451, 164543, 164551, 164635, 164644, 164647, 164938, 165028, 165140, 165248,
		165407, 165452, 165641, 165720, 165854, 165974, 166009, 166228, 166257, 166314, 166368, 166470, 166633, 166640,
		166667, 166672, 166782, 166927, 166998, 167003, 167127, 167547, 167684, 167909, 168090, 168491, 168713, 169337,
		169416, 169447, 169451, 169555, 169605, 170049, 170125, 170236, 170270, 171182, 171219, 171307, 171458, 171695,
		171773, 172040, 172398, 172489, 172785, 172870, 172965, 172980, 173001, 173425, 173479, 173541, 173719, 173795,
		173869, 174188, 174708, 174758, 174868, 174920, 175191, 175654, 176017, 176087, 176098, 176169, 176472, 176594,
		176632, 176635, 176686, 176692, 176711, 176819, 176897, 176900, 176975, 177080, 177189, 177284, 177288, 177376,
		177397, 177453, 177464, 177602, 177649, 177684, 178034, 178051, 178052, 178363, 178420, 179260, 179604, 180046,
		180064, 180279, 180389, 180981, 181764, 182177, 182300, 182366, 182628, 182869, 182966, 183085, 183100, 183405,
		183485, 183913, 184477, 184672, 184799, 184850, 184867, 185255, 185394, 185809, 185872, 186620, 186801, 186985,
		188368, 189227, 189572, 190491, 191866, 192388, 193037, 193550, 193773, 194160, 194186, 194289, 194296, 194299,
		194624, 195106, 195500, 195531, 196291, 196464, 196563, 196606, 196607, 196616, 196696, 196701, 196818, 197241,
		197247, 197314, 197390, 197479, 197486, 197578, 197581, 197649, 197792, 197962, 198058, 198117, 198376, 198546,
		198707, 198768, 198843, 199045, 199152, 199496, 199654, 200110, 200283, 201403, 201731, 201752, 201814, 202008,
		202016, 202362, 202381, 202472, 202607, 202807, 202996, 203250, 203279, 203472, 203647, 203823, 203978, 204727,
		204943, 204962, 205476, 205570, 205719, 206299, 206433, 206436, 206585, 206608, 206882, 207007, 207081, 207345,
		208542, 209338, 210558, 210652, 211098, 211587, 212055, 212205, 212323, 212720, 212842, 213312, 213420, 214159,
		214163, 214334, 214534, 214553, 214572, 215153, 215283, 216156, 216191, 216341, 216399, 217003, 217047, 217254,
		217407, 217567, 218071, 218192, 218214, 218260, 218456, 218680, 218891, 218962, 218974, 219023, 219065, 219424,
		219434, 219520, 219601, 219856, 219996, 220044, 220181, 220523, 220546, 220764, 220868, 221137, 221217, 221346,
		221376, 221392, 221536, 221835, 221987, 222011, 222157, 222190, 222241, 222754, 223173, 223285, 223318, 223723,
		224163, 224322, 224515, 224516, 224694, 224876, 224883, 225244, 225249, 225426, 225528, 225577, 225628, 225876,
		226465, 226569, 226631, 227164, 227365, 227712, 228026, 228058, 228078, 228310, 228491, 228669, 228711, 228809,
		229307, 229659, 229864, 230486, 230614, 231082, 231221, 231354, 231519, 231950, 232157, 233827, 233929, 234255,
		234704, 234867, 235131, 235160, 235225, 235305, 235441, 236069, 236161, 236170, 236293, 236306, 236347, 236351,
		236472, 236572, 237156, 237219, 237373, 239227, 239856, 241770, 242454, 244000, 244107, 246068, 247390, 256324,
		265148, 266636, 273227, 291459, 293413, 301047, 301424, 302001, 309915, 320948}

	target_value := 180000
	arr_total_value := 0
	for _, value := range arrs {
		arr_total_value += value
	}
	if arr_total_value < target_value {
		return arrs
	}

	var res_arrs []int
	diff_value := arr_total_value
	go_sum := 1
	len_arrs := len(arrs)
	if len_arrs > 200 {
		go_sum = 8
	}

	ch := make(chan Result, 2)

	// go_step := len_arrs / go_sum

	// start_step := 0
	// end_step := start_step + go_step
	end_step := len_arrs
	start_step := len_arrs / 2
	for i := 0; i < go_sum; i++ {

		// if i == go_sum-1 {
		// 	end_step = len_arrs
		// }

		if i == go_sum-1 {
			start_step = 0
		}

		go find_target_go(arrs, target_value, diff_value, start_step, end_step, ch)
		// start_step = end_step
		// end_step = start_step + go_step

		end_step = start_step
		start_step = end_step / 2
	}

	for i := 0; i < go_sum; i++ {

		res := <-ch
		// fmt.Println(i, res.arrs, res.diff_value)
		if res.diff_value < diff_value {
			diff_value = res.diff_value
			res_arrs = res.arrs
		}

	}

	return res_arrs
}

func find_target_go(arrs []int, target_value, diff_value, start_step, end_step int, ch chan Result) []int {
	a := time.Now()
	fmt.Println("ok")
	var res_arrs []int
	for step := start_step; step < end_step; step++ {
		start_idx := 0
		end_idx := start_idx + step

		is_find := false
		for end_idx <= len(arrs) {
			var find_arrs []int
			find_arrs = append(find_arrs, arrs[:start_idx]...)
			find_arrs = append(find_arrs, arrs[end_idx:]...)

			total_value := 0
			_temp_total_value := 0
			// fmt.Println(find_arrs)
			for idx, value := range find_arrs {
				_temp_total_value = total_value + value

				if _temp_total_value > target_value {
					is_find = true
					prev_abs_diff_value := target_value - total_value
					now_abs_diff_value := _temp_total_value - target_value

					temp_diff_value := 0
					var temp_arrs []int
					if prev_abs_diff_value < now_abs_diff_value {
						temp_diff_value = prev_abs_diff_value
						temp_arrs = find_arrs[:idx]
					} else {
						temp_diff_value = now_abs_diff_value
						temp_arrs = find_arrs[:idx+1]
					}

					if temp_diff_value < diff_value {
						diff_value = temp_diff_value
						res_arrs = temp_arrs
					}

					break

				} else if _temp_total_value == target_value {
					// fmt.Println(find_arrs[:idx+1])
					ch <- Result{diff_value: 0, arrs: find_arrs[:idx+1]}
					fmt.Println(time.Now().Sub(a))
					return find_arrs[:idx+1]
				}

				total_value += value
			}

			if step == 0 {

				break
			}
			start_idx += 1
			end_idx = start_idx + step
		}

		if !is_find {
			break
		}

	}

	fmt.Printf("time:%d start_step: %d end_step: %d \n", time.Now().Sub(a), start_step, end_step)
	ch <- Result{diff_value: diff_value, arrs: res_arrs}
	// fmt.Println(res_arrs)
	return res_arrs

}

生成so库

// go build --buildmode=c-shared -o find_target.so hello.go

python代码调用

写入find_target.py文件;将上一步的so文件放在同目录下即可


import json
import time
import ctypes

if __name__ == "__main__":
    a = time.time()
    find_target = ctypes.CDLL("./find_target.so").FindTarget
    find_target.argtypes = [ctypes.c_int]  # 指定FindTarget方法参数数据类型
    find_target.restype = ctypes.c_char_p  # 指定FindTarget方法返回值类型
    res = find_target(180000)
    print(sum(json.loads(res.decode("U8"))))
    print(time.time() - a)
    # print(time.time()-a)

原文地址:http://www.cnblogs.com/kayb/p/16881424.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性