Triangles

4105: Triangles 分享至QQ空间

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
总提交: 123            测试通过:27

描述

 

Triangle is the most stable graphics,so zzd likes to collect it.He wants to collect every different triangles and put them on the wall.In order to make triangles,zzd finds many sticks with different length.Can you tell him how many triangles he can make?

输入

 

The first line of each test case contains the number of sticks 0 ≤ N ≤ 2500.

The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces.

The input is terminated by a set starting with N = 0. This set should not be processed.

输出

 

Output must contain a single integer - the number of the triangles zzd could make.

样例输入

3
3 4 5
6
1 2 3 4 5 6
0

 

样例输出

Case 1: 1
Case 2: 7

 

题目来源

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int n,jishu;
 5 int arr[2505];
 6 
 7 int Binary(int left,int right,int num){
 8     int ans=0;
 9     int ee=right;
10     while(left<=right){
11         int mid=left+right>>1;
12         if(arr[mid]>=num) ans=mid,right=mid-1;
13         else left=mid+1;
14     }
15 //    cout << ans << endl;
16     if(ans==0) return 0;   //,没有找到
17     return ee-ans+1;   //找到返回个数
18 }
19 
20 int main(){
21     ios::sync_with_stdio(false);
22 
23     while(cin>>n&&n!=0){
24         for(int i=1;i<=n;i++) cin>>arr[i];
25         sort(arr+1,arr+1+n);
26         int res=        0;
27         for(int i=1;i<=n;i++){
28             for(int j=i+2;j<=n;j++){   //先确定两个边界  一个上届一个下届  
29                 int num=arr[j]-arr[i]+1;  //找大于等于num的第一个数
30                 res+=Binary(i+1,j-1,num);
31             }   
32         }
33         cout << "Case " << ++jishu << ": ";
34         cout << res << endl;
35     }
36     return 0;
37 }
View Code
原文地址:https://www.cnblogs.com/qq-1585047819/p/11321657.html