题目2681:蓝桥杯2022年第十三届省赛真题-矩形拼接JAVA实现下的思考

先附上AC的代码,供大家参考,如果感兴趣的话,可以继续向下阅读
在这里插入图片描述

代码:

//package _2022年省赛Java大学C组真题;
 
import java.util.Scanner;
 
public class Main{
    /**f
     *分类考虑问题,以A,B,C为第一参考,然后各自的长、宽为第二参考
     *故需要考虑3 *  2*2*2种情况 
     * 
     */
    private static int T;
    private static int a1,a2,a3,b1,b2,b3;
    private static int res;;
    //private static int arr[] = new int [2];
     
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        T = sc.nextInt();
        for(int p=0;p<T;p++) {
            a1 = sc.nextInt();
            b1 = sc.nextInt();
            a2 = sc.nextInt();
            b2 = sc.nextInt();
            a3 = sc.nextInt();
            b3 = sc.nextInt();
            //----------
            res=1002;
            find_minside(); 
            int temp = a1;
            a1 = a2;
            a2 = temp;
            temp = b1;
            b1 = b2;
            b2 = temp;
            find_minside();
            temp = a1;
            a1 = a3;
            a3 = temp;
            temp = b1;
            b1 = b3;
            b3 = temp;
            find_minside();
//          //下面面是以ABC顺序比较矩阵进行的
//          find_minside();
//          swap(a1,a2);
//          swap(b1,b2);
//          //下面面是以BAC顺序比较矩阵进行的
//          find_minside();
//          swap(a2,a3);
//          swap(b2,b3);
//          //下面面是以BCA顺序比较矩阵进行的
//          find_minside();
//          swap(a1,a3);
//          swap(b1,b3);
//          //下面面是以ACB顺序比较矩阵进行的
//          find_minside();
//          swap(a1,a2);
//          swap(b1,b2);
//          //下面面是以CAB顺序比较矩阵进行的
//          find_minside();
//          swap(a3,a2);
//          swap(b3,b2);
//          //下面面是以CBA顺序比较矩阵进行的
//          find_minside();
            System.out.println(res);
        }
    }
 
    private static void swap(int arr[],int x, int y ) {
        // TODO Auto-generated method stub
        int temp = arr[x];
        arr[x] = arr[y];
        arr[y] = temp;
    }
 
    private static void find_minside() {
        // TODO Auto-generated method stub
            res =  Math.min(res, thisway_minside(a1, b1, a2, b2, a3, b3));
            res =  Math.min(res, thisway_minside(b1, a1, a2, b2, a3, b3));
            res =  Math.min(res, thisway_minside(a1, b1, b2, a2, a3, b3));
            res =  Math.min(res, thisway_minside(a1, b1, a2, b2, b3, a3));
            res =  Math.min(res, thisway_minside(b1, a1, b2, a2, a3, b3));
            res =  Math.min(res, thisway_minside(b1, a1, a2, b2, b3, a3));
            res =  Math.min(res, thisway_minside(a1, b1, b2, a2, b3, a3));
            res =  Math.min(res, thisway_minside(b1, a1, b2, a2, b3, a3));
    }
 
    private static int thisway_minside(int a1, int b1, int a2, int b2, int a3, int b3) {
        // TODO Auto-generated method stub
        res = 4;//最好情况就是4
        //最坏情况则是8
        if(a1 == a2 && a1== a3) {
            return res; 
        }
        if(a1 != a2+a3) {
            res+=2;
        }
        if(b2 != b3) {
            res+=2;
        }
        return res;
    }
 
}

一开始做的时候,忘得东西太多了,来来回回零散时间做了好几遍,哎,太菜了

接下来说一下我的思考:

1.混分盲猜

1.当时通过观察规律发现答案应该会锁定在4-8之间,尤其是4,6, 8这三个数,因此打算看看直接输入他们会得到什么样的结果,但是却和我想的20-30%大相径庭
在这里插入图片描述
如图,实际下来只有4能得10分,其他的8可以得5分 , 5,6,7这三个数则会得到0分的结果,我个人感觉是挺诧异的。

2.通过穷举得到结果:

来自这位大佬:来自澳大利亚的兵 

package dotcpp.模拟;

import java.util.Scanner;

public class 矩阵拼接 {
   public static void main(String[] args) {
       Scanner scanner=new Scanner(System.in);
       int n=scanner.nextInt();
       while (n>0){
           n--;
           int a1=scanner.nextInt();
           int b1=scanner.nextInt();
           int a2=scanner.nextInt();
           int b2=scanner.nextInt();
           int a3=scanner.nextInt();
           int b3=scanner.nextInt();
           if(a1==a2){
               int side2=b1+b2;
               if(a1 ==a3|| a1 ==b3||side2==a3||side2==b3)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }
          else if(a1==b2){
               int side2=b1+a2;
               if(a1 ==a3|| a1 ==b3||side2==a3||side2==b3)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }
          else if(b1==a2){
               int side2=b2+a1;
               if(a2 ==a3|| a2 ==b3||side2==a3||side2==b3)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }
         else  if(b1==b2){
               int side2=a1+a2;
               if(b1 ==a3|| b1 ==b3||side2==a3||side2==b3)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }

         else  if(a2==a3){
               int side2=b3+b2;
               if(side2 == a1 || side2 == b1)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }
          else if(a2==b3){
               int side2=b2+a3;
               if(side2 == a1 || side2 == b1)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }
          else if(b2==a3){
               int side2=a2+b3;
               if(side2 == a1 || side2 == b1)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }
         else if(b2==b3){
               int side2=a2+a3;
               if(side2 == a1 || side2 == b1)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }

          else if(a1==a3){
               int side2=b1+b3;
               if(side2 == a2 || side2 == b2)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }
         else   if(a1==b3){
               int side2=b1+a3;
               if(side2 == a2 || side2 == b2)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }
         else   if(b1==a3){
               int side2=b3+a1;
               if(side2 == a2 || side2 == b2)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }
        else if(b1==b3){
               int side2=a1+a3;
               if(side2 == a2 || side2 == b2)
                   System.out.println(4);
               else
                   System.out.println(6);
               continue;
           }
         int side1=a1+a2;int side2=a3+a2;int side3=a1+a3;
         int side4=b1+b2;int side5=b3+b2;int side6=b1+b3;
         int side7=b1+a2;int side8=b3+a2;int side9=b1+a3;
         int side10=a1+b2;int side11=a3+b2;int side12=a1+b3;
          boolean bool1= a3==side1||b3==side1||side2==a1||side2==b1||side3==a2||side3==b2;
          boolean bool2= a3==side4||b3==side4||side5==a1||side5==b1||side6==a2||side6==b2;
          boolean bool3= a3==side7||b3==side7||side8==a1||side8==b1||side9==a2||side9==b2;
          boolean bool4= a3==side10||b3==side10||side11==a1||side11==b1||side12==a2||side12==b2;
          if(bool1||bool2||bool3||bool4)
              System.out.println(6);
          else
              System.out.println(8);
       }
   }
}

如图所示,他通过穷举所有情况,得到最终的结果也集中在4,6,8,由于我没有充值会员,看不到测试样例和测试结果,因此我感到十分奇怪,对我的盲猜法答案。

3.实践中的奇怪问题

在这里插入图片描述

我的疑问

如图所示,当我把res=1002,放在static里面时,则输出的结果全是4,不清楚是为什么 = =~
可能是我的java忘得有点多,不清楚为什么,输出全是4,然后也AC不了,

  • 我就想知道为什么我不能在static 那里默认初始化的值,然后在main方法里直接调用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值