首先输入一个正整数N,接下来的一行共有N个正整数表示每个木棒的长度。
N ≤ 50, 木棒的长度 ≤ 10000.
输出一个整数表示方法数。
5 1 2 3 4 5
3
public static int sanjiaoxing(int[] arrys) { int n = 0; int length = arrys.length; for (int i = 0; i < length - 2; i++) { for (int j = i + 1; j < length - 1; j++) { for (int k = j + 1; k < length; k++) { if (arrys[i] < arrys[j] + arrys[k] && arrys[j] < arrys[i] + arrys[k] && arrys[k] < arrys[i] + arrys[j]) { n++; } } } } return n; }
利用了暴力枚举
}
process.stdin.resume();process.stdin.setEncoding('ascii');var input = "";var input_array = "";process.stdin.on('data', function (data) {input += data;});process.stdin.on('end', function () {input_array = input.split("\n");var arr = input_array[1].split(" ");for(var i = 0; i < arr.length; i++){arr[i] = +arr[i];}arr.sort(function(a, b) { return a - b; });var str = arr.join(",");var count = 0;for(var i = 0; i < arr.length - 1; i++){for(var j = i + 1; j < arr.length; j++){var min = Math.abs(arr[i] - arr[j]);var max = arr[i] + arr[j];for(var k = arr[j] + 1; k < max; k++){if(arr.indexOf(k) >= 0){var index = arr.indexOf(k);while(arr[index] == k){if( index != i && index != j){count++;}index++;}}}}}console.log(count);});
暴力解决。。。感觉自己好low。。。 #include<iostream> #include<vector> using namespace std; int main() { int n; cin>>n; int count=0; vector<int>a(n); for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<n-2;i++) for(int j=i+1;j<n-1;j++) for(int k=j+1;k<n;k++) if(a[i]+a[j]>a[k]&&a[i]+a[k]>a[j]&&a[j]+a[k]>a[i]) count++; cout<<count<<endl; return 0; }
import java.util.Arrays; import java.util.Scanner; /** * Created by Scruel on 2017/3/24. * Personal blog : http://blog.csdn.net/scruelt * Github : https://github.com/scruel */ public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = input.nextInt(); } System.out.println(solve(arr)); } private static int solve(int[] arr) { int cnt = 0; Arrays.sort(arr); for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { for (int k = j + 1; k < arr.length; k++) { if (arr[i] + arr[j] > arr[k]) cnt++; else break; } } } return cnt; } }
import java.util.Arrays; import java.util.Scanner; /** * Created by Scruel on 2017/3/24. * Personal blog : http://blog.csdn.net/scruelt * Github : https://github.com/scruel */ public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = input.nextInt(); } System.out.println(solve(arr)); } private static int solve(int[] arr) { int cnt = 0; Arrays.sort(arr); int n = arr.length; //二分查找降低复杂度到O(n^2logn) for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { int num = Arrays.binarySearch(arr, arr[i] + arr[j]); if (num < 0) num = -(num + 2); if (num == -1) continue; if (arr[num] == arr[i] + arr[j]) { cnt += num - j - 1; //去除binarySearch返回的index值不是相等情况中第一个出现的位置的多余统计 for (int k = num - 1; k >= j; k--) { if (arr[num] == arr[k]) cnt--; else break; } } else if (arr[num] < arr[i] + arr[j]) cnt += num - j; } } return cnt; } }
importjava.util.Scanner;//组装三角形,暴力枚举三边,然后依次判断三边是否能组成三角形即可publicclassMain {publicstaticbooleancheck(inta,intb,intc){returna < b + c && b < a + c && c < a + b;}publicstaticvoidmain(String[] args){Scanner in = newScanner(System.in);while(in.hasNext()){intn = Integer.valueOf(in.nextLine());int[] arr = newint[n];for(inti = 0; i < arr.length; i++){arr[i] = in.nextInt();}intcount = 0;for(inti = 0; i < n; i++){for(intj = i + 1; j < n; j++){for(intk = j +1; k < n; k++){if(check(arr[i],arr[j],arr[k])){count++;}}}}System.out.println(count);}}}
package main import ( "fmt" "sort" ) func main() { var arr []int var count int fmt.Scanln(&count) if count < 3 { fmt.Println(0) return } for i := 0; i < count; i++ { var num int fmt.Scanf("%d", &num) arr = append(arr, num) } sort.Ints(arr) // 尺取 ans := 0 for i := 0; i < len(arr) - 2; i ++ { l,r := i + 1, i + 2 for r < len(arr) { if r == l { r ++ continue } // 任意两边和大于另一个边 abL := arr[i] + arr[l] if abL > arr[r] { mostLessRight := r for mostLessRight < len(arr) && arr[mostLessRight] < abL { mostLessRight ++ } ans += mostLessRight - r l ++ }else { l ++ } } } fmt.Println(ans) }
class Solution { public: int triangleNumber(vector<int>& nums) { int n = nums.size(); int res = 0; for(int i = n - 1; i >= 0; i++) { int left = 0, right = i - 1; while(left < right) { if(nums[left] + nums[right] > nums[i]) { res += right - left; --right; } else ++left; } } return res; } }
#include <iostream> using namespace std; int* order(int array[],int n){ int temp; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(array[j]<array[i]){ temp = array[i]; array[i] = array[j]; array[j] = temp; } } } return array; } int main(int argc, const char * argv[]) { int n,count=0,m; cin>>n; int array[n]; for(int i=0;i<n;i++){ cin>>array[i]; } int temp; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(array[j]<array[i]){ temp = array[i]; array[i] = array[j]; array[j] = temp; } } } for(int i=0;i<n-2;i++){ for(int j=i+1;j<n-1;j++){ m = j+1; while(array[i]+array[j]>array[m] &&m<n){ count++; m++; } } } cout<<count; }
/**
* 算法的复杂度是O(n平方)
* 思路是穷举顺序依次取两条边(i,j),i是从2开始(不可能从1开始),j是从i+1开始
* */
public static int countOfTriangle(int max) {
if (max <= 3) {
return 0;
}
int count = 0;
// i最大的是:max - 2,所以i < max - 1
for (int i = 2; i < max - 1; i++) {
for (int j = i + 1; j < max; j++) { // j 最大的是:max - 1,所以j < max
// 第三条边最小的可能值
int minK = j + 1;
// 第三条边最大的可能值
int maxK = Math.min(max, i + j - 1);
int kCount = 0;
if (maxK >= minK) {
kCount = maxK - minK + 1;
}
count += kCount;
}
}
return count;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, const char * argv[]) {
int n, count = 0;
cin>>n;
int l[n];
for (int i = 0; i < n; i++) {
cin>>l[i];
}
sort(l, l + n);
int aIndex, bIndex, cIndex;
for (aIndex = 0; aIndex < n - 2; aIndex++) {
for (bIndex = aIndex + 1; bIndex < n - 1; bIndex++) {
for (cIndex = n - 1; cIndex > bIndex; cIndex--) {
if (l[aIndex] + l[bIndex] > l[cIndex]) {
count += cIndex - bIndex;
break;
}
}
}
}
cout<<count;
return 0;
}
#include<iostream> using namespace std; bool bool***(int a,int b,int c) { int ma = a>b?a:b; int mb = a<b?a:b; int mc = ma>c?ma:c; ma = ma<c?ma:c; if(ma+mb > mc) return 1; return 0; } int main() { int n,ret = 0; cin >> n; int nums[n]; for(int i=0;i<n;i++) { cin >> nums[i]; } for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) for(int k=j+1;k<n;k++) { if(bool***(nums[i],nums[j],nums[k])) ret ++; } cout << ret; return 0; }
//暴力枚举 11 #include <stdio.h> 12 #include <stdlib.h> 13 14 //判断 i j k 是否相等 15 int func1(int i,int j,int k) 16 { 17 int stat1=0; 18 19 if(i == j) 20 stat1=1; 21 else if(i==k) 22 stat1=1; 23 else if(j==k) 24 stat1=1; 25 else 26 ; 27 28 return stat1; 29 } 30 31 //测试ijk是否满足条件 32 int func2(int i,int j,int k) 33 { 34 int stat2=0; 35 36 if(i+j>k && i+k>j && j+k>i) 37 stat2=1; 38 39 return stat2; 40 } 41 42 43 int main(void) 44 { 45 int N=0; 46 long int cnt=0; 47 int i=0; 48 int j=0; 49 int k=0; 50 51 printf("input N(<=10000)\n"); 52 if(scanf("%d",&N) == 1){ 53 if(N<10000 && N>0){ 54 for(i=1; i<=N; i++){ 55 for(j=i+1; j<=N; j++){ 56 for(k=j+1; k<=N; k++){ 57 if(!func1(i,j,k)){ 58 if(func2(i,j,k)) 59 cnt++; 60 } 61 } 62 } 63 } 64 printf("***%ld***\n",cnt); 65 } 66 else{ 67 printf("error:N\n"); 68 return EXIT_FAILURE; 69 } 70 } 71 else{ 72 printf("error:N\n"); 73 return EXIT_FAILURE; 74 } 75 76 return 0; 77 }