当前位置: 代码迷 >> 综合 >> BUPT OJ143 Triangle
  详细解决方案

BUPT OJ143 Triangle

热度:50   发布时间:2024-01-12 05:26:07.0

题目描述

Amy learned equilateral triangle today and was interested in it very much. After school, she took out her toys as usual and surprisingly found so many sticks! As the plot goes, she started to count how many different equilateral triangles they could form. However, there're countless sticks and it was not easy for such a little girl to finish this task. Help her!
An equilateral triangle is such a triangle, that the length of its three sides are equal. Two equilateral triangle are different if and only if they have different lengths of sides. Amy only uses one stick to form one side of a triangle.

 

输入格式

The input contains several cases. An integer  T(T100)  will exist in the first line of input, indicating the number of test cases.
Each test case begins with the number of sticks  N(N100) . The following line contains N numbers length[i]

( 1length[i]100 ), indicating all the lengths of sticks.

输出格式

Output the answer for each test case in respective lines.
 

输入样例

2
3
2 3 1
4
3 3 3 3

输出样例

0
1

其实是很简单的一题, 不过比赛时NC读错了, 又是一个WA再AC

感觉我WA率略高啊......

题意是找不同的等边三角形个数, 非常懒的排序然后再统计了, 感觉思路拙计时空效率也低

反正这些都是比赛代码, 过了就行, 要效率的话我再重写一个好了= =

话说我也想学C++啊, 为什么我每次都要写qsort的cmp啊, 已经写到不能忍了好么, 坑爹啊(摔



/*
USER_ID: test#birdstorm
PROBLEM: 143
SUBMISSION_TIME: 2014-03-11 00:27:40
*/
#include <stdio.h>
#include <stdlib.h>
#define For(i,m,n) for(i=m;i<n;i++)
#define MAXN 105int a[MAXN], cnt[MAXN];int cmp(const void* a, const void* b)
{return *(int*)a-*(int*)b;
}main()
{int t, i, j, k, n, m, sum;scanf("%d",&t);while(t--){scanf("%d",&n); k=sum=0;For(i,0,n) scanf("%d",&a[i]);For(i,0,n) cnt[i]=1;qsort(a,n,sizeof(int),cmp);For(i,0,n-1){if(a[i]==a[i+1]) cnt[k]++;else k++;}For(i,0,k+1) sum+=(cnt[i]>=3?1:0);printf("%d\n",sum);}return 0;
}