挑战程序设计竞赛（1）

2019.1.23 第一次打开这本书，p16热身题

1.三角形

 样例1 输入： 5 2 3 4 5 10 输出： 12 样例2 输入： 4 4 5 10 20 输出： 0

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int n,a[105];
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
///先判断能否构成三角形
sort(a,a+n);
if(a[0]+a[1]<=a[2])
{
printf("0\n");
}
///能构成，从最大数开始遍历符合三角形构成条件的最大数
else
{
for(int i=n-1; i>=0;)
{
if(a[i]<a[i-1]+a[i-2])
{
printf("%d\n",a[i]+a[i-1]+a[i-2]);
break;
}
else
i--;
}
}
}
}


2.蚂蚁（Ants，POJ1852）

Description

An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.

Input

The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.

Output

For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.

Sample Input

2
10 3
2 6 7
214 7
11 12 7 13 176 23 191


Sample Output

4 8
38 207

n只蚂蚁以每秒1cm的速度在长为Lcm的竿子上爬行。当蚂蚁爬到竿子的端点时就会掉落。由于竿子太细，两只蚂蚁相遇时，它们不能交错通过，只能各自反向爬回去。对于每只蚂蚁，我们知道它距离竿子左端的距离$x_{i}$，但不知道它当前的朝向。请计算所有蚂蚁落下竿子所需的最短时间和最长时间。

#include<stdio.h>
#include<algorithm>
using namespace std;
long long a[1000005];
int main()
{
long long l,n,tem,t;
scanf("%lld",&t);//组数
while(t--)
{
scanf("%lld%lld",&l,&n);
if(l%2!=0)
tem=(l+1)/2;
else
tem=l/2;
for(long long i=0; i<n; i++)
{
scanf("%lld",&a[i]);
if(a[i]>=tem)//将所有位置坐标都转化为“最小值”，即超过绳长一半的坐标转化成绳长减原值
a[i]=l-a[i];
}
sort(a,a+n);
printf("%lld %lld\n",a[n-1],l-a[0]);
}
}

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

04-06

07-25 1730
09-12 2万+
04-15 176
03-28
09-13 8722
10-11
06-24 161
09-10
07-30 1480
02-07 733
10-06 100
01-25 133
02-23 377