携程笔试第一题,数据流类别排序

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

void countof(int i, int hotel, int poi, int tnt, int& count, vector<string>& res)
{
	if (hotel == 0 && poi == 0 && tnt == 0)
	{
		count++;
		return;
	}
	if (i < 2)
	{
		if (poi > 0)
		{
			res.push_back("poi");
			countof(i + 1, hotel, poi - 1, tnt, count, res);
			res.pop_back();
		}
		if (tnt > 0)
		{
			res.push_back("tnt");
			countof(i + 1, hotel, poi, tnt - 1, count, res);
			res.pop_back();
		}
	}
	if (i > 1 && i < 4)
	{
		if (res[i - 1] != "poi"||res[i - 2] != "poi")
		{
			if (poi > 0)
			{
				res.push_back("poi");
				countof(i + 1, hotel, poi - 1, tnt, count, res);
				res.pop_back();
			}
		}
		if (res[i - 1] != "tnt" || res[i - 2] != "tnt")
		{
			if (tnt > 0)
			{
				res.push_back("tnt");
				countof(i + 1, hotel, poi, tnt - 1, count, res);
				res.pop_back();
			}
		}
	}
	if (i > 3 )
	{
		if (poi > 0 && (res[i - 1] != "poi" || res[i - 2] != "poi"))
		{
			res.push_back("poi");
			countof(i + 1, hotel, poi - 1, tnt, count, res);
			res.pop_back();
		}
		if (tnt > 0 && (res[i - 1] != "tnt" || res[i - 2] != "tnt"))
		{
			res.push_back("tnt");
			countof(i + 1, hotel, poi, tnt - 1, count, res);
			res.pop_back();
		}
		if (hotel > 0 && (res[i - 1] != "hotel" || res[i - 2] != "hotel"))
		{
			res.push_back("hotel");
			countof(i + 1, hotel - 1, poi, tnt, count, res);
			res.pop_back();
		}
	}
}

int main()
{
	int hotel, poi, tnt;
	cin >> hotel >> poi >> tnt;
	if (poi+tnt<4)
	{
		cout << 0 << endl;
		return 0;
    }
	vector<string> vec;
	int count = 0;
	countof(0, hotel, poi, tnt, count, vec);
    cout << count << endl;
}
a了,但是很笨重的代码,有人有更好的方法吗?
总觉得是道数学题。
#笔试题目##携程#
全部评论

相关推荐

今年读完研的我无房无车无对象,月入还没有过万&nbsp;看到他在朋友圈晒房产证,感叹自己白读了这么多年书
梦想是成为七海千秋:那咋了,双9毕业的现在还没存款呢(因为没念完),高中毕业的去直播带货月入几百万也是完全有可能的,退一万步讲,有些人刚出生父母就给买车买房了,上哪说理去,哪怕是同一个起点也会有截然不同的走向,过好自己的生活就完事了。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
ResourceUtilization:四六级不愧是大学最有用的证之一
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务