给定一个区间寻找三角形
One of my friend sent me below image as puzzle to count the triangles. I started counting them and first got to 15, then again recounted and reached to 19.
我的一位朋友给我发了下图作为拼图来计算三角形。 我开始计算它们,首先达到15,然后再次计数,达到19。
But I was not sure whether I was right or not. So I thought of writing a simple program to find out the total number of triangles. I used below algorithm to find out the number of triangles.
但是我不确定我是否正确。 因此,我想到编写一个简单的程序来找出三角形的总数。 我使用下面的算法找出三角形的数量。
- Any triangle has three distinct points. 任何三角形都有三个不同的点。
- Any triangle consists of three different lines. 任何三角形均由三条不同的线组成。
Now counting lines with points is easy, from below image we can easily see that it contains 7 lines.
现在用点数线很容易,从下面的图像中我们可以很容易地看到它包含7条线。
Below is the program to find out the total number of triangles in these scenarios.
下面是查找这些情况下三角形总数的程序。
package com.journaldev.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class FindTriangles {
public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
// System.out.println("Please enter number of lines");
// int count = Integer.parseInt(scanner.nextLine());
//
// List<String> lines = new ArrayList<String>();
// for(int i=0; i<count; i++){
// System.out.println("Please enter line "+(i+1));
// lines.add(scanner.nextLine());
// }
// scanner.close();
// System.out.println("Lines are::"+lines.toString());
findTriangles(Arrays.asList(new String[] { "ABC", "CDEF", "AIF",
"BGHI", "CKJI", "AGKD", "AHJE" }));
findTriangles(Arrays.asList(new String[] { "AB","BC","CA"}));
}
// Logic to find triangles
public static void findTriangles(List<String> lines) {
int count=0;
// Find total distinct points using Set
Set<String> points = new HashSet<String>();
for (String line : lines) {
char[] pointArray = line.toCharArray();
for (char c : pointArray)
points.add(c + "");
}
System.out.println("Points are::" + points.toString());
//Create Array from Set
String[] pointsArray = new String[points.size()];
pointsArray = points.toArray(pointsArray);
System.out.println(Arrays.toString(pointsArray));
//Create distinct points set by looping
for(int i=0; i< pointsArray.length-2; i++){
for(int j=i+1; j< pointsArray.length-1; j++){
for(int k=j+1; k< pointsArray.length; k++){
// Triangle has 3 distinct points and 3 different lines
String triangleOption = pointsArray[i]+pointsArray[j]+pointsArray[k];
//if all 3 points are in single line, its not a triangle
// also 2 points should be part of a line
boolean b1 = false;
boolean b2 = false;
boolean b3 = false;
for( String line : lines){
if(line.contains(pointsArray[i])
&& line.contains(pointsArray[j])
&& !line.contains(pointsArray[k])){
b1=true;
}
if(line.contains(pointsArray[i])
&& line.contains(pointsArray[k])
&& !line.contains(pointsArray[j])){
b2=true;
}
if(line.contains(pointsArray[j])
&& line.contains(pointsArray[k])
&& !line.contains(pointsArray[i])){
b3=true;
}
}
if(b1 && b2 && b3){
count++;
System.out.println(triangleOption);
}
}
}
}
System.out.println("Total Triangles="+count);
}
}
Below is the output of the above program.
下面是上述程序的输出。
Points are::[D, E, F, G, A, B, C, H, I, J, K]
[D, E, F, G, A, B, C, H, I, J, K]
DEA
DFA
DAC
DCK
EFA
EAC
ECJ
FAC
FCI
GAB
GAH
GAI
GIK
ABH
ABI
ACI
ACJ
ACK
AHI
AIJ
AIK
AJK
BCI
HIJ
Total Triangles=24
Points are::[A, B, C]
[A, B, C]
ABC
Total Triangles=1
So the total number of triangles is 24, well I missed a lot in counting them. But now I have an easy way to do it, I hope it will help someone too for these kind of problems.
所以三角形的总数是24,所以我在计算它们时非常想念。 但是,现在我有一个简单的方法来实现它,我希望它也可以对此类问题有所帮助。
翻译自: https://www.journaldev.com/7064/count-the-number-of-triangles-in-given-picture-programmatic-solution
给定一个区间寻找三角形