C ++ enumeration algorithm



What is an enumeration algorithm ?
  Enumeration, as the name implies, is to solve the problem with the most clumsy method (violent enumeration). A set enumeration is a program that lists all members of some finite sequence set, or is a count of a specific type of object . These two types often (but not always) overlap.

The enumeration algorithm is the one we use most in our daily life. Its core idea is:


The essence of enumerating all possible enumeration methods is to search for the correct solution from all candidate answers. The use of this algorithm needs to meet two conditions: (1) the number of candidate answers can be predetermined; (2) the range of candidate answers is There must be a certain set before solving.
The next few questions will give you a better understanding of enumeration

1.
Description of floor number topic:

Kobayashi stayed at the "New World" hotel during the NOIP competition. Unlike other hotels, this hotel has a high-energy number t every day. This number will not appear on the floor. Taking t = 3 as an example, the floors 3, 13, 31, 33 and so on do not exist.
Yes , the floor numbers are 1, 2, 4, 5, ... So the actual 4th floor is the 3rd floor. Kobayashi is known to have booked the room with the number m floor, and the high energy number on the day is t. What is the actual floor
1
2
3
4
Enter description

One line of two integers m and t, 1 <= m <= 100000, 0 <= t <= 9, to ensure that m has a legal
1
output description for t

Line an integer representing the real floor
1
Sample Input

14 3

Sample output

12

Ideas:

1. The floor containing this high-energy number must not appear, then the algorithm is as follows

bool s(int a,int b){
while(a!=0){
if(a%10==b){
return false;
}
a/=10;
}
return true;
}

 


2. Use a for loop to accumulate sum, if it does not contain high-energy numbers (ie s (i, high-energy numbers) = true), then sum ++

 

for(int i=1;i<=m;i++){
if(s(i,t)){
sum++;
}
}
cout<<sum

 


Code:

#include <iostream>
using namespace std;
bool s(int a,int b){
while(a!=0){
if(a%10==b){
return false;
}
a/=10;
}
return true;
}
int main(){
int m,t,sum=0;
cin>>m>>t;
for(int i=1;i<=m;i++){
if(s(i,t)){
sum++;
}
}
cout<<sum;
return 0;
}

 



2. Curling


Title description:

In the curling game, a target point P and a specified positive integer r are given. After each game, the teams A and B took turns curling 8 times, and the game ended. At this time, which side's curling is finally closer to the target point P, the side scores, and the other side does not score. Each scoring party can get 1 point if the distance from the target point P is less than or equal to r, and the curling position is closer to the target point P than all the curling of the other team

The game can be played up to 10 innings. After the end of a match between the two sides, the lagging side can abstain. At this point, the game is no longer going on

It is known that at the end of each round, the distance between each curling side of the two sides from the target point P and the positive integer r, please write a program to judge the score of each round between the two teams and the total score

Enter:

A positive integer r in the first line

 

There are several lines below (no more than 20 lines), each line of 8 positive integers (with a space between them)

 

The jth number in the second line indicates the distance from the jth curling of Party A to the target point P at the end of the first game

 

The jth number in the third line indicates the distance from the jth curling of Party B to the target point P at the end of the first game

...

The jth number in line 2k indicates the distance from the jth curling of Party A to the target point P at the end of the kth game

The jth number in line 2k + 1 indicates the distance from the jth curling of Party B to the target point P at the end of the kth game

If one party abstains halfway, the last line (even line) only has an integer -1, indicating that a waiver occurs at this time

:

Several lines are output, with two integers in each line, separated by a colon in the middle, indicating the scores of both parties in each game (A score first). There are 2 integers in the last line, separated by a colon in the middle, indicating the final score of both parties (A scores first)

This means, first enter a "standard" r, there are 10 games, each game is played 8 times, take the best score (the most recent is the smallest), and then compare with the opponent's best score, lower than the opponent must not If the score is higher than the opponent, the score will be smaller or equal to the standard. That is to say, in each game, one of the scores must not be scored, and there may be no score. If you abstain, you can directly output and return. 0; Finally, the total score is output (count only)
1
2
3
mainly enumeration

Sample input

12

5 60 25 74 71 100 3 93

66 75 70 66 52 73 67 14

93 84 74 99 79 64 89 22

65 5 95 59 80 8 35 61

65 61 49 60 58 50 32 85

68 38 96 38 82 64 26 93

74 92 47 21 97 30 45 78

44 99 90 27 3 46 55 34

49 45 83 3 18 1 67 23

60 47 95 81 17 1 87 85

18 74 74 84 29 20 27 71

37 60 26 56 23 65 67 49

57 7 62 92 52 5 10 69

46 97 88 28 76 27 66 7

89 89 94 31 11 20 1 17

19 48 35 6 77 61 45 21

52 11 76 70 73 99 85 55

90 25 20 7 64 24 94 4

3 43 32 74 10 93 35 77

77 100 63 91 10 73 22 57

Sample output

2:0

0:2

0:0

0:1

0:0

0:0

1:0

1:0

0:2

1:0

5:5

Sample explanation:

You can see that the standard is 12,

In the first game, A has the lowest 3, B has the lowest 14, 3 <14 && 3 <= 12, A scores, B does not score

In the second game, A has the lowest 22, B has the lowest 5, 5 <22 && 5 <= 12, B scores, A does not score

In the third game, A has a minimum of 32 and B has a minimum of 26

。。。。。。

Code

#include<iostream>
#include<string>
using namespace std;
int main()
{
int r,i,j;
cin>>r;
int a[9],b[9],f=0,f1=0,zf=0,zf1=0,min=10000,min1=100000;
for(i=1;i<=10;i++)
{
for(j=1;j<=8;j++)
{
cin>>a[j];
if(a[1]==-1)
{
cout<<zf<<':'<<zf1;
return 0;
}
}
for(j=1;j<=8;j++)
{
cin>>b[j];
}
for(j=1;j<=8;j++)
{
if(a[j]<min)
{
min=a[j];
}
}
for(j=1;j<=8;j++)
{
if(b[j]<min1)
{
min1=b[j];
}
}
if(min<min1)
{
for(j=1;j<=8;j++)
{
if(a[j]<=r&&a[j]<min1)
{
f++;
}
}
cout<<f<<':'<<f1<<endl;
zf=zf+f;
f=0;
}
else if(min1<min)
{
for(j=1;j<=8;j++)
{
if(b[j]<=r&&b[j]<min)
{
f1++;
}
}
cout<<f<<':'<<f1<<endl;
zf1=zf1+f1;
f1=0;
}
else
{
cout<<0<<':'<<0<<endl;
}
min=10000;10000=
min1;
}
cout<<zf<<':'<<zf1;
return 0;
}

 



The method of finding the minimum value is used here, and it can also be changed to [0] after sorting.

 

Guess you like

Origin www.cnblogs.com/AK-IOI/p/12726284.html