## Topic A: number selection

Question:

Thinking: the core idea of this question is enumeration. It is to find out the combination of all k numbers in n numbers, and then judge whether the sum of K numbers is the same as the target value

The easiest idea to think of is to use k cycles to find out all the situations, but obviously this method is not good, and the time complexity is too high.

So it'S relatively easy to solve it with delivery. Our purpose is to select k numbers and their sum is S. then we will judge each number once. For each number, we have the option of selecting the number and not selecting the number. When we select the number, count plus one, on this basis, we will judge whether the next number (recursive) is selected or not; when we do not select the number, we will not count, on this basis, we will judge whether the next number is selected or not The election. Then the next step is to judge the condition of return. Obviously, when the number obtained is equal to K and S exactly meets the condition, return will no longer choose from this basis, and then step back to make more choices (at this time, fix the previous k-1 step, change step k, and recurse in turn); there is also the result that does not meet the requirements. If K is not selected enough, the value is greater than S, or We have enough K, the value has not reached S, and the index of the number exceeds the number of arrays, all of which are returned.

Let's take the example to draw a schematic diagram. Each number is selected according to the choice of no selection first and then selection.

And so on. It's similar to the principle of playing nine links. It's recursive and backward.

#include<iostream> using namespace std; int y;//Qualified matching quantity int n,K,S;//Define the number of arrays, the number to get, and the sum to get int *a=new int[10000];//Array to hold input void solve2(int i,int sum,int x)//i is the index of the current array, sum is the target not reached, and x is the number of selected numbers {//Recursive function, used to calculate several qualified States if(x==K &&sum ==0) { y++; return; } if(i>=n) return; if(x>K||sum<0) return; solve2(i+1,sum,x); x++; solve2(i+1,sum-a[i],x); x--; } int main() { int T;//Number of input groups cin>>T; for(int i=0;i<T;i++)//Group loop { y=0; cin>>n>>K>>S; //cout<<n<<K<<S<<endl; //cout<<1<<endl; for(int j=0;j<n;j++) { // cout<<2<<endl; cin>>a[j]; } solve2(0,S,0); cout<<y<<endl; } }

## Topic B interval selection

Question:

Train of thought: the key to this problem is to sort the intervals. How to sort the intervals to ensure that a new number will be calculated once the latter interval does not intersect the former. The left boundary sorting I used at the beginning, although it can be done through simple examples, but it can't be compared through drawing, like the right boundary sorting, to ensure that the newly emerging interval traversed from front to back doesn't coincide with the previous interval. If we turn the problem into taking the right boundary of the interval to be compared as the mark point, then the right boundary sorting can ensure each new count The right boundary of the interval to be compared is a new marker point.

`

#include <iostream> #include<algorithm> using namespace std; struct quyu { int x,y; operator < (quyu a) { if(y!=a.y) return y<a.y; else return x>a.x; } }; int main() { int n; cin>>n; quyu m[1000]; //int a,b; for(int i=0;i<n;i++) { cin>>m[i].x>>m[i].y; //cout<<m[i].x<<" "<<m[i].y<<endl; } sort(m,m+n); /*for(int i=0;i<n;i++) { cout<<m[i].x<<" "<<m[i].y<<endl; }*/ int ans=1;//The number of recording points, initially a point in the first interval, increases when there is an interval that does not overlap with it int index=0;//Record the index of the currently compared right bound interval, initially the first interval for(int i=1;i<n;i++)//You need to compare all the intervals except the first one { // cout<<1<<endl; if(m[index].y<m[i].x) { index=i; ans++; } /*else if(m[index].y==m[i].x) { if(i==n-2) { index=i; ans++; } }*/ } cout<<ans<<endl; return 0; }

## Topic C interval coverage

Idea: I used the left boundary sorting for this topic. The purpose is to take the starting point of coordinates as the point to be compared, to compare the left boundary of the interval with the point to be compared, and then to compare the left boundary of each interval with the right boundary of the last interval to be covered + 1. If the left boundary is included, look at the right boundary. If the right boundary is larger than the right boundary of the interval to be covered, Update this interval to a new reserve coverage interval (in a word, to find the interval with the largest right boundary connected with the previous interval), so each interval has three comparative opportunities, the last coverage interval once (to judge whether it is possible to become a reserve), the reserve once (to judge whether it is the best reserve or whether it cannot be covered), and the last one Coverage interval once, to see if it can become a new reserve after coverage interval update.

`

