Java面向对象3(K~O)

时间:2023-02-14 12:59:05

 K     正方形(SDUT 2444)

import java.lang.reflect.Array;
import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		node s[] = new node[2000];
		// System.out.println(55);
		for (int k = 0; k < t; k++) {

			for (int i = 0; i < 4; i++) {
				s[i] = new node();
				s[i].x = sc.nextInt();
				s[i].y = sc.nextInt();
				// System.out.println(s[i].x + " " + s[i].y);
			}
			s[4] = new node();
			s[4].x = s[0].x;
			s[4].y = s[0].y;
			Sum p = new Sum(s);
			if (p.getAns() == 1) {
				System.out.print("YES\n");
			} else
				System.out.print("NO\n");
		}
		sc.close();
	}
}

class node {
	int x;
	int y;
}

class Sum {
	node s[] = new node[200];

	Sum(node s[]) {
		this.s = s;
	}

	int f1() {
		int f = 1;
		for (int i = 0; i < 3; i++) {
			int x = s[i].x - s[i + 1].x;
			int y = s[i].y - s[i + 1].y;
			int z = s[i + 1].x - s[i + 2].x;
			int w = s[i + 1].y - s[i + 2].y;
			if (x * x + y * y != z * z + w * w) {
				f = 0;
				break;
			}
		}
		return f;
	}

	int f2() {
		int f = 1;
		for (int i = 0; i < 3; i++) {
			int x = (s[i + 1].x - s[i].x) * (s[i + 2].x - s[i + 1].x);
			int y = (s[i + 1].y - s[i].y) * (s[i + 2].y - s[i + 1].y);
			if (x != -y) {
				f = 0;
				break;
			}
		}
		return f;
	}

	int getAns() {
		int flag1 = f1();
		int flag2 = f2();
		if (flag1 == 1 && flag2 == 1)
			return 1;
		else
			return 0;
	}
}

相似三角形(SDUT 2562)

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

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[10];
		while (sc.hasNext()) {
			for (int i = 0; i < 6; i++) {
				a[i] = sc.nextInt();
			}
			Node p = new Node(a);
			int ans = p.getAns();
			if (ans == 1)
				System.out.println("YES");
			else
				System.out.println("NO");
		}
	}
}

class Node {
	int a[] = new int[10];

	Node(int a[]) {
		this.a = a;
	}

	int getOk() {
		int f1 = 0;
		int f2 = 0;
		if (a[0] + a[1] > a[2] && a[0] + a[2] > a[1] && a[1] + a[2] > a[0])
			f1 = 1;
		if (a[3] + a[4] > a[5] && a[3] + a[5] > a[4] && a[4] + a[5] > a[3])
			f2 = 1;
		if (f1 == 1 && f2 == 1)
			return 1;
		else
			return 0;
	}

	int getAns() {
		if (getOk() == 1) {
			int f = 0;
			Arrays.sort(a, 0, 3);
			Arrays.sort(a, 3, 6);
			if ((a[0] * a[4] == a[1] * a[3]) && (a[0] * a[5] == a[2] * a[3]) && (a[1] * a[5] == a[2] * a[4]))
				f = 1;
			return f;
		} else
			return 0;
	}
}

N     手机键盘 (SDUT 2618)

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

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s[] = new String[10];
		s[2] = "abc0";s[3] = "def0";s[4] = "ghi0"; s[5] = "jkl0";
		s[6] = "mno0"; s[7] = "pqrs"; s[8] ="tuv0";s[9] = "wxyz";
		String str;
		while (sc.hasNext()) {
			str = sc.nextLine();
			Node p;
			p = new Node(s,str);
			int ans = p.getAns();
			System.out.println(ans);
		}

}
}

class Node {
	String str;
	String s[] = new String[10];
	Node(String s[], String str) {
		this.str = str;
		this.s = s;
	}
	int getAns()
	{
		int ans = 0,f = 0;
		int flag = 0;
		int i,j,k;
		int len = str.length();
		for( i = 0; i < len; i ++)
		{
			flag = 0;
			for( j = 2; j <= 9; j ++)
			{
				for( k = 0; k < 4; k ++)
				{
					if(i == 0)
					{
						if(s[j].charAt(k) == str.charAt(i)) {
							ans += k + 1;
							f = j;
							flag = 1;
							break;
						}
					}
					else {
						if(s[j].charAt(k) == str.charAt(i))
						{
							if(f == j)ans += k + 3;
							else {ans += k + 1;f = j;}
							flag = 1;
							break;
						}
					}
				}
				if(flag == 1) break;
			}
		}
		return ans;
	}
}

2-2 Time类的定义 (SDUT 2669)

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

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int H,M,S;
		H = sc.nextInt();
		M = sc.nextInt();
		S = sc.nextInt();
		Time t = new Time(H,M,S);
		t.prit();
	}
}

class Time
{
	int h,m,s;
	Time(int H, int M, int S)
	{
		h = H;
		m = M;
		s = S;
	}
	void setH()
	{
		if(h > 12 || h < 0) h = 12;
	}
	void setM()
	{
		if(m > 60 || m < 0) m = 0;
	}
	void setS()
	{
		if(s > 60 || s < 0) s = 0;
	}
	void prit()
	{
		setH();setM();setS();
		System.out.printf("%02d:%02d:%02d\n", h,m,s);
	}
}