前言
本来以为可以纯暴力解决这一类题目,没想到还混杂了大量的dp..花了几天断断续续的写完了。。
P2241 统计方形(数据加强版)
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
Scanner <u>scanner</u> = new Scanner(System.in);
long m = scanner.nextLong(), n = scanner.nextLong();//这里必须定义long防止溢出
long rectangle = (m*(m+1)/2)*(n*(n+1)/2), square = 0;//所有的四边形
for (long i = 1; i <= Math.min(m, n); i++) {
square += (n-i+1)*(m-i+1);//而对于一个边长为 i (1 ≤ i ≤ <u>min</u>{n, m}) 的正方形,数量为:(n-i+1)(m-i+1)
}
System.out.println(square + " " + (rectangle-square));
}
}
烤鸡
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int count = 0;
int a,b,c,d,e,f,g,h,i,j;
for (a = 1; a <= 3; a++) {
for (b = 1; b <= 3; b++) {
for (c = 1; c <= 3; c++) {
for (d = 1; d <= 3; d++) {
for (e = 1; e <= 3; e++) {
for (f = 1; f <= 3; f++) {
for (g = 1; g <= 3; g++) {
for (h = 1; h <= 3;
h++) {
for (i = 1; i
<= 3; i++) {
for (j
= 1; j <= 3; j++) {
if(a+b+c+d+e+f+g+h+i+j==n) {
count++;
}
}
}
}
}
}
}
}
}
}
}
System.out.println(count);
for (a = 1; a <= 3; a++) {
for (b = 1; b <= 3; b++) {
for (c = 1; c <= 3; c++) {
for (d = 1; d <= 3; d++) {
for (e = 1; e <= 3; e++) {
for (f = 1; f <= 3; f++) {
for (g = 1; g <= 3; g++) {
for (h = 1; h <= 3;
h++) {
for (i = 1; i
<= 3; i++) {
for (j
= 1; j <= 3; j++) {
if(a+b+c+d+e+f+g+h+i+j==n) {
System.out.println(a+" "+b+" "+c+" "+d+" "+e+" "+f+" "+g+" "+h+" "+i+"
"+j);
}
}
}
}
}
}
}
}
}
}
}
scanner.close();
}
}
P1618 三连击(升级版)
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
Scanner <u>sc</u>=new Scanner(System.in);
int m1=sc.nextInt();
int m2=sc.nextInt();
int m3=sc.nextInt();
int f=0;
for(int a=123;a<999;a++){
int b=a/m1*m2;
int c=a/m1*m3;
if(b>=999||c>=999)break;
String a1=String.valueOf(a);
String b1=String.valueOf(b);
String c1=String.valueOf(c);
if(a>b||a>c||b>c)break;
if(pd(a1,b1,c1)){
if(hj(a,b,c).equals(hj(m1,m2,m3))){
f=1;
System.out.println(a+" "+b+" "+c);
}
}
}
if(f==0){
System.out.println("No!!!");
}
}
private static String hj(int a, int b, int c) {
// TODO Auto-generated method stub
for(int i=a;i>=2;i--){
if(a%i==0&&b%i==0&&c%i==0){
a=a/i;
b=b/i;
c=c/i;
}
}
return String.valueOf(a)+String.valueOf(b)+String.valueOf(c);
}
private static boolean pd(String a1, String b1, String c1) {
// TODO Auto-generated method stub
String s=a1+b1+c1;
for(char i='1';i<='9';i++){
if(s.contains(String.valueOf(i))==false){
return false;
}
}
return true;
}
}
P1036 [NOIP2002 普及组] 选数
import java.util.*;
public class Test04 {
public static int n,k;
public static int res;
public static int[] a;
public static boolean judge(int num){
for(int i=2;i<=Math.sqrt(num);i++){
if(num%i==0)
return false;
}
return true;
}
public static void dfs(ArrayList<Integer> arr,int cur){
if(arr.size() == k){
int sum = 0;
for(int i=0;i<arr.size();i++)
sum += arr.get(i);
if(judge(sum))
res++;
return;
}
if(cur == n) return;
for(int i=cur;i<n;i++){
arr.add(a[i]);
dfs(arr,i+1);
arr.remove(arr.size()-1);
}
}
public static void main(String[] args){
Scanner <u>sc</u> = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
a = new int[n];
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
res = 0;
ArrayList<Integer> arr = new ArrayList<>();
for(int i=0;i<n;i++){
arr.add(a[i]);
dfs(arr,i+1);
arr.remove(arr.size()-1);
}
System.out.println(res);
}
}
全排列问题
import java.util.Scanner;
public class Test06 {
public static StringBuilder result = new StringBuilder();//使用append操作
public static int num;
public static int[] ans;//保存当前的方案
public static boolean[] used;////表示每个数是否被用过
public static void dfs(int k) {
if (k > num) {
for (int j = 1; j <= num; j++) {
result.append(" ").append(ans[j]);//使用空格来控制场宽
}
result.append('
');//换行
return;
}
for (int i = 1; i <= num; i++) {//从小到大枚举
if (!used[i]) {//判断这个数是否用过
used[i] = true;//标记这个数被使用了
ans[k] = i;////保存到方案中
dfs(k+1);//进行下一步搜索
used[i] = false;////撤销标记回溯
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
num = scanner.nextInt();
scanner.close();
ans = new int[num+1];//从1开始
used = new boolean[num+1];
dfs(1);
System.out.print(result);
}
}
涂国旗
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] line1 = reader.readLine().split("\s+");
int n = Integer.parseInt(line1[0]), m = Integer.parseInt(line1[1]);
char[][] graph = new char[n][m];
for (int i = 0; i < n; i++) {
char[] read_temp = reader.readLine().toCharArray();
for (int j = 0; j < m; j++) {
graph[i][j] = read_temp[j];
}
}
reader.close();
int min_result = 0x7fffffff, temp_result = 0;
//确定白行数
for (int i = 0; i < n-2; i++) {
//确定蓝行数
for (int j = i+1; j < n-1; j++) {
//循环白
for (int k = 0; k <= i; k++) {
for (int p = 0; p < m; p++) {
if (graph[k][p] != 'W') {
temp_result++;
}
}
}
//循环蓝
for (int k = i+1; k <= j; k++) {
for (int p = 0; p < m; p++) {
if (graph[k][p] != 'B') {
temp_result++;
}
}
}
//循环红
for (int k = j+1; k < n; k++) {
for (int p = 0; p < m; p++) {
if (graph[k][p] != 'R') {
temp_result++;
}
}
}
if (temp_result < min_result) {
min_result = temp_result;
}
temp_result = 0;
}
}
System.out.println(min_result);
}
}