#include<iostream> #include<algorithm> using namespace std; struct quyu{ int x,y; bool operator <(quyu a) { if(x!=a.x) return x<a.x; else return y<a.y; } }; int ans;//Number of intervals taken int index;//Interval limits to be compared int n,t;//Define the number of areas and the end point of the area int in1;//index's reserve quyu a[100000];//Array of storage areas int main() { index=1; ans=1;//Initialize first cin>>n>>t; for(int i=0;i<n;i++) { cin>>a[i].x>>a[i].y; } sort(a,a+n); in1=0; for(int i=0;i<n;i++) { if(a[i].x<=index&&a[i].y>=index)//The right boundary of the previous interval to be compared is in this interval { if(in1<a[i].y+1)//Compare whether the rightmost boundary of the current interval is larger than the rightmost boundary of the previous interval. If it is larger, it will be updated in1=a[i].y+1;//Look for the y-largest interval that can be connected to the previous interval } if(a[i].x>index)//When the current interval breaks away from the rightmost boundary, update the rightmost boundary to index reserve in1 { index=in1;//Now judge this if(a[i].x>index) { cout<<-1<<endl; return 0; } ans++; } if(a[i].x<=index&&a[i].y>=index)//The right boundary of the previous interval to be compared is in this interval { if(in1<a[i].y+1)//Compare whether the rightmost boundary of the current interval is larger than the rightmost boundary of the previous interval. If it is larger, it will be updated in1=a[i].y+1;//Look for the y-largest interval that can be connected to the previous interval } /*else { cout<<-1<<endl; return 0; }*/ if(in1>t) break; } if(in1<=t)//If you don't cover t in the end { cout<<-1<<endl; return 0; } cout<<ans<<endl; }

----------------------------I am the line---------------------------

As you can see, I am the draft box of this melon. The reason why I appear is that this melon has set up a flag in the first blog.

So now let's record my debugging process. Let's first look at the question of vigorously working out miracles. This is a simple point (cross out).

My first code

#include<iostream> #include<string> #include<cstring> #include<sstream> #include<algorithm> #include<iomanip> using namespace std; struct stu { string name; // char score[8]; int num;//Answer questions int sum;//Total time consuming bool operator <(const stu &a)const { if(num!=a.num) return num>a.num; if(sum!=a.sum) return sum<a.sum; int l1=name.length(); int l2=a.name.length(); for(int i=0;i<min(l1,l2);i++) { if(name[i]==a.name[i]) continue; if(name[i]<a.name[i]) return true; if(name[i]>a.name[i]) return false; } if(l1<l2) return true; return false; } //Overloaded comparison operator, easy to sort students output };//Define student structure stu *kaosh=new stu;//Define student structure array int n,m; int main() { cin>>n>>m;//Number of questions and each penalty int i=0;//Student index char s[1000]; //Each student's record of each question while(cin>>s)//Input name { kaosh[i].name=s; kaosh[i].num=0; kaosh[i].sum=0; for(int j=0;j<n;j++)//Input record { cin>>s; int e=0;//Index of the character int l=strlen(s); if(s[e]!='-'&&s[e]!='0')//When a positive number is recorded { int um=0; kaosh[i].num++;//Number of correct questions + 1 //e++; //string ss; / / the string containing the arithmetic //int ee=0; / / index of arithmetic string while(e<l&&s[e]!='(')//Before parentheses are encountered (score before parentheses is calculated) { //ss[ee]=s[e]; //ee++; um=um*10+s[e]-'0'; e++; } //stringstream ssr; //double a; //ssr<<ss; //ssr>>a; kaosh[i].sum=kaosh[i].sum+um; //ee=0; um=0; while(e<l&&s[e]!=')')//After encountering parentheses (calculate the score in parentheses) / if there is no blank after parentheses, do not loop { //ss[ee]=s[e]; //ee++; um=um*10+s[e]-'0'; e++; } kaosh[i].sum=kaosh[i].sum+um*m; } else//When negative or 0 { if(s[e]=='-') { e++; //string ss; / / the string containing the arithmetic //int ee=0; / / index of arithmetic string int um; while(e<l) { //ss[ee]=s[e]; um=um*10+s[e]-'0'; } // stringstream ssr; // double a; // ssr<<ss; // ssr>>a; kaosh[i].sum=kaosh[i].sum+um*m; } } } i++; } int z=i;//Record the number of students sort(kaosh+1,kaosh+z); for(int dd=0;dd<z;dd++) { cout<<left<<setw(10)<<kaosh[dd].name<<" "; cout<<right<<setw(2)<<kaosh[dd].num<<" "<<setw(4)<<kaosh[dd].sum<<endl; } return 0; }

Hiss, there are too many useless notes. Simplify it

