每日一题(小白)暴力娱乐篇24

由题已知这是一个匹配题目,题目已经说了三阶幻方是给定的,经过镜像和旋转,镜像*2旋转*4;

总共八种方案,然后接收每次的数据去匹配(跳过0),如果匹配就输出匹配的数组,如果不匹配就输出-1。

题目给出了:492357816 旋转和镜像可以得到"672159834", "276951438", "294753618", "834159672", "438951276", "618753294", "816357492"

①存储8个固定字符串

②以字符串的形式接收给定数组

③进行比对,若为0则跳过,若不对应记录0,若对应记录下标

④输出结果,没找到ToMany;找到输出对应下标字符串

代码如下👇

复制代码
public static void main(String[] args) {
			Scanner scan=new Scanner(System.in);
		    String[] s1= {
		    		"672159834", "276951438", "294753618","492357816",
		    		"834159672", "438951276", "618753294", "816357492"
		    };
		    String s2="";
		    for (int i = 0; i < 3; i++) {
				s2+=scan.nextLine();
			}
		    s2=s2.replaceAll(" ", "");
		    System.out.println(s2);//测试点
		    int count=0;
		    //System.out.println(s2.charAt(1)!='0' && s1[0].charAt(1)==s2.charAt(1));测试
		    for (int i = 0; i < s1.length; i++) {
				for (int j = 0; j < 9; j++) {
					if (s2.charAt(j)!='0' && s1[i].charAt(j)==s2.charAt(j)) {
						count++;
					}
					if (s2.charAt(j)!='0' && s1[i].charAt(j)!=s2.charAt(j)) {
						count=0;
					     break;
					}
				}
				if (count>0) {
					count=i;
					break;
				}
			}
		    if (count==0) {//本来判断赋值为-1.这里省略
				System.out.println("Too Many");
			}else {
				for (int i = 0; i < 9; i++) {
					System.out.print(s1[count].charAt(i)+" ");
					if (i==2) {
						System.out.println();
					}
					if (i==5) {
						System.out.println();
					}
					if (i==8) {
						System.out.println();
					}
				}
			}
		    
			scan.close();
    }

进行优化👇

复制代码
	public static void main(String[] args) {
			Scanner scan=new Scanner(System.in);
		    String[] s1= {
		    		"672159834", "276951438", "294753618","492357816",
		    		"834159672", "438951276", "618753294", "816357492"
		    };
		    String s2="";
		    for (int i = 0; i < 3; i++) {
				s2+=scan.nextLine();
			}
		    s2=s2.replaceAll(" ", "");
		    int count=-1;//寄存下标或答案
		    for (int i = 0; i < 8; i++) {
				if (compares(s2, s1[i])) {//比较成功进行赋值
					count=i;
					break;
				} 
			}
		    if (count==-1) {//比较失败输出  太多
				System.out.println("Too Many");
			}else {
				prints(s1, count);
			}
			scan.close();
    }
	public static boolean compares(String s1,String s2) {//比较
		for (int i = 0; i < 9; i++) {
			if (s1.charAt(i)!='0') {
				if (s1.charAt(i)!=s2.charAt(i)) {
					return false;
				}
			}
		}
		return true;
	}
	public static void prints(String[] s,int x) {//打印
		for (int i = 0; i < 9; i++) {
			System.out.print(s[x].charAt(i)+" ");
			if (i==2) {
				System.out.println();
			}
			if (i==5) {
				System.out.println();
			}
			if (i==8) {
				System.out.println();
			}
		}
	}
相关推荐
偷吃的耗子6 分钟前
【CNN算法理解】:CNN平移不变性详解:数学原理与实例
人工智能·算法·cnn
dazzle1 小时前
机器学习算法原理与实践-入门(三):使用数学方法实现KNN
人工智能·算法·机器学习
那个村的李富贵1 小时前
智能炼金术:CANN加速的新材料AI设计系统
人工智能·算法·aigc·cann
张张努力变强1 小时前
C++ STL string 类:常用接口 + auto + 范围 for全攻略,字符串操作效率拉满
开发语言·数据结构·c++·算法·stl
万岳科技系统开发1 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
张登杰踩1 小时前
MCR ALS 多元曲线分辨算法详解
算法
YuTaoShao2 小时前
【LeetCode 每日一题】3634. 使数组平衡的最少移除数目——(解法一)排序+滑动窗口
算法·leetcode·排序算法
波波0072 小时前
每日一题:.NET 的 GC是如何分代工作的?
算法·.net·gc
风暴之零2 小时前
变点检测算法PELT
算法
深鱼~2 小时前
视觉算法性能翻倍:ops-cv经典算子的昇腾适配指南
算法·cann