挑战程序设计竞赛(1)

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

1.三角形

有n根棍子,棍子i的长度为a_{i}。想要从中选出3根棍子组成周长尽可能长的三角形。请输出最大的周长,若无法组成三角形,输出0。

限制条件: 3\leqslantn\leqslant100     1\leqslanta_{i}\leqslant10^{6}

第一行输入n值,第二行输入每个棍子的长度。输出一行周长。

样例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
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值