如样例
30 4
7
22 4 (第1对,跨度 18)
2 6 (第2对,跨度 4)
10 3 (第3对,跨度 7)
15 12 (第4对,跨度 3)
9 8 (第5对,跨度 1)
17 17 (第6对,跨度 0)
4 2 (第7对,跨度 2)
因此,先开通第6对好友城市,其次是第5对、再其次是第7对,依次类推。注意:判断新航道与已开通的所有航道是否交叉,都不交叉才可以开通这个新航道,否则不能。判断两条航线交叉的方法是C1-C2,D1-D2的值是否异号,异号则交叉。
这里你题目描述的方向不是太清楚,根据题意可以猜测出:河是正东西走向,城市在和两岸,因此是南北分布。只是这句话“正数C,D(C、D〈=x),描述每一对友好城市沿着河岸与西边境线的距离”。那这样一来,告诉河的宽度和长度似乎没有意义。
#include <stdio.h>
#include <stdlib.h>
int c1, c2, n, w[10]int weight = 0, max = 0
int count1 = 0, count2 = 0//添加的代码
int sum = 0
int arrC1[10], arrC2[10]//用来装C1 C2货船上的货物的重量
void search(int m)
{
if (m == n)
{
if (weight <= c1)
if (weight >= max)
max = weight
}
else
{
weight += w[m]
search(m + 1)
//添加的代码 如果max已被赋值,则c1货船装到了最大重量的货物 函数立即return不再继续执行,
//把返回路径上添加的重量放到c1的数组里
if (max>0 &&c1>=weight &&sum-weight<=c2 )
{
arrC1[count1] = w[m]count1++return
}
weight -= w[m]
search(m + 1)
//添加的代码 如果max已被赋值,则c1货船装到了最大重量的货物 函数立即return不再继续执行,
//把剔除的货物放到c2的数组里
if (max >0 /*&&c1 >= weight*/ &&sum - weight <= c2)
{
arrC2[count2] = w[m]count2++return
}
}
}
int main()
{
int i
scanf("%d%d%d", &c1, &c2, &n)
while (n != 0)
{
for (i = 0i <ni++)
{
scanf("%d", &w[i])sum += w[i]
}
search(0)
if (sum - max <= c2)
{
printf("Yes\n")
//添加的代码 输出c1货船上的货物
printf("c1: ")
for (int i = 0i <count1i++)
printf(" %d ", arrC1[i])
//输出c2货船上的货物
printf("\tc2: ")
for (int i = 0i <count2i++)
printf(" %d ", arrC2[i])
printf("\n")
}
else
printf("No\n")
max = 0
sum = 0
//添加的代码
count1 = 0
count2 = 0
//刚才忘了把weight清0 所以出错了
weight = 0
scanf("%d%d%d", &c1, &c2, &n)
}
return 0
}