Topic of the 2019 Lanqiao Cup Provincial Competition - "Decomposition of Numbers"

Table of contents

topic

Require

train of thought

final code

result


topic

Decompose 2019 into the sum of 3 different positive integers, and require that each positive integer does not contain the numbers 2 and 4, how many different decomposition methods are there?

Note that swapping the order of 3 integers is considered the same way, e.g. 1000+1001+18 and 1001+1000+18 are considered the same.

Require

This is a question to fill in the blanks, you just need to calculate the result and submit it

train of thought

The idea is very simple - enumerate every number

Every positive integer does not contain the numbers 2 and 4, so the thousands, hundreds, and tens of each digit are extracted and judged separately

bool Panduan(int n)
{
	int a = n / 1000;
	int b = (n - a * 1000) / 100;
	int c = (n - a * 1000 - b * 100) / 10;
	int d = n % 10;
	if (a == 2 || a == 4 || b == 2 || b == 4 || c == 2 || c == 4 || d == 2 || d == 4)
	{
		return false;
	}
	else
	{
		return true;
	}
}

Regarding the screening, only the numbers with different orders can be fixed in size, such as three numbers x, y, z fixed x>y>z, so that it will not cause more calculation results because of the different order

	for (int i = 1; i<2019 ;i++)
	{
		for (int j = i+1; j < sum_1 - i; j++)
		{
			int z = sum_1 - i - j;
        ...
        }
    }

final code

that is

#include <iostream>
using namespace std;
int sum_1 = 2019;
bool Panduan(int n)
{
	int a = n / 1000;
	int b = (n - a * 1000) / 100;
	int c = (n - a * 1000 - b * 100) / 10;
	int d = n % 10;
	if (a == 2 || a == 4 || b == 2 || b == 4 || c == 2 || c == 4 || d == 2 || d == 4)
	{
		return false;
	}
	else
	{
		return true;
	}
}
int main()
{
	int sum_max = 0;
	for (int i = 1; i<2019 ;i++)
	{
		for (int j = i+1; j < sum_1 - i; j++)
		{
			int z = sum_1 - i - j;
			if (i == z || z == j || j >= z)
			{
				continue;
			}
			if (Panduan(i) && Panduan(j) && Panduan(z))
			{
				sum_max++;
			}
			else
			{
				continue;
			}
		}
	}
	cout << sum_max << endl;
    return 0;
}

result

40785

Guess you like

Origin blog.csdn.net/mumuemhaha/article/details/132119564