1.

package triangularVerticies;
/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, WS10/11
* Problem: 209 Triangular Verticies
* Link: http://uva.onlinejudge.org/external/2/209.pdf
*
* @author Patrick Bédat
* @author Christoph Göttschkes
* @version 1.0, 10/29/2010
*
* Method : Extended Euclid Algorithm
* Status : Accepted
* Runtime: 1.120
*/
import java.io.*;
import java.util.*;

public class Main
{

public static void main(String[] args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

do
{
String line = reader.readLine();
StringTokenizer tokenizer = new StringTokenizer(line);
int numberOfTokens = tokenizer.countTokens();

String output = "";

int[] inputPoints = new int[numberOfTokens];
for (int i = 0; i < numberOfTokens; i++)
inputPoints[i] = Integer.parseInt(tokenizer.nextToken());

int[] points = Arrays.copyOf(inputPoints, numberOfTokens);
Arrays.sort(points);

switch (numberOfTokens)
{
case 3:
if (checkTriangle(points))
output += inputPoints[0] + " " + inputPoints[1] + " " + inputPoints[2] + " are the vertices of a triangle";
break;
case 4:
if (checkParallelogram(points))
output += inputPoints[0] + " " + inputPoints[1] + " " + inputPoints[2] + " " + inputPoints[3] + " are the vertices of a parallelogram";
break;
case 6:
if (checkHexagon(points))
output += inputPoints[0] + " " + inputPoints[1] + " " + inputPoints[2] + " " + inputPoints[3] + " " + inputPoints[4] + " "
+ inputPoints[5] + " are the vertices of a hexagon";
break;
}

if (output.isEmpty())
{
for (int i : inputPoints)
output += i + " ";
output += "are not the vertices of an acceptable figure";
}

System.out.println(output);

} while (reader.ready());
}

public static boolean checkHexagon(int[] points)
{
int m = points[2] + (points[1] - points[0]);
return checkTriangle(new int[]
{
points[0], points[2], m
})
&& checkTriangle(new int[]
{
points[0], points[1], m
})
&& checkTriangle(new int[]
{
points[2], m, points[4]
})
&& checkTriangle(new int[]
{
m, points[4], points[5]
})
&& checkTriangle(new int[]
{
m, points[3], points[5]
})
&& checkTriangle(new int[]
{
points[1], m, points[3]
});
}

public static boolean checkParallelogram(int[] points)
{
int[] lines = new int[]
{
getRowNumber(points[0]), getRowNumber(points[1]), getRowNumber(points[2]), getRowNumber(points[3])
};

if (lines[0] == lines[1])
{
return (checkTriangle(new int[]
{
points[0], points[2], points[3]
}) && checkTriangle(new int[]
{
points[0], points[1], points[3]
}))
|| (checkTriangle(new int[]
{
points[0], points[1], points[2]
}) && checkTriangle(new int[]
{
points[1], points[2], points[3]
}));
} else if (lines[1] == lines[2])
{
return checkTriangle(new int[]
{
points[0], points[1], points[2]
}) && checkTriangle(new int[]
{
points[1], points[2], points[3]
});
} else
{
return false;
}
}

public static boolean checkTriangle(int[] points)
{
int[] lines = new int[]
{
getRowNumber(points[0]), getRowNumber(points[1]), getRowNumber(points[2])
};

if (lines[0] == lines[1])
{
int goDown = points[1] - points[0];
int first = points[0];
for (int i = lines[0] + 1; i < lines[0] + 1 + goDown; i++)
{
first += i;
}
return first == points[2];
} else if (lines[1] == lines[2])
{
int goUp = points[2] - points[1];
int first = points[1];
for (int i = lines[1] - 1; i > lines[1] - 1 - goUp; i--)
first -= i;
return first == points[0];
} else
{
return false;
}
}

public static int getRowNumber(int n)
{
return (int) Math.ceil(((Math.sqrt(1 + 8 * n) - 1) / 2));
}
}





2.

/**
* ACM Problem 209 - Triangular Vertices
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=145
*
* @author Miesel Christoph
* @version 2.0, 05/07/2009
* Status : Accepted
* Runtime: 0.880
*/

import java.io.*;
import java.util.*;

