一个菜鸟的java求字谜算法
来源:互联网 发布:淘宝网店推广目标 编辑:程序博客网 时间:2024/05/01 23:07
《数据结构与算法分析》的一个课后习题
我自己写的代码如下。。。
感觉效率也太低了。。。
有大神能指导指导吗 萌新路过
呃呃。。这是原题
编写一个程序求解字谜游戏问题
12341this2wats3oahg4fgdtpublic class test02 {
static String str [][] = {{"t","h","i","s"},{"w","a","t","s"},{"o","a","h","g"},{"f","g","d","t"}};
public static void main(String args[]){
int index[] = jiemi("this");
jiemi01("this",index[0],index[1]);
jiemi01("this",index[2],index[3]);
}
public static int[] jiemi(String s){
int index[] = {0,0,0,0,0,0};
int num = 0;
char c[] = s.toCharArray();
String first = c[0]+"";
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
if(first.equals(str[i][j])){
index[num++] = i ;
index[num++] = j;
}
}
}
int t1 = index[0];
int t2 = index[1];
int t3 = index[2];
int t4 = index[3];
return index;
}
public static void jiemi01(String s,int a,int b){
String temp = "";
for(int j = b;j>=0;j--){
temp+=str[a][j];
}
isornot(temp,s);
temp = "";
for(int i = a;i>=0;i--){
temp+=str[i][b];
}
isornot(temp,s);
temp="";
for(int j = b;j<=3;j++){
temp+=str[a][j];
}
isornot(temp,s);
temp = "";
for(int i = a;i<=3;i++){
temp+=str[i][b];
}
isornot(temp,s);
temp = "";
int min = min(a,b);
int max = max(a,b);
int t = b;
for(int i = min;i>=0;i--){
temp+=str[i][t--];
}
isornot(temp,s);
temp = "";
t = a;
for(int i = max;i<=3;i++){
temp+=str[t--][i];
}
temp = "";
t = a;
for(int i = max;i<=3;i++){
temp+=str[t++][i];
}
temp = "";
t = a;
for(int i = max;i>=0;i--){
temp+=str[t++][i];
}
isornot(temp,s);
}
public static void isornot(String temp,String s){
if(temp.equals(s)){
System.out.println(temp+"是字谜");
System.exit(0);
}
}
public static int min(int a,int b){
if(a<b){
return a;
}
if(a>b){
return b;
}
else{
return a;
}
}
public static int max(int a,int b){
if(a<b){
return b;
}
if(a>b){
return a;
}
else{
return a;
}
}
}
0 0
- 一个菜鸟的java求字谜算法
- 英语字谜求解程序(JAVA):iMessage-GamePigeon的“字谜”
- java算法求一个字符串的倒序
- 一个关于单词的字谜问题
- 数据结构与算法书中,字谜游戏java实现
- 一个求两字串最长子序列的java算法
- 求一个整数的组合数算法(java)
- java:求一个字符串中子字符串的个数,简便算法
- 纵横字谜的答案
- 纵横字谜的答案
- 纵横字谜的答案
- JAVA菜鸟关于接口的一个问题
- 一个Java菜鸟的学习之道~~~
- 一个java菜鸟的求职辛酸史
- 一个Java菜鸟的自我见解
- Java菜鸟编程 一个有趣的练习
- 字谜
- 字谜
- spring maven 搭建dubbo框架 消费端
- 让Linux看起来cool
- Python使用pytesseract库识别验证码(Mac)
- python之pandas学习
- Java静态绑定与动态绑定
- 一个菜鸟的java求字谜算法
- Android开发之Retrofit+RxJava的使用
- 1048. 数字加密(20)
- 开始了解
- Android StackView 堆叠视图
- 十四、成为一个Java的架构师要学习哪些知识?
- angular cli安装
- Java高级编程-NIO
- 读输入流的字节,打印显示