寒假5-数学与简单dp

寒假5-数学与简单dp_第1张图片

#include
using namespace std;
int n, m;
bool dp[1000000];
int main()
{
	cin >> n >> m;
	int minn = min(n, m);
	int maxx = max(n, m);
	int ans = 1;
	dp[0] = true;
	for (int i = minn;i < n * m;i++)
	{
		if (dp[i - minn])dp[i] = true;
		else if (i >= maxx && dp[i - maxx])dp[i] = true;
		else ans = i;
	}
	cout << ans << endl;
	return 0;
}

 

寒假5-数学与简单dp_第2张图片

#include
using namespace std;
#include
int arr[110];
int fx[110];
int n;
int main()
{
	cin >> n;
	int ans = 1;
	int t1 = 0, t2 = 0, t3 = 0, t4 = 0;
	for (int i = 1;i <= n;i++)
	{
		cin >> arr[i];
		if (i >= 2)
		{
			if (arr[i] > 0)
			{
				fx[abs(arr[i])] = 1;
			}
			else
			{
				fx[abs(arr[i])] = -1;
			}
		}
	}
	for (int i = 100;i > abs(arr[1]);i--)
	{
		if (fx[i] == 1)t1++;
		else if (fx[i] == -1)
		{
			t3++;break;
		}
	}
	for (int i = 1;i < abs(arr[1]);i++)
	{
		if (fx[i] == -1)t2++;
		else if (fx[i] == 1)
		{
			t4++;
			break;
		}
	}
	if (arr[1] > 0)
	{
		for (int i = 1;i <= n;i++)
		{
			if (arr[i] < arr[1] && arr[i]>0)
			{
				ans++;
			}
			else if (arr[i] < 0 && abs(arr[i])>abs(arr[1]))
			{
				ans++;
			}
		}
		if (t3 == 0)ans = 1;
	}
	else
	{
		for (int i = 1;i <= n;i++)
		{
			if (arr[i] > 0 && arr[i] < abs(arr[1]))
			{
				ans++;
			}
			else if (arr[i]<0 && abs(arr[i]) > abs(arr[1]))
			{
				ans++;
			}
		}
		if (t4 == 0)ans = 1;
	}
	cout << ans << endl;
	return 0;
}

 

寒假5-数学与简单dp_第3张图片 

#include
using namespace std;
int main()

{
	int n;
	cin >> n;
	int sum = n;
	int pingai = n;
	while (pingai >= 3)
	{
		sum += (pingai / 3);
		pingai = (pingai / 3) + (pingai % 3);
	}
	cout << sum << endl;
	return 0;
}

 

 

#include
using namespace std;
int n, v;
int V[1010], N[1010];
int f[1010][1010];
int main()
{
	cin >> n >> v;
	for (int i = 1;i <= n;i++)
	{
		cin >> V[i] >> N[i];
	}
	for (int i = 1;i < n;i++)
	{
		for (int j = 1;j <= v;j++)
		{
			f[i][j] = f[i - 1][j];
			if (j >= V[i])f[i][j] = max(f[i][j], f[i - 1][j - V[i]] + N[i]);
		}
	}
	cout << f[n][v] << endl;
	return 0;
}

你可能感兴趣的:(算法,c++,图论)