首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

走迷宫编程兑现

2012-12-27 
走迷宫编程实现限于办公室网络封锁只能发布在此了,自从开了博客第一篇文章,请各位大牛小圣赐教。package co

走迷宫编程实现

限于办公室网络封锁只能发布在此了,自从开了博客第一篇文章,请各位大牛小圣赐教。

package com.gigcode.algorithm;import java.io.File;import java.io.FileWriter;import java.io.IOException;/** * 编程实现解迷宫,从2011入口进入,2012出口走出 * @author gig * */public class Maze {private String[] path = new String[100];private int step = 1;private int limit = 99;private int resultnum = 0;private File resultFile = new File("result.txt");private FileWriter fw;/** * add 7 * @param x the last step value */public void add(float x) {if (step == limit) {return;}// ifpath[step++] = " +7";divide(x + 7);multiple(x + 7);sub(x + 7);path[step--] = null;}// add/** * divide 2 * @param x the last step value */public void divide(float x) {if (step == limit) {return;}// ifpath[step++] = " /2";add(x / 2);multiple(x / 2);sub(x / 2);path[step--] = null;}// divide/** * multiple 3 * @param x the last step value */public void multiple(float x) {if (step == limit) {return;}// ifpath[step++] = " *3";if (x * 3 == 2012) {print();return;}add(x * 3);sub(x * 3);divide(x * 3);path[step--] = null;}// mulitple/** * sub 5 * @param x the last step value */public void sub(float x) {if (step == limit) {return;}// ifpath[step++] = " -5";if (x - 5 == 2012) {print();return;}//ifadd(x - 5);multiple(x - 5);divide(x - 5);path[step--] = null;}// sub/** * print result  */public void print() {StringBuffer result = new StringBuffer("2011");for (int i = 1; i < step; i++) {result.append(path[i]);}// forresult.append("=2012\n");try {fw.write(result.toString());} catch (IOException e) {e.printStackTrace();}resultnum++;}// print/** * slove the problem  * @param x */public void resolveMaze(float x) {try {if(!resultFile.exists()){resultFile.createNewFile();}fw = new FileWriter(resultFile);add(x);divide(x);fw.close();System.out.println("it has " + this.resultnum+" results");} catch (IOException e) {e.printStackTrace();}}// sloveMazepublic static void main(String[] args) {Maze maze = new Maze();maze.resolveMaze(2011);}// main}// MAZE

?

热点排行