class a { public static void main(String[] args) { a a=new a();
System.out.println("Hello World!="+a.test(4)); }
public int test(int i){ int s=0; if (i==1) { return 1; } return i*test(i-1); } } 这是一个简单的第归的例子,希望对你有帮助。
小菜一蝶,注:除法使用四舍五入,如果和你要求的不一样,改Math.round为其他函数即可 /* By Daniel Woo you can contact me: DanielYWoo.hotmail.com*/ import java.util.Random; public class NumPuzzle{ private int n=6; private int MAX = 100; private int num[] = new int[n]; Random rand = new Random(); public static final int OP_PLUS = 1; public static final int OP_MINUS = 2; public static final int OP_MULTI = 3; public static final int OP_DIVIDE = 4; public static void main(String args[]) { NumPuzzle np = new NumPuzzle(); np.init(); np.resolve(0, 0, 0); }
public void init() { int i; for(i=0; i<n; i++){ num[i]=(int)(rand.nextFloat()*100); System.out.print(" " + num[i]); } } public boolean resolve(int operation, int value, int step) { //System.out.print("\nDebug: \t" + step + "\t" + operation +"\t" + value ); if(step!=0){//if it's first step, skip these codes if(step>=n) //reach end of num[] if(value==24){ printOP(-1, value, step); return true;//found }else return false;//not found
switch(operation){ case OP_PLUS: value += num[step]; break; case OP_MINUS: value -= num[step]; break; case OP_MULTI: value *= num[step]; break; case OP_DIVIDE: if(num[step]==0) {step--; return false;} value = Math.round(value/num[step]); break; } }else{//first step, so set initial value value = num[0]; }
晚上刚吃完饭,手痒了,给你改改吧 /* By Daniel Woo you can contact me: DanielYWoo.hotmail.com*/ import java.util.Random; import java.util.Stack;public class NumPuzzle{ private static int n=6; private int MAX = 100; private int num[] = new int[n]; private Random rand = new Random(); private static Stack stk = new Stack(); public static final int OP_PLUS = 1; public static final int OP_MINUS = 2; public static final int OP_MULTI = 3; public static final int OP_DIVIDE = 4; public static void main(String args[]) { NumPuzzle np = new NumPuzzle(); np.init(); if(np.resolve(0, 0, 0)) outputResult(); } public static void outputResult() { System.out.println("\nFound!"); for(int i=0; i<=n; i++) System.out.print(stk.pop()); } public void init() { int i; for(i=0; i<n; i++){ num[i]=(int)(rand.nextFloat()*100); System.out.print(" " + num[i]); } } public boolean resolve(int operation, int value, int step) { if(step!=0){//if it's first step, skip these codes if(step>=n) //reach end of num[] if(value==24){ pushElement(-1, value, step); return true;//found }else return false;//not found
switch(operation){ case OP_PLUS: value += num[step]; break; case OP_MINUS: value -= num[step]; break; case OP_MULTI: value *= num[step]; break; case OP_DIVIDE: if(num[step]==0) {step--; return false;} value = Math.round(value/num[step]); break; } }else{//first step, so set initial value value = num[0]; }
{
public static void main(String[] args)
{
a a=new a();
System.out.println("Hello World!="+a.test(4));
}
public int test(int i){
int s=0;
if (i==1)
{
return 1;
}
return i*test(i-1);
}
}
这是一个简单的第归的例子,希望对你有帮助。
/* By Daniel Woo you can contact me: DanielYWoo.hotmail.com*/
import java.util.Random;
public class NumPuzzle{
private int n=6;
private int MAX = 100;
private int num[] = new int[n];
Random rand = new Random();
public static final int OP_PLUS = 1;
public static final int OP_MINUS = 2;
public static final int OP_MULTI = 3;
public static final int OP_DIVIDE = 4;
public static void main(String args[])
{
NumPuzzle np = new NumPuzzle();
np.init();
np.resolve(0, 0, 0);
}
public void init()
{
int i;
for(i=0; i<n; i++){
num[i]=(int)(rand.nextFloat()*100);
System.out.print(" " + num[i]);
}
}
public boolean resolve(int operation, int value, int step)
{
//System.out.print("\nDebug: \t" + step + "\t" + operation +"\t" + value );
if(step!=0){//if it's first step, skip these codes
if(step>=n) //reach end of num[]
if(value==24){
printOP(-1, value, step);
return true;//found
}else
return false;//not found
switch(operation){
case OP_PLUS:
value += num[step];
break;
case OP_MINUS:
value -= num[step];
break;
case OP_MULTI:
value *= num[step];
break;
case OP_DIVIDE:
if(num[step]==0) {step--; return false;}
value = Math.round(value/num[step]);
break;
}
}else{//first step, so set initial value
value = num[0];
}
step++;
if(resolve(OP_PLUS, value, step)) {printOP(OP_PLUS, value, step); step--; return true;}
if(resolve(OP_MINUS, value, step)) {printOP(OP_MINUS, value, step); step--; return true;}
if(resolve(OP_MULTI, value, step)) {printOP(OP_MULTI, value, step); step--; return true;}
if(resolve(OP_DIVIDE, value, step)) {printOP(OP_DIVIDE, value, step); step--; return true;}
step--;
return false;
}
public void printOP(int i, int value, int step)
{ step--;
switch(i){
case OP_PLUS:System.out.print((step==n-1 ? " = " : " + ") + num[step]);break;
case OP_MINUS:System.out.print((step==n-1 ? " = " : " - ") + num[step]);break;
case OP_MULTI:System.out.print((step==n-1 ? " = " : " * ") + num[step]);break;
case OP_DIVIDE:System.out.print((step==n-1 ? " = " : " / ") + num[step]);break;
default:System.out.print(" \nFound! " + value);break;
}
}
}
/* By Daniel Woo you can contact me: DanielYWoo.hotmail.com*/
import java.util.Random;
import java.util.Stack;public class NumPuzzle{
private static int n=6;
private int MAX = 100;
private int num[] = new int[n];
private Random rand = new Random();
private static Stack stk = new Stack();
public static final int OP_PLUS = 1;
public static final int OP_MINUS = 2;
public static final int OP_MULTI = 3;
public static final int OP_DIVIDE = 4; public static void main(String args[])
{
NumPuzzle np = new NumPuzzle();
np.init();
if(np.resolve(0, 0, 0))
outputResult();
}
public static void outputResult()
{
System.out.println("\nFound!");
for(int i=0; i<=n; i++)
System.out.print(stk.pop());
}
public void init()
{
int i;
for(i=0; i<n; i++){
num[i]=(int)(rand.nextFloat()*100);
System.out.print(" " + num[i]);
}
}
public boolean resolve(int operation, int value, int step)
{
if(step!=0){//if it's first step, skip these codes
if(step>=n) //reach end of num[]
if(value==24){
pushElement(-1, value, step);
return true;//found
}else
return false;//not found
switch(operation){
case OP_PLUS:
value += num[step];
break;
case OP_MINUS:
value -= num[step];
break;
case OP_MULTI:
value *= num[step];
break;
case OP_DIVIDE:
if(num[step]==0) {step--; return false;}
value = Math.round(value/num[step]);
break;
}
}else{//first step, so set initial value
value = num[0];
}
step++;
if(resolve(OP_PLUS, value, step)) {pushElement(OP_PLUS, value, step); step--; return true;}
if(resolve(OP_MINUS, value, step)) {pushElement(OP_MINUS, value, step); step--; return true;}
if(resolve(OP_MULTI, value, step)) {pushElement(OP_MULTI, value, step); step--; return true;}
if(resolve(OP_DIVIDE, value, step)) {pushElement(OP_DIVIDE, value, step); step--; return true;}
step--;
return false;
}
public void pushElement(int i, int value, int step)
{ step--;
switch(i){
case OP_PLUS:stk.push("" + num[step] + (step==n-1 ? " = " : " + "));break;
case OP_MINUS:stk.push("" + num[step] + (step==n-1 ? " = " : " - "));break;
case OP_MULTI:stk.push("" + num[step] + (step==n-1 ? " = " : " * "));break;
case OP_DIVIDE:stk.push("" + num[step] + (step==n-1 ? " = " : " / "));break;
default:stk.push(" " + value);break;
}
}
}
结果里没有加小括号,按自左至右顺序运算,如 2 + 3 /1 其实为 ( 2 + 3 )/1
之后更改为4个数字的,得到的一组数据:
4 39 16 26
Found!
4 - 39 / 16 + 26 = 24
这个结果有点莫名。