#include<iostream> #include<string> #include<cstring> #include<algorithm> #include<iomanip> using namespace std; struct stu { string name; int num;//Answer questions int sum;//Total time consuming bool operator <(const stu &a)const { if(num!=a.num) return num>a.num; if(sum!=a.sum) return sum<a.sum; int l1=name.length(); int l2=a.name.length(); for(int i=0;i<min(l1,l2);i++) { if(name[i]==a.name[i]) continue; if(name[i]<a.name[i]) return true; if(name[i]>a.name[i]) return false; } if(l1<l2) return true; return false; } //Overloaded comparison operator, easy to sort students output };//Define student structure stu *kaosh=new stu;//Define student structure array int n,m; int main() { cin>>n>>m;//Number of questions and each penalty int i=0;//Student index char s[1000]; //Each student's record of each question while(cin>>s)//Input name { kaosh[i].name=s; kaosh[i].num=0; kaosh[i].sum=0; for(int j=0;j<n;j++)//Input record { cin>>s; int e=0;//Index of the character int l=strlen(s); if(s[e]!='-'&&s[e]!='0')//When a positive number is recorded { int um=0; kaosh[i].num++;//Number of correct questions + 1 while(e<l&&s[e]!='(')//Before parentheses are encountered (score before parentheses is calculated) { //ss[ee]=s[e]; //ee++; um=um*10+s[e]-'0'; e++; } kaosh[i].sum=kaosh[i].sum+um; um=0; while(e<l&&s[e]!=')')//After encountering parentheses (calculate the score in parentheses) / if there is no blank after parentheses, do not loop { um=um*10+s[e]-'0'; e++; } kaosh[i].sum=kaosh[i].sum+um*m; } else//When negative or 0 { if(s[e]=='-') { e++; int um; while(e<l) { um=um*10+s[e]-'0'; } kaosh[i].sum=kaosh[i].sum+um*m; } } } i++; } int z=i;//Record the number of students sort(kaosh+1,kaosh+z); for(int dd=0;dd<z;dd++) { cout<<left<<setw(10)<<kaosh[dd].name<<" "; cout<<right<<setw(2)<<kaosh[dd].num<<" "<<setw(4)<<kaosh[dd].sum<<endl; } return 0; }

This is more convenient. Then I added several test points and found that there are several problems in the score calculation module:

1.e. after judging the left bracket, no one was added, which led to um mistaking the left bracket for a number in the calculation, resulting in confusion

2. When calculating the contents in parentheses, e forgets + +, resulting in the loop cannot be terminated

3. The most serious problem, which I found only after running with the test data and made me cry, is that I misunderstood the meaning of the question. If the question is not solved, it can't be regarded as a penalty. However, my simple and innocent is not only calculated, but also puzzled at the beginning to see that the input-9999 output in the test data is 0.

4. The array is too small, and it seems that the dynamic structure array is not suitable, so as soon as I bite my teeth, I have opened 10000 spaces for each of the student's structure and s string, and the space is instantly enriched

After dealing with the calculation of penalty time plate, the program can finally run out of the results, so under my predication, the data is indeed wrong. After eliminating the above proposal understanding error, I locked my eyes on the comparison module, after a series of tests on the heavy-duty function, I cleared his suspicion, so all the impossibilities are eliminated, leaving There is only one truth, that is, you - sort function. Finally, by using the control variable method, sort is transformed into sort(kaosh,kaosh+z) at the cost of discarding a value. By the way, explain my variable name.

Naive you think it's over here? After CE added a header file (previously changed to string.h when I used other functions during debugging), WA staring at the platform fell into deep self denial and self doubt. I open my compiler and look at my scarred code. I can't help but shed tears of regret. In this fuzzy tears, I seem to see a light flashing on my screen, as if a voice rings in my ear: only this time, the next one is not an example, and I don't need to face your code again. Go!

I did it by magic, so

At this time, I know that it is he, a great man named metaphysics, who once came and quietly left after enlightening the confused me. What a big spirit, big to array burst.

Next, I will recall the haggard appearance of the patient once, and the bandage is very painful.

