A basic ACM one-dimensional array application problem

The title describes that
    there is a row of trees on the road with a length of L on the street, and the distance between every two trees is 1 meter; that is, 0, 1, 2, 3,..., L meters, the positions are all There is a tree;
    there are some areas to be used to build the subway; the area is represented by the starting point and the ending point, the starting point and the ending point are both integers, integer meters; now the trees in the area (including the two endpoints) should be removed; the calculation is to
    remove After the trees in the area, the number of remaining trees;

A sample input
    500 3
    150 300
    100 200
    470 471
    sample output: 298

The original program is like this;

Let's do the MFC version; VC6 creates a new dialog project;

The design interface is as follows;

 

Add member variables for the edit box, all of UINT type;

 

Add another button to the interface, and add the click function of the button;

 

m_cnt is the number of areas, and each calculation can have 1-4 areas, and the edit box corresponding to m_cnt can only be entered in 1-4;

 

Add the following member variables to the header file of the dialog box;

private:
    int a[10000], i, j, m;

a, define a large-capacity array;
i, the loop variable;
m, the number of intervals calculated this time;

j, the final result;

In addition, an edit box needs to be added to the interface to add member variable m_result, UINT type, to display the result;

In the initialization function CShutestDlg::OnInitDialog() of the dialog box, the array a is initialized, and the result is j;

    memset(a, 0, sizeof(a));
    j = 0;

The name of the project is shutest;

button click function,

void CShutestDlg::OnShuCalc() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m = m_cnt;

	//在这次的区间数中循环
	for(int k=0; k<m; k++)
	{
		if(k==0)
		{
			for(i=m_qj1; i<=m_qj2; i++)
			{
			   a[i] = 1;
			}
		}

		if(k==1)
		{
			for(i=m_qj3; i<=m_qj4; i++)
			{
			   a[i] = 1;
			}
		}

		if(k==2)
		{
			for(i=m_qj5; i<=m_qj6; i++)
			{
			   a[i] = 1;
			}
		}

		if(k==3)
		{
			for(i=m_qj7; i<=m_qj8; i++)
			{
			   a[i] = 1;
			}
		}
	}

	for(i=0; i<=m_sum; i++)
	{
	   if(a[i] != 1)
	   {
		  j = j+1;
	   }
	}

	m_result = j;
	UpdateData(FALSE);
}

Start the program, enter some data, and click the button to calculate as follows; 

 

It cannot be calculated continuously; if you want to calculate another situation, you need to restart the program;

 

 Time to improve; add some input exception handling;

Guess you like

Origin blog.csdn.net/bcbobo21cn/article/details/131821563