Day7-------准备蓝桥杯的第七天

啊啊啊啊拖了大半个月,现在才更新到第一周,真是羞愧羞愧。可是没办法,还有12天就要过年啦,距离去学校仅剩22天啊啊啊!再加把劲吧,22天后,活出不一样的自己,怎么说呢,可能还是有相同点的,还是一样渣吧hhhhhh!

随机算法的运用

eg1、30个人的班级,出现生日重复的概率

public class Birth{
   public static void main(String[] args){
     //概率模拟,0-365中随机产生数字,结果有没有产生碰撞。
     final int N = 1000*100;
     int n = 0;
     for(int i = 0;i<N;i++){
         for(int j = 0;j<30;j++){
            int[] x = new int[365];
            int p = (int)(Math.random()*365);
            if(x[p] == 1){
               n++;
               break;
            }
            else
                x[p] = 1;
          }
        }
        double r = (double)n/N;
        System.out.println(r);
      }
}

eg2、给定4张扑克牌,点数为1-10,用加减乘除来计算,结果正好是24。

import java.util.*;
import java.util.Stack;

public class jisuanRandom {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		while(true) {
			System.out.print("请输入四位整数:");
			String[] ss = in.nextLine().split(" +");
			f(ss);
		}
	}
	public static void f(String[] ss) {
		/*
		 * 思路:随机产生组合序列,计算结果若等于24,则表示找到了;
		 * 运用栈,6 3 4 * +表示为6+3*4,这个表达式为逆波兰表达式,看到数字就压栈。
		 */
		for(int k = 0;k<1000*100;k++) {
			String[] buf = new String[7];
			for(int i = 0;i<4;i++) {
				buf[i] = ss[i];
			}
			for(int i = 4;i<7;i++) {
				buf[i] = random_op();
			}
			shuffle(buf);  //打乱次序
			if(ji_suan(buf)) {
				show(buf);
				break;
			}
		}
	}
	public static String random_op() {
		int n = (int)(Math.random()*4);
		if(n==0) return "+";
		if(n==1) return "-";
		if(n==2) return "*";
		return "/";
		
	}
	public static void shuffle(String[] x) {
		for(int i = 0;i<x.length;i++) {
			int j = (int)(Math.random()*x.length);
		    String t = x[i];
		    x[i] = x[j];
		    x[j] = t;
	   }
	}
	public static boolean ji_suan(String[] data) {
		Stack stk = new Stack();
		try {
			for(int i = 0;i<data.length;i++) {
				if(data[i].equals("+") || data[i].equals("-")|| data[i].equals("*")|| data[i].equals("/")) {
					int a = Integer.parseInt((String)stk.pop());
					int b = Integer.parseInt((String)stk.pop());
					stk.push(op(a,b,data[i]+""));
				}
				else
					stk.push(data[i]);
			}
		}
			catch (Exception e) {
				return false;
			}
			if(stk.size()==1&&stk.pop().equals("24")) {
				return true;
			}
			return false;
	}
	public static int op(int a,int b,String oper) throws Exception {
		if(oper.equals("+")) return a+b;
		if(oper.equals("-")) return a-b;
		if(oper.equals("*")) return a*b;
		if(a%b!=0) throw new Exception("not /");
		return a/b;
	}
	public static void show(String[] data) {
		Stack stk = new Stack();
		for(int i = 0;i<data.length;i++) {
			if(data[i].equals("+") || data[i].equals("-")|| data[i].equals("*")|| data[i].equals("/")) {
				stk.push("("+stk.pop()+data[i]+stk.pop()+")");
				//int a = Integer.parseInt((String)stk.pop());
				//int b = Integer.parseInt((String)stk.pop());
				//stk.push(op(a,b,data[i]+""));
			}
			else {
				stk.push(data[i]);
			}
			System.out.println(stk.pop());
	    }
	}
}

这个代码我检查了好多遍,都没有发现错误,可就是运行不出来,以后再改进了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值