Java面向对象3(K~O)

Java面向对象 同时被 3 个专栏收录
12 篇文章 0 订阅
44 篇文章 1 订阅

 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);
	}
}

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页

打赏

Mercury_Lc

愿闻天下事,愿读圣贤书。

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值