zoukankan      html  css  js  c++  java
  • 华为2014第三题

    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

  • 相关阅读:
    Recommended Books for Algo Trading in 2020
    Market Making is simpler than you think!
    Top Crypto Market Makers of 2020
    Top Crypto Market Makers, Rated and Reviewed
    爬取伯乐在线文章(五)itemloader
    爬取伯乐在线文章(四)将爬取结果保存到MySQL
    爬取伯乐在线文章(三)爬取所有页面的文章
    爬取伯乐在线文章(二)通过xpath提取源文件中需要的内容
    爬取伯乐在线文章(一)
    爬虫去重策略
  • 原文地址:https://www.cnblogs.com/maydow/p/4577331.html
Copyright © 2011-2022 走看看