输入如下不等式: 
例:3+6=3 
动一根木棒, 若等式成立,返回true,不成立返回false 提示:1式子中无负数 
      2输入的数字 在0-9之间 
      3式子只进行加减运算,等号不能移动 
      4数字的构成图如下 (没法贴图呀,说一下吧,就如计算器中的数字显示效果,例如:1由两根棒构成,2由5根棒构成,0由6根棒构成...) 如:将“+”中的一更棒移动到3 上 ,就变成了 9-6=3 ; 要求用c编程 
用java也行吧,反正是算法多一点,请各位大侠指点。 

解决方案 »

  1.   

    搞java的刚开始学习时对算法要求不高
      

  2.   

    搞java的刚开始学习时对算法要求不高
      

  3.   

    算法不难,但考虑的东西太多.import java.util.*;
    /** transformation是一个Map<Character,char[][]>,存放每一种字符对应的变换.
      * char[][]是一个长度为3的二维数组,char[0]是自身变换,char[1]是减一根火柴的变换,char[2]是加一根火柴对就的变换.
      * 比如对于'6',对应的char[][]是{ {'0','9'}, {'5'}, {'8'}},表示自身可以变为'0','9',减一根可以变为'5',加一根可一变为'5','8'
      *
      * symbolPattern<Character,char[][]>,存放每一种字符对应的火柴摆放图案.
    */
    public class MatchMove{
    public MatchMove(){
    transformation.put('0',new char[][]{ {'6','9'},  null,          {'8'}});
    transformation.put('1',new char[][]{ null,       null,          {'7'}});
    transformation.put('2',new char[][]{ {'3'},      null,          {'6','9'}});
    transformation.put('3',new char[][]{ {'2'},      null,          {'9'}});
    transformation.put('4',new char[][]{ null,       null,          null});
    transformation.put('5',new char[][]{ {'3'},      null,          {'6','9'}});
    transformation.put('6',new char[][]{ {'0','9'},  {'5'},         {'8'}});
    transformation.put('7',new char[][]{ null,       {'1'},         null});
    transformation.put('8',new char[][]{ null,       {'0','6','9'}, null});
    transformation.put('9',new char[][]{ {'0','6'},  {'3','5'},     {'8'}});
    transformation.put('+',new char[][]{ null,       {'-'},         null});
    transformation.put('-',new char[][]{ null,       null,          {'+'}});
    transformation.put('=',new char[][]{ null,       null,          null});

    symbolPattern.put('0',new char[][]{{' ','-',' '},
                                   {'|',' ','|'},
                                   {' ',' ',' '},
                                   {'|',' ','|'},
                                   {' ','-',' '}
                                  });
    symbolPattern.put('1',new char[][]{{' ',' ',' '},
                                   {' ',' ','|'},
                                   {' ',' ',' '},
                                   {' ',' ','|'},
                                   {' ',' ',' '}
                                  });
    symbolPattern.put('2',new char[][]{{' ','-',' '},
                                   {' ',' ','|'},
                                   {' ','-',' '},
                                   {'|',' ',' '},
                                   {' ','-',' '}
                                  });
    symbolPattern.put('3',new char[][]{{' ','-',' '},
                                   {' ',' ','|'},
                                   {' ','-',' '},
                                   {' ',' ','|'},
                                   {' ','-',' '}
                                  });
    symbolPattern.put('4',new char[][]{{' ',' ',' '},
                                   {'|',' ','|'},
                                   {' ','-',' '},
                                   {' ',' ','|'},
                                   {' ',' ',' '}
                                  });
    symbolPattern.put('5',new char[][]{{' ','-',' '},
                                   {'|',' ',' '},
                                   {' ','-',' '},
                                   {' ',' ','|'},
                                   {' ','-',' '}
                                  });
    symbolPattern.put('6',new char[][]{{' ','-',' '},
                                   {'|',' ',' '},
                                   {' ','-',' '},
                                   {'|',' ','|'},
                                   {' ','-',' '}
                                  });
    symbolPattern.put('7',new char[][]{{' ','-',' '},
                                   {' ',' ','|'},
                                   {' ',' ',' '},
                                   {' ',' ','|'},
                                   {' ',' ',' '}
                                  });
    symbolPattern.put('8',new char[][]{{' ','-',' '},
                                   {'|',' ','|'},
                                   {' ','-',' '},
                                   {'|',' ','|'},
                                   {' ','-',' '}
                                  });
    symbolPattern.put('9',new char[][]{{' ','-',' '},
                                   {'|',' ','|'},
                                   {' ','-',' '},
                                   {' ',' ','|'},
                                   {' ','-',' '}
                                  });
    symbolPattern.put('-',new char[][]{{' ',' ',' '},
                                   {' ',' ',' '},
                                   {' ','-',' '},
                                   {' ',' ',' '},
                                   {' ',' ',' '}
                                  });
    symbolPattern.put('+',new char[][]{{' ',' ',' '},
                                   {' ',' ',' '},
                                   {' ','+',' '},
                                   {' ',' ',' '},
                                   {' ',' ',' '}
                                  });
    symbolPattern.put('=',new char[][]{{' ',' ',' '},
                                   {' ',' ',' '},
                                   {' ','=',' '},
                                   {' ',' ',' '},
                                   {' ',' ',' '}
                                  });
    }
    /**把str表示的等式通过移动火柴,使等式成立.
      
    */
    public void move(String str){
    if(str==null||str.trim().length()==0){
    throw new RuntimeException("字符串为空!");
    }

    String regex="[0-9]+[+-][0-9]+=[0-9]+";
    if(!str.matches(regex)){
    throw new RuntimeException("给定的字符串格式不对");
    }

    char[] chars=str.toCharArray();
    if(isEquation(chars)){
    System.out.println("所给式子本身就是等式");
    }

    for(int i=0;i<chars.length;i++){
    char c=chars[i];               //
    char[][] tran=transformation.get(c);    //取出关于c字符的所有变换方案.
    if(tran[0]!=null){            //字符本身的自变换.
    for(char temp : tran[0]){
    chars[i]=temp;        //自变换的结果替换掉原来的字符.
    if(isEquation(chars)){
    System.out.println("结果:");
    printPattern(chars);
    }
    chars[i]=c;           //复原回到原来的状态.
    }
    }
    if(tran[1]!=null){            //减一根火柴的变换.
    for(char temp :tran[1]){
    chars[i]=temp;        //减一根火柴后的变换结果替换掉原来的字符.
    //下面的循环试着把火柴加到每一个除chars[i]之外的字符之上,看等式成立否.
    for(int j=0;j<chars.length;j++){
    if(i==j){
    continue;
    }
    char c2=chars[j];
    char[][] tran2=transformation.get(c2);
    if(tran2[2]!=null){
    for(char temp2 : tran2[2]){
    chars[j]=temp2;         //chars[j]加一根火柴.
    if(isEquation(chars)){
    System.out.println("结果");
    printPattern(chars);
    }
    chars[j]=c2;         //复原chars[j].
    }
    }

    }
    chars[i]=c;                     //复原chars[i].
    }

    }
    }
    return;
    }
        //测试:
        
    public static void main(String[] args){
    MatchMove mm=new MatchMove();
    System.out.println("测试一");
    System.out.println("原等式如下:");
    mm.printPattern("3+6=3");
    mm.move("3+6=3");
    System.out.println("测试二");
    System.out.println("原等式如下:");
    mm.printPattern("12+3=5");
    mm.move("12+3=5");
    System.out.println("测试三");
    System.out.println("原等式如下:");
    mm.printPattern("90+12=69");
    mm.move("90+12=69");
    }
        public void printPattern(String str){
         printPattern(str.toCharArray());
        }
    private  Map<Character,char[][]> transformation=new HashMap<Character,char[][]>();
    private  Map<Character,char[][]> symbolPattern=new HashMap<Character,char[][]>();

    /**判断equation字符数组中存放的等式能不能成立.
    */
    private boolean isEquation(char[] equation){
    int num1=0;
    int num2=0;
    int num3=0;
    char operator;
    int index=0;
    for(;Character.isDigit(equation[index]);index++){
    num1=num1*10+equation[index]-'0';
    }
    operator=equation[index++];
    for(;Character.isDigit(equation[index]);index++){
    num2=num2*10+equation[index]-'0';
    }
    index++;
    for(;index<equation.length;index++){
    num3=num3*10+equation[index]-'0';
    }
    //System.out.println(num1+" "+operator+num2+"="+num3);
    if(operator=='-'){
    return num1-num2==num3;
    }else{
    return num1+num2==num3;
    }
    }

    /**打印cs中存放的等式
    */
    private void printPattern(char[] cs){
    for(int i=0;i<5;i++){
    for(int j=0;j<cs.length;j++){
    char[][] temp=symbolPattern.get(cs[j]);
    for(int k=0;k<temp[i].length;k++){
    System.out.print(temp[i][k]);
    }
    }
    System.out.println();
    }
    }
    }
      

  4.   

    F:\java>java MatchMove
    测试一
    原等式如下:
     -     -     -
      |   |       |
     -  +  -  =  -
      |   | |     |
     -     -     -
    结果
     -     -     -
    | |   |       |
     -  -  -  =  -
      |   | |     |
     -     -     -
    结果:
     -     -     -
      |   | |     |
     -  +     =  -
      |   | |     |
     -     -     -
    测试二
    原等式如下:
        -     -     -
      |  |     |   |
        -  +  -  =  -
      ||       |     |
        -     -     -
    结果
        -     -     -
      |  |     |   | |
        -  -  -  =  -
      ||       |     |
        -     -     -
    测试三
    原等式如下:
     -  -        -     -  -
    | || |     |  |   |  | |
     -     +     -  =  -  -
      || |     ||     | |  |
     -  -        -     -  -
    结果
     -  -        -     -  -
    |  | |     || |   |  | |
     -     +     -  =  -  -
      || |     |  |   | |  |
     -  -        -     -  -
      

  5.   

    发现有错误:改为:
    transformation.put('2',new char[][]{ {'3'},      null,          null});
    2加一根火柴变不成6,和9.
      

  6.   

    2加一根火柴什么也变不成,所以[3][]是null.
      

  7.   

    楼上不对,
    所以,'2'对应的char[2][] 是null
      

  8.   

    还行 算法应该不是很难 只是考虑的多点map键装0到9 值对应一个10位数组能放入相应火柴,
    运算符录入时就以bool型处理 0到9还要接受两种变化多一根火柴和去一根火柴的会形成的新数字的判断,-时前项必定大于后项,+时第三项大于前两项 下班咯
      

  9.   

    应该不是一定要你写出可运行的完整代码吧,没IDE不好写,应该是你只要写出个大概思路就可以了