Schrieffer.zsy 2021-03-09 12:29 采纳率: 93.8%
浏览 70
已采纳

为什么我这个程序会什么也不干?

#include<iostream>
#include<vector>
#include<ctime>
using namespace std;

class Person
{
public:
	int code;
	Person(){}
	Person(int a)
	{
		code = a;
	}
	bool operator == (Person P)
	{
		return P.code == code;
	}
}person(1),p0(2);

class Sheep
{
public:
	int code;
	Sheep() {}
	Sheep(int a)
	{
		code = a;
	}
	bool operator == (Sheep P)
	{
		return P.code == code;
	}
}sheep(1),s0(0);

class Wolf
{
public:
	int code;
	Wolf() {}
	Wolf(int a)
	{
		code = a;
	}
	bool operator == (Wolf P)
	{
		return P.code == code;
	}
}wolf(1),w0(0);

class Vegetable
{
public:
	int code;
	Vegetable() {}
	Vegetable(int a)
	{
		code = a;
	}
	bool operator == (Vegetable P)
	{
		return P.code == code;
	}
}vegetable(1),v0(0);



class Shore
{
public:
	Person p;
	Sheep s;
	Wolf w;
	Vegetable v;
	Shore()
	{
		p = person;
		s = sheep;
		w = wolf;
		v = vegetable;
	}

	bool checkShore()
	{
		bool judge=1;
		
		judge = judge && ((p == p0) && (s == sheep) && (v == vegetable) && (w == wolf));
		judge = judge && ((p == p0) && (s == sheep) && (v == vegetable) && (w == w0));
		judge = judge && ((p == p0) && (s == sheep) && (v == v0) && (w == wolf));

		judge = judge && ((p == person) && (s == s0) && (v == v0) && (w == w0));
		judge = judge && ((p == person) && (s == s0) && (v == v0) && (w == wolf));
		judge = judge && ((p == person) && (s == s0) && (v == vegetable) && (w == w0));

		return judge;
	}

	void printShore()
	{
		cout << "原岸:" << endl;
		if (p == person) { cout << "人  "; }
		if (s == sheep) { cout << "羊  "; }
		if (w == wolf) { cout << "狼  "; }
		if (v == vegetable) { cout << "菜  "; }
		cout << endl << endl;
	}
}shore;

void printSolution()
{
	int i = 0;
	srand((unsigned int)time(NULL));

	while (true)
	{
		if (i % 2 == 0)
		{
			shore.p = p0;
			while (true)
			{
				int judge = rand()%3+1;

				if (judge == 1)
				{
					shore.s = s0;
					if (shore.checkShore())
					{
						break;
					}
					shore.s = sheep;
				}
				else if (judge == 2)
				{
					shore.v = v0;
					if (shore.checkShore())
					{
						break;
					}
					shore.v = vegetable;
				}
				else
				{
					shore.w = w0;
					if (shore.checkShore())
					{
						break;
					}
					shore.w = wolf;
				}
			}
			shore.printShore();
		}
		else
		{
			shore.p = person;
			while (true)
			{
				int judge = rand() % 4 + 1;

				if (judge == 1)
				{
					shore.s = sheep;
					if (shore.checkShore())
					{
						break;
					}
					shore.s = s0;
				}
				else if (judge == 2)
				{
					shore.v = vegetable;
					if (shore.checkShore())
					{
						break;
					}
					shore.v = v0;
				}
				else if(judge==3)
				{
					shore.w = wolf;
					if (shore.checkShore())
					{
						break;
					}
					shore.w = w0;
				}
				else 
				{
					if (shore.checkShore())
					{
						break;
					}
				}
			}
			shore.printShore();
		}
		i++;
		if ((shore.p == p0) && (shore.s == s0) && (shore.v == v0) && (shore.w == w0))
		{
			break;
		}
	}
	cout << "运送结束" << endl;
}

int main()
{
	printSolution();
	system("pause");
	return 0;
}

什么也不打印,但也不会崩。

  • 写回答

3条回答 默认 最新

  • ProfSnail 2021-03-09 13:20
    关注

    程序不是啥也没干,是一直干着活呢,但是一直在循环。

    你这是蒙特卡洛过桥法啊。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP