神奇的变量。。。求助!
查看原帖
神奇的变量。。。求助!
787888
Channel_Choo楼主2024/11/22 07:53
#include <iostream>
using namespace std;
unsigned long long n, k, a[45], maxx = -1, mul;
void dfs(unsigned long long x, unsigned long long ans, unsigned long long last)
{
	if (x == k)
	{
		mul = 0;
		for (unsigned long long j = last + 1;j < n;j++)
		{
			mul += a[j];
			mul *= 10;
		}
		mul += a[n];
		maxx = max(maxx, mul * ans);
		return ;
	}
	for (unsigned long long i = last + 1;i < n - k + x;i++)
	{
		mul = 0;
		for (unsigned long long j = last + 1;j < i;j++)
		{
			mul += a[j];
			mul *= 10;
		}
		mul += a[i];
		dfs(x + 1, ans * mul, i);
	}
}
int main()
{
	cin >> n >> k;
	for (unsigned long long i = 1;i <= n;i++)
	{
		char c;
		cin >> c;
		a[i] = c - '0';
	}
	dfs(0, 1, 0);
	cout << maxx;
	return 0;
}

#include <iostream>
using namespace std;
long long n, k, a[45], maxx = -1, mul;
void dfs(long long x, long long ans, long long last)
{
	if (x == k)
	{
		mul = 0;
		for (int j = last + 1;j < n;j++)
		{
			mul += a[j];
			mul *= 10;
		}
		mul += a[n];
		maxx = max(maxx, mul * ans);
		return ;
	}
	for (int i = last + 1;i < n - k + x;i++)
	{
		mul = 0;
		for (int j = last + 1;j < i;j++)
		{
			mul += a[j];
			mul *= 10;
		}
		mul += a[i];
		dfs(x + 1, ans * mul, i);
	}
}
int main()
{
	cin >> n >> k;
	for (int i = 1;i <= n;i++)
	{
		char c;
		cin >> c;
		a[i] = c - '0';
	}
	dfs(0, 1, 0);
	cout << maxx;
	return 0;
}

这份代码我是用了unsigned long long 过不了,0pts,第二份代码是我用long long 60pts,求解为什么long long能过,但是unsigned long long 过不了

回复必关

2024/11/22 07:53
加载中...