#include <stdio.h> //解题思路:镶嵌循环,计算b数组中每一个子数组和大小,每次内循环都要与a数组之和做差,再和上次的差比较 // 如果更小记录此时子数组的开头和结尾的下标。 int main() { int n, m, sum1 = 0, ps = 0, min = 1000000, bs = 0; scanf("%d %d", &n, &m); int a[100] = {0}, b[100] = {0}; for (int i = 0; i < n; i++) { scanf("%d ", &a[i]); sum1 += a[i]; } for (int i = 0; i < m; i++) { scanf("%d ", &b[i]); } for (int i = 0; i < m; i++) {//镶嵌循环 int sum2 = 0; for (int k = 0; k < m - i; k++) { sum2 += b[i + k];//从b[i]开始累和 int gg = sum1 - sum2;//记录每次子数组之和与a数组之和的差值 if (gg < 0) gg = -gg;//相当于做差值的绝对值 if (min > gg) {//与上一次循环的差值作比较 ps = i; bs = k + i;//如果更小记录数组的前后下标 min = gg;//并重置记录此次差值大小 } } } for (int i = ps; i <= bs; i++) {//根据下标打印子数组 printf("%d ", b[i]); } return 0; }