Fight with algorithms
After a week of algorithm battle, I finally touched the tail of the algorithm (although the algorithm seems to have no tail). Next, I'll summarize my harvest:
1. Review of ranking
Understanding of sorting algorithm:
1. Bucket sorting: you need an array with three variables. Note that the size of the array is the maximum value of the elements you want to save
Basic idea: store the numbers you want to sort in the array, initialize the array so that each element is 0, and then read each number in turn with a loop. Then, the number stored in the array is the number of times this number appears, and finally, output the array in turn according to the number of times.
Points of special attention:
1. Array initialization
2. During the second cycle, remember to judge that the number of times is at least 1, and the number of times is 0 will not be output.
#include<stdio.h> int main() { int a[100]={0},i,j,t;//An array, three variables, and complete initialization (for loop can also be used, but simplify the code) for(i=0;i<5;i++)//If the number of comparisons is unknown, set a variable n { scanf("%d",&t);//Read in data, and the number of times stored in the array is a[t]++; } for(i=0;i<=100;i++)//From small to large, if from large to small, traverse the array in reverse and output it { for(j=1;j<=a[i];j++)//Output the subscript according to the number of times, but note that the subscript must be greater than or equal to 1, not 0 { printf("%d",i); } } return 0; }
2. Bubble sort: one array and three variables are required
Basic principle: compare adjacent elements. If the first element is larger than the second, then exchange (from small to large), and vice versa.
Points to note: it's better than n-1. Pay attention to the problem of array out of bounds
Another point is that the upper and lower are matched. If the array starts with a subscript of 1, then all subscripts must be 1, with an equal sign, and the two-level loop does not subtract one;
If you start from 0, you should start from zero without an equal sign, and the second level cycle minus one (remember first and then understand)
#include<stdio.h> int main() { int a[10],i,j,t,n; scanf("%d",&n);//Enter the number of numbers you want to compare for(i=0;i<n;i++) { scanf("%d",&a[i]);//Read in the numbers } for(i=0;i<n-1;i++)//Compare n-1 times { for(j=0;j<n-i-1;i++)//How many times does this person compare { if(a[j]<a[j+1])//Small right and large left { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } for(i=0;i<n;i++) { printf("%d",a[i]);//Traversal output } return 0; }
3. Quick sort: you need an array and unknowns n, as well as left, right, two pointer things and four variables
#include<stdio.h> int a[101],n; void quicksort(int left,int right) { int i,j,t,temp; if(left>right) return; temp=a[left]; i=left; j=right; while(i!=j) { while(a[j]>=temp&&i<j) { j--; } while(a[i]<=temp&&i<j) { i++; } } if(i<j) { t=a[i]; a[i]=a[j]; a[j]=t; } a[left]=a[i]; a[i]=temp; quicksort(left,i-1); quicksort(j+1,right); return; }
The focus is to learn how to quickly sort calls!
2. Sequence extension (stack, queue, linked list)
Queue: queue is a special linear structure. It only allows deletion at the head of the queue, which is called out of the queue, and insertion at the end of the queue, which is called in the queue (first delete and last in)
Key points: out of team and in team operation
#include<stdio.h> int main() { int a[102]={0,6,3,1,7,5,8,9,2,4},head,tail; head=1; tail=10; while(head<tail) { printf("%d",a[head]);//Print the head of the queue and get out of the queue. If you get out of the queue, point directly to the pointer like thing of the head element + +; head++; a[tail]=a[head]; tail++;//Add the same truth, but point to the end of the line, something similar to a pointer (which can also be understood as the subscript of an array)++ head++ } return 0; }
Stack:
1. Difference between and queue:
Queue: it is a linear table that can only be inserted at one end of the table and deleted at the other end
Stack: it is a linear table that can be inserted and deleted at one end of the table
Queue: first in first out
Stack: first in, last out
Determine the number of palindromes by stack:
#include<stdio.h> #include<string.h> int main() { char a[101],s[101]; int i,len,mid,next,top; gets(a); len=strlen(a); mid=len/2-1; top=0; for(i=0;i<mid;i++) { s[++top]=a[i]; } if(len%2==0) { next=mid+1; } else next=mid+2; for(i=next;i<=len-1;i++) { if(a[i]!=s[top]) break; top--; } if(top==0) { printf("YES"); } else printf("NO"); }
At least remember before you understand
Linked list: it has been summarized before without detailed explanation
Detailed explanation of Luogu good questions:
Xiao Nan has a set of lovely toy villains. They have different occupations.
One day, these toy villains hid Xiao Nan's glasses. Xiaonan found that the toy villains formed a circle. Some of them face inside the circle and some face outside the circle. As shown below:
At this time, singer singer told Xiao Nan a riddle: "the glasses are hidden in the third toy villain from the left, the right, the 11th toy villain and the 22nd toy villain from the left."
Xiao Nan found that the orientation of the toy villain in this puzzle is very key, because the left and right directions of the toy villain facing inward and outward are opposite: the toy villain facing the circle is clockwise on the left and counterclockwise on the right; The toy Figurine facing outside the circle is counterclockwise on the left and clockwise on the right.
Xiao Nan was struggling to identify the toy villain and counted:
Singer singer faces inward, and the 33rd from the left is archer.
Archer Archer faces outward, and the 11th from the right is thinker.
thinkerthinker faces outward, and the 22nd from the left is the writer.
So the glasses are hidden in the writer writer!
Although he successfully recovered his glasses, Xiao Nan was not at ease. If more toy villains hide his glasses next time, or the length of the puzzle is longer, he may not be able to find his glasses. So Xiao Nan wants you to write a program to help him solve similar puzzles. Such a puzzle can be described as:
There are nn a circle of toy figurines. Their occupation and orientation are known. Now the 11th toy villain tells Xiaonan a puzzle containing mm instructions, in which the zz instruction is shaped like "left / right, ss toy villain". You need to output the occupation of the toy villain after counting these instructions in turn.
Start answering:
For topic analysis: first, the villain has two sitting directions. Secondly, there are differences in the direction of transmission from east to west. Therefore, the general idea is to divide it into four categories: forward left, reverse right, forward right and reverse left. In this way, it can be divided into counterclockwise or clockwise rotation to achieve the purpose. In view of the two variables of position and name, the structure is required, The specific codes are as follows:
#include<stdio.h> struct { int id; //Record sitting direction char name[15];//Record name }stu[100000]; int next[][2] = { -1, 1, 1, -1 };//There are four collocations, two are counterclockwise and two are clockwise and counterclockwise int main() { int n, m, i, ans, dir, num; ans = 0; scanf("%d %d", &n, &m);//n stands for several villains for (i = 0; i < n; i++) scanf("%d %s", &stu[i].id, stu[i].name); for (i = 0; i < m; i++) {//m represents direction and number of steps scanf("%d %d", &dir, &num); ans += num * next[stu[ans].id][dir]; if (ans < 0) ans = n - ((-1) * ans) % n; //Convert clockwise to counterclockwise else if (ans >= n) ans %= n; //If it is more than one circle, take the remainder } printf("%s", stu[ans].name); return 0; }
Pure simulation topic, according to the requirements of the topic.
Previously done double disk of Luogu question (p5717)
Title: give the length of three segments a, B, CA, B and C, which are integers not greater than 10000. I'm going to make these three segments into a triangle. What triangle can it be?
If three line segments cannot form a triangle, output Not triangle;
If it is a Right triangle, output Right triangle;
If it is an Acute triangle, output Acute triangle;
If it is an Obtuse triangle, output Obtuse triangle;
If it is an Isosceles triangle, output Isosceles triangle;
If it is an Equilateral triangle, output the Equilateral triangle.
If the triangle meets the above conditions, please output it in the above order and separate it with a newline character.
Tips:
This problem is not very difficult, but it is easy for beginners to judge the wrong conditions and jump out of the loop conditions, and finally can't ac
#include<stdio.h> int main() { int a,b,c,tmp;//Manual sorting, C language scanf("%d%d%d",&a,&b,&c); if(a>b){ tmp=a; a=b; b=tmp; } if(a>c){ tmp=a; a=c; c=tmp; } if(b>c){ tmp=b; b=c; c=tmp; }//a<b<c if(a+b>c){ if(a*a+b*b==c*c)//right angle printf("Right triangle\n"); if(a*a+b*b>c*c)//Acute angle printf("Acute triangle\n"); if(a*a+b*b<c*c)//Obtuse angle printf("Obtuse triangle\n"); if(a==b||a==c||b==c)//Isosceles printf("Isosceles triangle\n"); if(a==b&&b==c)//Equilateral triangle printf("Equilateral triangle\n"); } else printf("Not triangle\n"); return 0; }
Key points of doing questions: it is necessary to judge the size of the three sides well, and pay attention to the nesting of conditional structures when writing questions in the future; Is it possible that the special judgment is implied in the supplementary concentration.
Finally, c language
1. Macro definition
Key points of macro definition:
1. There must be no semicolon when defining;
2. If the macro definition is nested, it must follow the one defined later;
3; The macro definition is just a simple replacement, which is different from typedef
4. Macro definition and # usage
5. Macro definitions can be used to write linked lists