public class TriangularVertices
{
static int[] triangle(int a, int b, int c)
{
int[] array = new int[] {a, b, c};
Arrays.sort(array);
return array;
}

static int[] para(int a, int b, int c, int d)
{
int[] array = new int[] {a, b, c, d};
Arrays.sort(array);
return array;
}

static int[] hexagon(int a, int b, int c, int d, int e, int f)
{
int[] array = new int[] {a, b, c, d, e, f};
Arrays.sort(array);
return array;
}

public static void main(String[] args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
StringTokenizer token = new StringTokenizer(line);
int counter = 1;
int x1=0,x2=0,x3=0,x4=0,x5=0,x6=0;

while(line != null)
{
token = new StringTokenizer(line);
while(token.hasMoreTokens())
{
switch(counter)
{
case 1: {x1 = Integer.parseInt(token.nextToken()); counter++; break;}
case 2: {x2 = Integer.parseInt(token.nextToken()); counter++; break;}
case 3: {x3 = Integer.parseInt(token.nextToken()); counter++; break;}
case 4: {x4 = Integer.parseInt(token.nextToken()); counter++; break;}
case 5: {x5 = Integer.parseInt(token.nextToken()); counter++; break;}
case 6: {x6 = Integer.parseInt(token.nextToken()); counter++; break;}
default: break;
}

}

// counter ist hier genau um 1 zu hoch!!!!!!!

switch(counter)
{
case 4: //Dreieck
{
int n1 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x1));
int n2 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x2));
int n3 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x3));
int[] tests = triangle(n1,n2,n3);
int[] points = triangle(x1,x2,x3);

if(tests[0]==tests[1] && tests[0] != tests[2])
{
int diff = points[1]-points[0];

if(diff==(tests[2]-tests[1]) && points[2] == 0.5*tests[2]*(tests[2]+1)-(0.5*tests[1]*(tests[1]+1)-points[1])-diff)
System.out.println(x1+" "+x2+" "+x3+" are the vertices of a triangle");
else System.out.println(x1+" "+x2+" "+x3+" are not the vertices of an acceptable figure");
}
else if(tests[1]==tests[2] && tests[1] != tests[0])
{
int diff = points[2]-points[1];

if(diff==(tests[1]-tests[0]) && points[1]== 0.5*tests[2]*(tests[2]+1)-(0.5*tests[0]*(tests[0]+1)-points[0])-diff)
System.out.println(x1+" "+x2+" "+x3+" are the vertices of a triangle");
else System.out.println(x1+" "+x2+" "+x3+" are not the vertices of an acceptable figure");
}
else System.out.println(x1+" "+x2+" "+x3+" are not the vertices of an acceptable figure");
break;
}
case 5: // Parallelogramm
{
int n1 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x1));
int n2 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x2));
int n3 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x3));
int n4 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x4));
int[] tests = para(n1,n2,n3,n4);
int[] points = para(x1,x2,x3,x4);

if(tests[1]==tests[0])
{
int diff = points[1]-points[0];
if(diff==points[3]-points[2]&&diff==tests[3]-tests[1]&&diff==tests[2]-tests[1] && diff!=0)
{
if((0.5*tests[1]*(tests[1]+1)-points[1]==0.5*tests[3]*(tests[3]+1)-diff-points[3]) || (0.5*tests[1]*(tests[1]+1)-points[1]==0.5*tests[3]*(tests[3]+1)-points[3]))
{
System.out.println(x1+" "+x2+" "+x3+" "+x4+" are the vertices of a parallelogram");
}
else System.out.println(x1+" "+x2+" "+x3+" "+x4+" are not the vertices of an acceptable figure");
}
else System.out.println(x1+" "+x2+" "+x3+" "+x4+" are not the vertices of an acceptable figure");
}
else
{
if(tests[1]==tests[2])
{
int diff = points[2]-points[1];
if(2*diff==tests[3]-tests[0]&&tests[1]-tests[0]==diff&&diff==tests[3]-tests[2]&&diff!=0)
{
if(0.5*tests[0]*(tests[0]+1)-points[0]==0.5*tests[2]*(tests[2]+1)-points[2] && 0.5*tests[0]*(tests[0]+1)-points[0]==0.5*tests[3]*(tests[3]+1)-points[3]-diff)
{
System.out.println(x1+" "+x2+" "+x3+" "+x4+" are the vertices of a parallelogram");
}
else System.out.println(x1+" "+x2+" "+x3+" "+x4+" are not the vertices of an acceptable figure");
}
else System.out.println(x1+" "+x2+" "+x3+" "+x4+" are not the vertices of an acceptable figure");
}
else System.out.println(x1+" "+x2+" "+x3+" "+x4+" are not the vertices of an acceptable figure");
}
break;
}
case 7: // Hexagon
{
int n1 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x1));
int n2 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x2));
int n3 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x3));
int n4 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x4));
int n5 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x5));
int n6 = (int)Math.ceil(-0.5+0.5*Math.sqrt(1+8*x6));

int[] points = hexagon(x1,x2,x3,x4,x5,x6);
int[] tests = hexagon(n1,n2,n3,n4,n5,n6);