#include<iostream> #include<string.h> #include<string> #include<cstring> #include<algorithm> #include<iomanip> using namespace std; struct stu { string name; int num;//Answer questions int sum;//Total time consuming bool operator <(const stu &a)const { if(num!=a.num) { return num>a.num; } if(sum!=a.sum) { return sum<a.sum; } /* int w; w=strcmp(a.name,name); if(w>0) return true; else return false;*/ int l1=name.length(); int l2=a.name.length(); for(int i=0;i<min(l1,l2);i++) { // strcmp if(name[i]==a.name[i]) continue; if(name[i]<a.name[i]) return true; if(name[i]>a.name[i]) return false; } if(l1<l2) return true; return false; } //Overloaded comparison operator, easy to sort students output }kaosh[10000];//Define student structure //stu *kaosh=new stu; / / define the array of student structures int n,m; int main() { cin>>n>>m;//Number of questions and each penalty int i=0;//Student index char s[100000]; //Each student's record of each question while(cin>>s)//Input name { kaosh[i].name=s; // cout<<kaosh[i].name<<endl; kaosh[i].num=0; kaosh[i].sum=0; for(int j=0;j<n;j++)//Input record { // cout<<"**********"<<endl; cin>>s; int e=0;//Index of the character int l=strlen(s); if(s[e]!='-'&&s[e]!='0')//When a positive number is recorded { // cout<<"&&&&&&&&"<<endl; int um=0; kaosh[i].num++;//Number of correct questions + 1 while(e<l&&s[e]!='(')//Before parentheses are encountered (score before parentheses is calculated) { um=um*10+s[e]-'0'; e++; } // cout<<um<<endl; kaosh[i].sum=kaosh[i].sum+um; // cout<<kaosh[i].sum<<endl; e++; um=0; while(e<l&&s[e]!=')')//After encountering parentheses (calculate the score in parentheses) / if there is no blank after parentheses, do not loop { um=um*10+s[e]-'0'; e++; } // cout<<um<<endl; kaosh[i].sum=kaosh[i].sum+um*m; // cout<<kaosh[i].sum<<" "<<kaosh[i].num<<endl; } /* else//When negative or 0 { if(s[e]=='-') { // cout<<"^^^^^^^^"<<endl; e++; int um=0; while(e<l) { um=um*10+s[e]-'0'; e++; } // cout<<um<<endl; kaosh[i].sum=kaosh[i].sum+um*m; // cout<<kaosh[i].sum<<endl; // cout<<kaosh[i].num<<endl; } }*/ } i++; } int z=i;//Record the number of students // cout<<z<<endl; sort(kaosh,kaosh+z); for(int i=0;i<z;i++) { cout<<std::left<<setw(10)<<kaosh[i].name<<" "; cout<<std::right<<setw(2)<<kaosh[i].num<<" "<<setw(4)<<kaosh[i].sum<<endl; } return 0; }

But now he has recovered:

#include<iostream> #include<string.h> #include<string> #include<cstring> #include<algorithm> #include<iomanip> using namespace std; struct stu { string name; int num;//Answer questions int sum;//Total time consuming bool operator <(const stu &a)const { if(num!=a.num) { return num>a.num; } if(sum!=a.sum) { return sum<a.sum; } int l1=name.length(); int l2=a.name.length(); for(int i=0;i<min(l1,l2);i++) { if(name[i]==a.name[i]) continue; if(name[i]<a.name[i]) return true; if(name[i]>a.name[i]) return false; } if(l1<l2) return true; return false; } //Overloaded comparison operator, easy to sort students output }kaosh[10000];//Define student structure int n,m; int main() { cin>>n>>m;//Number of questions and each penalty int i=0;//Student index char s[100000]; //Each student's record of each question while(cin>>s)//Input name { kaosh[i].name=s; kaosh[i].num=0; kaosh[i].sum=0; for(int j=0;j<n;j++)//Input record { cin>>s; int e=0;//Index of the character int l=strlen(s); if(s[e]!='-'&&s[e]!='0')//When a positive number is recorded { int um=0; kaosh[i].num++;//Number of correct questions + 1 while(e<l&&s[e]!='(')//Before parentheses are encountered (score before parentheses is calculated) { um=um*10+s[e]-'0'; e++; } kaosh[i].sum=kaosh[i].sum+um; e++; um=0; while(e<l&&s[e]!=')')//After encountering parentheses (calculate the score in parentheses) / if there is no blank after parentheses, do not loop { um=um*10+s[e]-'0'; e++; } kaosh[i].sum=kaosh[i].sum+um*m; } } i++; } int z=i;//Record the number of students sort(kaosh,kaosh+z); for(int i=0;i<z;i++) { cout<<std::left<<setw(10)<<kaosh[i].name<<" "; cout<<std::right<<setw(2)<<kaosh[i].num<<" "<<setw(4)<<kaosh[i].sum<<endl; } return 0; }

And then the Ruishen played cards.

I tried to use the linked list at the beginning, I would not put the source code, but I did not debug it for half a day. So I changed my strategy angrily. The array is so delicious.

It's still to build the structure of playing cards, to build the pattern and number conversion function, but after I finished typing the code according to the original idea, I found my first fatal error: the meaning of the licensing question was misunderstood, I naively thought that the order of the licensing started from one person, one person took 13 cards, so I started the licensing cycle is 4 × 13, and the result can be right, it should be hair 13 cycles, four people take one at a time, 13 × 4.

