刷题计划

刷题的计划

小结

最好在每次的刷题结束后都总结一下,刷题的经验
为了以后的进步
就比如最近刷了几个入门的题目

P1534

package com.LG.Base;

import java.util.Scanner;

public class P1534 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt(), a, b, s = 0, c = 0;
		for (int i = 0; i < n; i++) {
			a = in.nextInt();
			b = in.nextInt();
			c += a + b - 8;
			// c = a + b - 8;
			s += c;
		}
		System.out.println(s);
		in.close();
	}

}

要注意看清题意

P1548

package com.LG.Base;

import java.util.Scanner;

public class P1548 {
	// It can be solved by formula
	// But I don't know how to do it.
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt(), b = in.nextInt();
		int c = 0, d = 0;
		for (int i = 0; i < a; i++) {
			for (int j = 0; j < b; j++) {
				for (int j2 = i + 1; j2 <= a; j2++) {
					for (int k = j + 1; k <= b; k++) {
						if (j2 - i == k - j) {
							c++;
						} else {
							d++;
						}
					}
				}
			}
		}
		System.out.println(c + " " + d);
		in.close();
	}

}

其实是有公式的,只是当时还不知道
下面引用的别人的推导方法


说说公式是怎么推导的吧

找规律:

正方形:

边长为1的正方形个数为n*m

边长为2的正方形个数为(n-1)*(m-1) (自己动手想想)

边长为3的正方形为个数(n-2)*(m-2)

边长为min(n,m)的正方形为个数s1=(n-min(n,m)+1)*(m-min(n,m)+1)

然后从边长为1到min(m,m)的正方形个数全部加起来;

长方形:(包括正方形,好像正方形属于长方形来着?)

长为1的长方形(包括正方形)有n个

长为2的长方形(包括正方形)有n-1个

长为n的长方形(包括正方形)有1个

长为1到n的长方形1+2+...+n个

同理 宽为1的长方形(包括正方形)有m个

宽为2的长方形(包括正方形)有m-1个

宽为m的长方形(包括正方形)有1个

宽为1-m的长方形1+2+...+m个

然后把它们乘起来,根据乘法原理,总数s2=((1+n)(1+m)n*m)/4;

题目要求的是“非正方形的长方形”,因此要减去s1;

P1554

没什么好说的,第一次用的是循环,结果超时了,足足4s多,好家伙
后来就直接用分支了

package com.LG.Base;

import java.util.Scanner;

public class P1554 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt(), b = in.nextInt();
		int c[] = new int[10];
		for (int i = a; i <= b; i++) {
			for (int j2 = 0; j2 < (i + "").length(); j2++) {
				if ((i + "").charAt(j2) == 0 + 48) {
					c[0]++;
				} else if ((i + "").charAt(j2) == 1 + 48) {
					c[1]++;
				} else if ((i + "").charAt(j2) == 2 + 48) {
					c[2]++;
				} else if ((i + "").charAt(j2) == 3 + 48) {
					c[3]++;
				} else if ((i + "").charAt(j2) == 4 + 48) {
					c[4]++;
				} else if ((i + "").charAt(j2) == 5 + 48) {
					c[5]++;
				} else if ((i + "").charAt(j2) == 6 + 48) {
					c[6]++;
				} else if ((i + "").charAt(j2) == 7 + 48) {
					c[7]++;
				} else if ((i + "").charAt(j2) == 8 + 48) {
					c[8]++;
				} else if ((i + "").charAt(j2) == 9 + 48) {
					c[9]++;
				}
			}
		}
		for (int i : c) {
			System.out.print(i + " ");
		}
		in.close();
	}

}

P1567

其实是可以不需要数组的

package com.LG.Base;

import java.util.Scanner;

public class P1554 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt(), b = in.nextInt();
		int c[] = new int[10];
		for (int i = a; i <= b; i++) {
			for (int j2 = 0; j2 < (i + "").length(); j2++) {
				if ((i + "").charAt(j2) == 0 + 48) {
					c[0]++;
				} else if ((i + "").charAt(j2) == 1 + 48) {
					c[1]++;
				} else if ((i + "").charAt(j2) == 2 + 48) {
					c[2]++;
				} else if ((i + "").charAt(j2) == 3 + 48) {
					c[3]++;
				} else if ((i + "").charAt(j2) == 4 + 48) {
					c[4]++;
				} else if ((i + "").charAt(j2) == 5 + 48) {
					c[5]++;
				} else if ((i + "").charAt(j2) == 6 + 48) {
					c[6]++;
				} else if ((i + "").charAt(j2) == 7 + 48) {
					c[7]++;
				} else if ((i + "").charAt(j2) == 8 + 48) {
					c[8]++;
				} else if ((i + "").charAt(j2) == 9 + 48) {
					c[9]++;
				}
			}
		}
		for (int i : c) {
			System.out.print(i + " ");
		}
		in.close();
	}

}

懒得说了。

posted on 2022-05-05 23:14  Hex酱  阅读(61)  评论(0)    收藏  举报

导航