題目描述
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);
}
}
}
③:等等等,一系列問題。