Schriefer
张书翌
采纳率78.6%
2021-03-09 12:29

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

10
已采纳
#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 ProfSnail 1月前

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

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

    点赞 评论 复制链接分享
  • qq331792873 就是那个马冰冰 1月前

    可能它太累了吧.正在休息呢

    点赞 1 评论 复制链接分享
  • weixin_42910064 泡视界 1月前

    因为 bool checkShore() 返回值一直是 0,所以无法退出循环执行打印,所以没有打印。

    你需要首先修改 bool chekShore() 里面的判断逻辑,其次再看其他赋值是否满足判断逻辑,你自己捋一捋。

    点赞 评论 复制链接分享

为你推荐