Then when I input the array, I first set two arrays a and b to store two rows of data respectively, and then connect a and b with a c. The result shows that this method is extremely unstable. First, there is a "H" for no reason, and then there is a problem in operation, and changing the size of array a, b and c without affecting the storage directly results in The output is not stable, so we give up decisively and choose to turn c into a transit variable, which refers to different arrays a and b with the release of playing cards. (later, when I learned that my roommates mostly used the strategy of saving while losing, I felt a little sad.)

After that, I encountered new challenges. At first, I put the transformation calls of colors and numbers in the overloaded functions of the structure, which means that I need to perform n^2 transformations when comparing, so I put them in the input module, so the number of transformations is reduced to n.

After that, I met my last fatal mistake. This time, it was related to the understanding of the meaning of the question. As for the output mode, the topic says that there are more blank lines at the end of each group, so I take it for granted that there are more blank lines at the end of each line of output data (I actually think a line of data is a group!!!)

Here is my final version.

#include<iostream> #include<string.h> #include<cstring> #include<algorithm> #include<iomanip> using namespace std; //char * str_bin(char* a, char* b); int suitturn(char suit) { if(suit=='C') return 15; if(suit=='D') return 16; if(suit=='S') return 17; if(suit=='H') return 18; } int numturn(char num) { if(num=='T') return 10; if(num=='J') return 11; if(num=='Q') return 12; if(num=='K') return 13; if(num=='A') return 14; if(num>'1'||num<='9') return num-'0'; } struct node{ char suit;//Decor, C (plum) D (square) S (spade) H (red) char num;//Number of playing cards int x,y; // void turn () //{ // x=suitturn(suit); // y=numturn(num); //} bool operator < ( node &s) { //turn(); //s.turn(); if(x!=s.x) return x<s.x; if(y!=s.y) return y<s.y; } };//Single playing card struct men{ node poker[100];//Playing card array int former;//Store next person's index }man[4]; int main() { char n;//Dealer cin>>n; man[0].former=1; man[1].former=2; man[2].former=3; man[3].former=0;//Labeled index // cout<<n<<endl;; char a[1000]; char b[1000];//Store two rows of cards respectively //cout<<b<<endl; int xu;//Index as man array while(n!='#') { cin>>a>>b;//Enter two lines of cards // Cout < < overall cycle "< endl; if(n=='S') xu=1; else if(n=='W') xu=2; else if(n=='N') xu=3; else xu=0; int s1=0;//Index of string //char c[10000]; //strcat(c,a); //strcat(c,b); // cout<<c<<endl; char c[100]; strcpy(c,a); // cout<<c<<endl; for(int i=0;i<13;i++) { //Cout < < total wheel cycle; // Cout < < output current array < < C < < endl; for(int j=0;j<4;j++) { // Cout < < single cycle < < endl; man[xu].poker[i].suit=c[s1]; man[xu].poker[i].x=suitturn(c[s1]); // cout<<c[s1]<<endl; // cout<<man[xu].poker[i].suit<<endl; s1++; man[xu].poker[i].num=c[s1]; man[xu].poker[i].y=numturn(c[s1]); // cout<<c[s1]<<endl; s1++; if(s1==52) { s1=0; strcpy(c,b); } xu=man[xu].former; // Cout < < end of single cycle "< endl; } //sort(man[xu].poker,man[xu].poker+13); } // Cout < "end of playing card cycle" < endl; for(int i=0;i<4;i++) { sort(man[i].poker,man[i].poker+13); // Cout < output cycle < endl; if(i==0) cout<<"South player:"<<endl; else if(i==1) cout<<"West player:"<<endl; else if(i==2) cout<<"North player:"<<endl; else cout<<"East player:"<<endl; //cout<<endl; cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl; //cout<<endl; for(int j=0;j<13;j++) { cout<<"|"<<man[i].poker[j].num<<" "<<man[i].poker[j].num; } cout<<"|"<<endl; //cout<<endl; for(int j=0;j<13;j++) { cout<<"|"<<" "<<man[i].poker[j].suit<<" "; } cout<<"|"<<endl; //cout<<endl; for(int j=0;j<13;j++) { cout<<"|"<<man[i].poker[j].num<<" "<<man[i].poker[j].num; } cout<<"|"<<endl; //cout<<endl; cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl; //cout<<endl; } cout<<endl; cin>>n; } return 0; }

Finally, I would like to say that the foundation is not solid, the ground is shaking, carelessness is refreshing for a while, debug crematorium.