码迷,mamicode.com
首页 > 其他好文 > 详细

华为2014第三题

时间:2015-06-15 16:11:23      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.security.auth.kerberos.KerberosKey;
public class Main{
    int N=8;
    int [] step=new int[N];
    String ope[]={"+","-",""};
    ScriptEngineManager manager=new ScriptEngineManager();
    ScriptEngine engine=manager.getEngineByName("js");
    private void step()
    {
        step[N-1]++;
        for(int i=N-1;i>=0;i--)
        {
            if(step[i]>2)
            {
                step[i]=step[i]%3;
                if(i!=0)
                    step[i-1]++;
            }
            else {
                break;
            }
        }
    }
    public int search() throws ScriptException
    {
          int K=(int)Math.pow(3, N);
        int count=0;
          for(int i=0;i<K;i++)
          {
              String operation="1"+ope[step[0]]+"2"+ope[step[1]]+"3"+ope[step[2]]+
                      "4"+ope[step[3]]+"5"+ope[step[4]]+"6"+ope[step[5]]+
                      "7"+ope[step[6]]+"8"+ope[step[7]]+"9";
                //Object result=engine.eval(operation);
              //double d=((Double)result).doubleValue();
                String result=sizeyunsuan(operation);
                double d=Double.parseDouble(result);
                if((int)d==5)
                {
                    count++;
                }
                step();
          }
          return count;
    }
    private static String addBigDecimal(String a,String b)
    {
        double a1=Double.parseDouble(a);
        double b1=Double.parseDouble(b);
        BigDecimal a2=BigDecimal.valueOf(a1);
        BigDecimal b2=BigDecimal.valueOf(b1);
        BigDecimal s=a2.add(b2);
        return s.toString();
    }
    private static String substractBigDecimal(String a,String b)
    {
        double a1=Double.parseDouble(a);
        double b1=Double.parseDouble(b);
        BigDecimal a2=BigDecimal.valueOf(a1);
        BigDecimal b2=BigDecimal.valueOf(b1);
        BigDecimal s=a2.subtract(b2);
        return s.toString();
    }
    private static String sizeyunsuan(String s)
    {
        //1、先将  +-*/找出来
        int p=0;//the count of (+-*/)
        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i)==‘+‘ || s.charAt(i)==‘-‘)
                p++;
        }
        //将字符串按照运算符进行切割总共有  2*p+1个段   example:p=5
        String[] piece=new String[2*p+1];// save pieces divided by operator   11 pieces
        int start=0,index=0;//
        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i)==‘+‘ || s.charAt(i)==‘-‘)
            {
                piece[index]=s.substring(start,i);//index=0  i=1  p[0]="9"
                index++;
                piece[index]=""+s.charAt(i);    //index=1  i=1  p[1]="+"
                index++;
                start=i+1;   //start=2
            }
        }
        // last piece;
        piece[index]=s.substring(start,s.length());
        ///
        int count=p;
        while(count>0)
        {
            //then calculate +-
            for(int i=0;i<piece.length;i++)
            {
                if(piece[i].equals("+") || piece[i].equals("-"))
                {
                    //find strs in piece has not calculated
                    //find strs left not equals "p"
                    int l=0;
                    for(l=i-1;l>-1;l--)
                    {
                        if(!piece[l].equals("p"))
                            break;
                    }
                    //find strs right not equals "p"
                    int r=0;
                    for(r=i+1;r<piece.length;r++)
                    {
                        if(!piece[r].equals("p"))
                            break;
                    }
                    if(piece[i].equals("+"))
                    {
                        piece[i]=addBigDecimal(piece[l], piece[r]);
                        piece[l]="p";
                        piece[r]="p";
                        count--;
                    }
                    else
                    {
                        piece[i]=substractBigDecimal(piece[l], piece[r]);
                        piece[l]="p";
                        piece[r]="p";
                        count--;
                    }
                    //break;
                }
            }
        }
        String r="";
        //find the string not equals "p"
        for(int i=0;i<piece.length;i++)
        {
            if(!piece[i].equals("p"))
            {
                r=piece[i];
                break;
            }
        }
        return r;
    }
      public static void main(String[] args) throws ScriptException{
          Main main=new Main();
          long begin=System.currentTimeMillis();
          int c=main.search();
          long end=System.currentTimeMillis();
          System.out.println(c);
          System.out.println("共花费"+(end-begin)+"ms");
      }
      
}

使用ScriptEngineManager

21

共花费637ms

使用自定义算法

21

共花费80ms

华为2014第三题

标签:

原文地址:http://www.cnblogs.com/maydow/p/4577331.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!