2736. Canvas Frames(c++)

题目描述:
Canvas Frames
Nicholas, a painter is going to paint several new canvases. Nicholas is sure that the canvases will turn out so great that each one will need framing and being hung on the wall. Frames are what Nicholas decided to begin with. Nicholas has n sticks whose lengths equal a1,a2,… an. Nicholas does not want to break the sticks or glue them together. To make a h×w-sized frame, he needs two sticks whose lengths equal h and two sticks whose lengths equal w. Specifically, to make a square frame (when h=w), he needs four sticks of the same length.Now Nicholas wants to make from the sticks that he has as many frames as possible; to be able to paint as many canvases as possible to fill the frames. Help him in this uneasy task. Note that it is not necessary to use all the sticks Nicholas has.

Input
The first line contains an integer n (1≤n≤100) − the number of sticks. The second line contains n space-separated integers. The i-th integer equals the length of the i-th stick ai (1≤ai≤100).

Output
Print the single number − the maximum number of frames Nicholas can make for his future canvases.

Examples
Input

5
2 4 3 2 3

Output

1

Input

13
2 2 4 4 4 4 6 6 6 7 7 9 9

Output

3

Input

4
3 3 3 5

Output

0

这道题就是让我们用给出的n个棍子得出所能制作的最多框架数。h*w大小的框架要求有两根长度为h、两根长度的w的棍子。当h=w时,相当于用四根相等长度的棍子。
这道题目可以看作是给出n个给定长度的棍子,用两对长度相同的棍子即可组成一个框架。看最多得到几个框架。
我的代码思路如下:
使用一个一维数组记录n个棍子的长度,对数组里长度相等的棍子进行计数,记录到一个新数组中。比如样例输入1中,5根棍子的长度为2、4、3、2、3,那么长度为2的有2根,为3的有2根,为4的有1根,然后得到的新数组的值为2、2、1。然后看新数组中一共有几个2,每两个2便可得到一个框架。意思就是看长度相等的棍子有几对,每两对便可得到一个框架。

#include<iostream>
#include<algorithm>
using namespace std;
int a[100]={0};//记录棍子长度
int same[100]={0};//记录相同长度的棍子数量
int main()
{
	int n;cin>>n;
	for(int i=0;i<n;i++)
	{cin>>a[i];}
	sort(a,a+n);//sort排序,从小到大排序
	int k=0;same[0]=1;//k记录有几种长度不同的棍子
	for(int i=1;i<n;i++)
	{
		if(a[i]==a[i-1])
		{same[k]++;}//长度相同的,在same[k]上计数
		else if(a[i]!=a[i-1])
		{k++;same[k]++;} //长度不同,k++,并在same[k]上计数	
	}
	int kk=0;
	for(int i=0;i<=k;i++)
	{
		kk+=same[i]/2;
	}//记录得到same数组中有kk个2,kk/2即可得到答案
	cout<<kk/2;
 	return 0;
}

更简洁的方法:

#include<iostream>
using namespace std;
int same[10000]={0};//记录相同长度的棍子数量
int main()
{
	int n,x;cin>>n;
	int kk=0;
	for(int i=0;i<n;i++)
	{
		cin>>x;same[x]++;
	}
	for(int i=1;i<=100;i++)same[i]/=2;//得到每个长度有多少对相同的
	for(int i=1;i<=100;i++)kk+=same[i];//累加得到一共有多少对
	cout<<kk/2;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值