upc 个人训练赛 第五场 Problem A Make a Rectangle

题目描述

We have N sticks with negligible thickness. The length of the i-th stick is Ai.
Snuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides. Find the maximum possible area of the rectangle.

Constraints
4≤N≤105
1≤Ai≤109
Ai is an integer.

输入

Input is given from Standard Input in the following format:
N
A1 A2 ... AN

输出

Print the maximum possible area of the rectangle. If no rectangle can be formed, print 0.

样例输入

6
3 1 2 4 2 1

样例输出

2

提示

1×2 rectangle can be formed.

-----------------------------------------------------------------------------------------------------------------

”这么简单的题,我为什么做不出来???????????????????????"

这是我思考半个小时之后对这道题的看法

这道题的算法应该很简单,简单到我读两遍题就知道应该怎么做。但是我并不能实现它。

我的思路:找出一个数组中的重复两遍及两遍以上的元素,再把这些元素作比较,得到最大和次大两个元素,相乘得最大面积。

遍历两遍数组,找出相同的元素。把这些a元素提出来存在另一个数组b里,遍历这个数组进行比较,选出最大的和最小的。用k计数,知道b数组中元素的个数,如果为2,则不用遍历。最后相乘输出。

我的困难:①我采用的是最最简单的遍历,遍历方法应该是最麻烦的,时间复杂度会很大。

②相同元素的查找总是错误的,代码如下

#include<stdio.h>
int main()
{
    int n;
    while(scanf("%d",&n))
    {
        int a[10005];
        int b[10005];
        int k=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(i=j)j++;
                if(a[i]==a[j])
                    {
                        b[k]=a[i];
                        k++;
                        printf("%d  ",b[k]);
                    }
            }
        }
        printf("%d  ",k);
        for(int i=0;i<k;i++)
        {
            printf("%d ",b[i]);
        }
        printf("\n");
        if (k==0)printf("0");
        if(k!=0)
        {
            int s=b[0];
            printf("%d   ",s);
            int s1=b[1];
            printf("%d   ",s1);
            int sum=s*s1;
            printf("%d\n",sum);
        }

    }
}

③:等等等,一系列问题。

猜你喜欢

转载自blog.csdn.net/shahare/article/details/81237507