【蓝桥杯学习记录】【7】随机算法的应用

一个大问题,没有解决那个扑克牌凑24问题中的 栈的问题,以后继续完善

#include <iostream>
#include <cstdio>
#include <string.h>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <time.h>
#include <stack>

#define PI atan(1.0)*4

using namespace std;
/*
string random_op()
{
	srand( (unsigned)time( NULL ) ); 
	int a = rand() % 4;
	switch(a)
	{
		case(0): return "+";
		case(1): return "-";
		case(2): return "*";
		case(3): return "/";
	}
}

void shuffle(string x[])
{
	srand( (unsigned)time( NULL ) ); 
	for(int i = 0; i < x->length(); i++)
	{
		int j = rand() % x->length();
		string t = x[i];
		x[i] = x[j];
		x[j] = x[i];
	}
}
int op(int a, int b, string opera)
{
	if(opera == "+") return a + b;
	if(opera == "-") return a - b;
	if(opera == "*") return a * b;
	//if(opera == "/") return a + b;
}
int compute(string data[])//计算合法的逆波兰表达式 
{
	stack <int> stk; 
	try{
		for(int i = 0; i < data->length(); i++)
		{
			if(data[i] == "+" || data[i] == "-" || data[i] == "*" || data[i] == "/")
			{
				int	a = stk.top();
				stk.pop();
				int	b = stk.top();
				stk.pop();
				stk.push(op(a,b,data[i]));
			}
			else 
				stk.push(data[i]);
		}
	}
	catch(Exception e) return 0;
	if(...24) return 1;
	else return 0;
}
*/
int main()
{
	//概率的方法解决问题
	
	/**30人的班级,出现生日重复 的概率有多大**/ 
	//1 ~ 365  随机产生数字,  这些数字有没有碰撞
	
	int count = 0;
	int N = 1000 * 100;
	srand( (unsigned)time( NULL ) );
	//srand()函数产生一个以当前时间开始的随机种子.
	//应该放在for等循环语句前面 不然要很长时间等待
for(int n = 0; n < N; n++)
{
	
	/*int a[30];
	for(int i = 0; i < 30;i++)
	{
		a[i] = rand() % 365; 
		//cout << a[i] << endl;
	} 
	for(int j = 0,flag = 0; j < 30 && flag == 0; j++)
	{
		for(int k = j+1;k < 30; k++)
		{
			if(a[j] == a[k])
			{
				count++;
				flag = 1; 
				//cout << a[j] << " " << a[k] << endl;
				break;
				//goto breakLoop;
			}
		}
	}
	//breakLoop:;
	*/
	int* a = new int[365];
	for(int i = 0; i < 30;i++)
	{
		int p = rand() % 365;
		if(a[p] == 1)
		{
			count++;
			break;
		}
		else
			a[p] = 1;
		
	}
	
}
	
	cout << count << endl;
	cout << (double)count / N << endl;
	double a = 1,b = 1,c = 1;
	for(int t = 0; t < 30; t++)
	{
		//a = 365*a;
		//b = (365-t)*b;
		c = c*(365-t)/365;
	}
	cout << 1-c <<endl;
	
	
	
	/**
	给定4张扑克牌,
	点数 1 ~ 10 
	用 + - * / 运算
	结果正好是24 
	**/ 
	
	//逆波兰表达式
	// 6 3 4 * +
	// 3 + 4 * 6
	// 逆波兰表达式 碰见数字就压栈 碰见运算符就出栈
	// 逆波兰表达式(后缀表达式)
	// 波兰式(前缀式)
	
	/*string a[4];
	for(int ii = 0; ii <= 3; ii++)
	{
		cin >> a[ii];
	}
	for(int k = 0; k <10000; k++)
	{
		string buf[7];//4个数 3个操作符 
		for(int i = 0; i < 4; i++)
			buf[i] = a[i];
		for(int i = 4; i < 7; i++)
			buf[i] = random_op();
		shuffle(buf);
		if(compute(buf))
			show(buf);
	}
*/
	
	
	
	
	
	
	
	 
	
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值