if(tests[1]==tests[0])
{
int diff = points[1]-points[0];
if(diff==tests[2]-tests[0]&&tests[3]-tests[1]==diff&&tests[5]-tests[3]==diff&&tests[4]-tests[2]==diff&&points[5]-points[4] ==diff&&points[3]-points[2]==2*diff && diff!=0)
{
if(0.5*tests[1]*(tests[1]+1)-points[1]==0.5*tests[3]*(tests[3]+1)-points[3]&&0.5*tests[1]*(tests[1]+1)-points[1]==0.5*tests[5]*(tests[5]+1)-points[5]-diff)
{
System.out.println(x1+" "+x2+" "+x3+" "+x4+" "+x5+" "+x6+" are the vertices of a hexagon");
}
else System.out.println(x1+" "+x2+" "+x3+" "+x4+" "+x5+" "+x6+" are not the vertices of an acceptable figure");
}
else System.out.println(x1+" "+x2+" "+x3+" "+x4+" "+x5+" "+x6+" are not the vertices of an acceptable figure");
}
else System.out.println(x1+" "+x2+" "+x3+" "+x4+" "+x5+" "+x6+" are not the vertices of an acceptable figure");
break;
}
default:
{

switch(counter)
{
case 2:
{
System.out.println(x1+" are not the vertices of an acceptable figure");
break;
}
case 3:
{
System.out.println(x1+" "+x2+" are not the vertices of an acceptable figure");
break;
}
case 6:
{
System.out.println(x1+" "+x2+" "+x3+" "+x4+" "+x5+" are not the vertices of an acceptable figure");
break;
}

}
}
}
line = reader.readLine();
counter = 1;
}
}
}


2.

package problemSetVolumes.volume002;

import java.util.Arrays;
import java.util.Scanner;

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 209 - Triangular Vertices
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=4&problem=145&mosmsg=Submission+received+with+ID+8356505
*
* @author Siegfried Ippisch
* @version 1.0
*
* Method : -
* Status : Accepted
* Runtime: 1.640
*/
public class TriangularVertices {

static class Point{
int line, index;
Point(int line, int index){
this.line = line; this.index = index;
}

public String toString(){
return "line: "+line+" index: "+index;
}
}

static Point[] points;

static void init(int max){
points = new Point[max];

int i = 1;

try{
for(int line=0; true; line++){
for(int index=0; index<=line; index++){
points[i++] = new Point(line,index);
}
}
} catch(IndexOutOfBoundsException e){}
}

public static void main(String[] args){
init(32768);
Scanner in = new Scanner(System.in);

while(in.hasNext()){
String line = in.nextLine();
String[] s = line.split(" ");

if(s.length == 3)
System.out.println(line + (
containsTriangle( points[Integer.parseInt(s[0])],
points[Integer.parseInt(s[1])],
points[Integer.parseInt(s[2])]) ?
" are the vertices of a triangle": " are not the vertices of an acceptable figure"));

else if(s.length == 4)
System.out.println(line + (
containsParallelogram( Integer.parseInt(s[0]),
Integer.parseInt(s[1]),
Integer.parseInt(s[2]),
Integer.parseInt(s[3])) ?
" are the vertices of a parallelogram": " are not the vertices of an acceptable figure"));

else if(s.length == 6)
System.out.println(line + (
containsHexagon(Integer.parseInt(s[0]),
Integer.parseInt(s[1]),
Integer.parseInt(s[2]),
Integer.parseInt(s[3]),
Integer.parseInt(s[4]),
Integer.parseInt(s[5])) ?
" are the vertices of a hexagon": " are not the vertices of an acceptable figure"));

else
System.out.println(line + " are not the vertices of an acceptable figure");
}

in.close();
}

static boolean containsTriangle(Point p1, Point p2, Point p3){

// Tauschen, das p1 und p2 in der gleichen Zeile sind
if(p1.line == p2.line){
} else if(p2.line == p3.line){
Point x = p3;
p3 = p1;
p1 = x;
} else if(p1.line == p3.line){
Point x = p3;
p3 = p2;
p2 = x;
} else
return false; // Punkte sind auf 3 linien verteilt

int lineDiff = Math.abs(p3.line - p1.line);
if(lineDiff == 0 || lineDiff != Math.abs(p1.index - p2.index) )
return false; // Punkt 3 hat den falschen Abstand (Line)

if(p3.line < p1.line && Math.min(p1.index, p2.index) == p3.index)
return true;
else if(Math.min(p1.index, p2.index) + lineDiff == p3.index)
return true; // Falls index Passt -> Dreieck
return false;
}

static boolean containsParallelogram(int... p){
Arrays.sort(p);
return (containsTriangle(points[p[0]],points[p[1]],points[p[2]]) ||
containsTriangle(points[p[0]],points[p[1]],points[p[3]]) )&&
(containsTriangle(points[p[2]],points[p[3]],points[p[0]]) ||
containsTriangle(points[p[2]],points[p[3]],points[p[1]]) );
}

static boolean containsHexagon(int... p){
Arrays.sort(p);
if(points[p[2]].line != points[p[3]].line)
return false;
Point m = new Point(points[p[2]].line, (points[p[2]].index + points[p[3]].index) /2);
return containsTriangle(m,points[p[0]],points[p[1]])
&& containsTriangle(m,points[p[1]],points[p[3]])
&& containsTriangle(m,points[p[3]],points[p[5]])
&& containsTriangle(m,points[p[5]],points[p[4]])
&& containsTriangle(m,points[p[4]],points[p[2]])
&& containsTriangle(m,points[p[2]],points[p[0]]);
}
}