int time[1000]
int main()
{
int person
int i, j
int index
int sum=0
scanf("%d", &person)
for(i=0i<personi++)
scanf("%d", &time[i])
if(1==person)
{
sum = time[0]
printf("%d\n", sum)
return 0
}
else if(2==person)
{
sum = time[1]
printf("%d\n", sum)
return 0
}
else if(3==person)
{
sum = time[0]+time[1]+time[2]
printf("%d\n", sum)
return 0
}
//从小到大排序
for(i=0i<personi++)
for(j=i+1j<personj++)
{
int tmp
if(time[i]>time[j])
{
tmp = time[i]
time[i] = time[j]
time[j] = tmp
}
}
index = person-1
while(index>2)
{
sum += (time[0]+2*time[1]+time[index])
index -= 2
}
if(2==index)
{
sum += (time[0]+time[1]+time[2])
}
else
{
sum += time[1]
}
printf("%d\n", sum)
return 0
}
设信号量: MUTEX=1 (东西方互斥)MX=1(西向东使用计数变量互斥)
设整型变量: CD=0 (东向西的已上桥人数)
CX=0 (西向东的已上桥人数)
从东向西:
P (MD)
IF (CD=0)
{P (MUTEX) }
CD=CD+1
V (MD)
P (MD)
CD=CD-1
IF (CD=0)
{V (MUTEX) }
V (MD)
从西向东:
P (MX)
IF (CX=0)
{P (MUTEX) }
CX=CX+1
V (MX)
过桥
P (MX)
CX=CX-1
IF (CX=0)
{V (MUTEX) }
V (MX)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)