想刷水题,结果被水题刷了,蒟蒻求助大佬
查看原帖
想刷水题,结果被水题刷了,蒟蒻求助大佬
699471
yuxiaoyu20090104楼主2022/8/16 20:39
#include<iostream>
#include<string>
using namespace std;
int used[20];//记录用过的数字
bool check(int x)//判断每一位是否用过
{
	bool flag = 1;
	int n = x;
	while (n > 0)
	{
		if(used[n%10]) flag = 0;
		n /= 10;
	}
	if (flag)//如果全没用过
	{
		while (x > 0)//标记每一位
		{
			used[x % 10] = 1;
			x /= 10;
		}
	}
	return flag;
}
void init(int x)
{
	while (x > 0)
	{
		used[x % 10] = 0;
		x /= 10;
	}
}
int main()
{
	//for循环枚举第一个数字
	for (int i = 1; i <= 9; i++)
	{
		used[i] = 1;
		for (int j = 0; j <= 9; j++)
		{
			if (!used[j])
			{
				used[j] = 1;
				for (int l = 0; l <= 9; l++)
				{
					if (!used[l])
					{
						used[l] = 1;
						int x = i * 100 + j * 10 + l;
						int y = x * 2;
						int z = x * 3;
						if (check(y) && check(z))
						{
							cout << x << " " << y << " " << z << endl;
							init(z);//从新初始化
							init(y);
						}
						used[l] = 0;
					}
				}
				used[j] = 0;
			}
		}
		used[i] = 0;
	}
	return 0;
}
2022/8/16 20:39
加载中...