Algorithmic Basis-Enumeration

Keywords: PHP less

Enumeration: A Stylistic Solution Strategy Based on Attempting to Answer One by One

1. Perfect Cube

Title Description:

Equations such as a^3= b^3 + c^3 + d^3 are called perfect cubic equations. for example
12 ^ 3 = 6 ^ 3 + 8 ^ 3 + 10 ^ 3. Write a program to assign a positive integer N
(N < 100), look for all quaternions (a, b, c, d), so that a^3 = b^3+
c^3 + d^3, where a,b,c,d are greater than 1, less than or equal to N, and B < = C < = D.
input
A positive integer N (N < 100).
output
Output of each line is a perfect cube. The output format is:
Cube = a, Triple = (b,c,d)
Where a, b, C and D are located, they are substituted by the actual values of quaternions.
Require a from small to large output.   

Ideas for solving problems:

Quadruple cyclic enumeration a,b,c,d
a is in the outermost layer, d is in the innermost layer, each layer is enumerated from small to large.
a Scope [2, N]
b Scope [2, a-1]
c Scope [b, a-1]
d range [c, a-1]

Code example:

// Perfect Cube
# include <stdio.h> 

int main(void)
{
	int a,b,c,d;
	int N;
	scanf("%d",&N);
	
	for (a=2; a<=N; a++)
		for (b=2; b<=a-1; b++)
			for (c=b; c<=a-1; c++)
				for (d=c; d<=a-1; d++)
					if (a*a*a == b*b*b + c*c*c + d*d*d)
						printf("Cube = %d, Triple = (%d,%d,%d)\n",a,b,c,d);
	
	return 0;
}

2. Physiological Cycle

Title Description:

People are born with three physiological cycles: physical, emotional and intellectual cycles, which are 23, 28 and 33 days in length. One day in each cycle is the peak. On peak day, people will perform well in corresponding aspects. For example, at the peak of the intellectual cycle, people are quick-thinking and easy to concentrate. Because the perimeters of three cycles are different, the peak of three cycles usually does not fall on the same day. For everyone, we want to know when the three peaks fall on the same day. For each cycle, we give the number of days from the first day of the current year to the peak (not necessarily the first peak).
 Your task is to give a number of days starting from the first day of the year and output three peaks starting at a given time (excluding a given time).
 The time that falls on the same day (the number of days from a given time).
 For example, given a time of 10, the next time three peaks occur on the same day is 12, then output 2 (note that this is not 3). The input has multiple sets of data, ending with a line-1.

Ideas for solving problems:

Starting from day d+1, try until day 21252 to see if you are satisfied with each day.
The difference between current date and p e i can be divided by 23 2833 at the same time, respectively.

Code example:

// Physiological cycle 
# include <iostream>
using namespace std;

int main(void)
{
	int p,e,i,d,j, num=0;
	// It's not necessary to enumerate them all. 
	while (cin>>p>>e>>i>>d && p!=-1)
	{
		++num;
		for (j=d+1; (j-p)%23; j++); // Find the first date that is 23 times 
		for (     ; (j-e)%28; j+=23); // Again, look at the increment of 23 to see if it's a multiple of 28. 
		for (     ; (j-i)%33; j+=23*28); // Finally, the increment of 23*28 is used to determine whether it is a multiple of 33. 
		cout<<"Case"<<num<<": the next triple peak occours in";
		cout<<j-d<<"days"<<endl; // The interval between the final output j d  
	}
	
	return 0;
 } 

3. Coins

Problem Description:

There are 12 coins, including 11 real coins and one counterfeit coin.
Counterfeit coins weigh differently from real ones, but it is not known whether counterfeit coins are lighter or heavier than real ones.
Now, weigh these coins three times with a balance and tell you the result of weighing.
Please find out the counterfeit coins and determine whether they are light or heavy.

Input sample:
1
ABCD EFGH even//balance left is ABCD four, right is EFGH, balance.
ABCI EFJK up//Right up
ABIJ EFGH even//same
// They all represent the right weight.

Ideas for solving problems:

		For each coin, assume that it is light to see if it fits the weighing result.
		If so, the problem will be solved. Otherwise, assume that he is heavy, see if
		It accords with the result of weighing. If you try all the coins, you will find some special coins.

Code example:

// Coin
# include <iostream>
# include <cstring> 
using namespace std;
char Left[3][7]; // Coins on the left of the balance 
char Right[3][7]; // Coins on the right of the balance 
char result[3][7]; // Result 
bool IsFake(char c, bool light);

int main(void)
{
	int t; 
	cin>>t;
	while(t--)
	{
		for (int i = 0; i<3; i++)
			cin>>Left[i]>>Right[i]>>result[i];
		for (char c='A'; c<='L'; c++)
		{
			if (IsFake(c, true))
			{
				cout<<c<<" is the counterfeit coin and it is light.\n";
				break;
			}
			else if(IsFake(c, false))
			{
				cout<<c<<" is the counterfeit coin and it is heavy.\n";
				break;
			}
		}
	}
	return 0;
}

bool IsFake(char c, bool light)
// Light is true, assuming the counterfeit currency is light, otherwise the counterfeit currency is heavy. 
{
	for (int i=0; i<3; i++)
	{
		char * pLeft,*pRight; // A string pointing to both sides of the balance
		if (light)
		{
			pLeft = Left[i];
			pRight = Right[i];
		}
		else // If the hypothesis is heavy, the balance results are changed from left to right. 
		{
			pLeft = Right[i];
			pRight = Left[i];
		} 
		
		switch(result[i][0]) // The situation on the right side of the balance 
		{
			case 'u': if(strchr(pRight,c) == NULL)
						return false;
					  break;
			case 'e': if(strchr(pLeft,c) || strchr(pRight,c))
						return false;
					  break;
			case 'd': if(strchr(pLeft,c) == NULL)
						return false;
					  break;
		}
		 
	} 
	return true;
} 

  

 

RRR

Posted by Sinikka on Tue, 23 Jul 2019 08:50:32 -0700