The basics of algorithm brushing (1) - the basic knowledge of algorithms

1. Respond to the input requirements of the algorithm brushing website

1. What should I do if I don't know when the input will end?

for example:

PAT 1002: Read in a positive integer n , calculate the sum of its digits, and write out each digit of the sum in Chinese Pinyin.

You have no idea how long the positive integer you entered is, what should you do?

Hoichi: while …… EOF type

while(scanf("%d",&n)!=EOF){
	……
}

The meaning of the code is: when reading the file, it will keep looping until the end of the file is read. Repeatedly read in n, and execute the contents of the loop body.

scanf is universal, either a string (%s) or a number (%d). If it is just a string, you can also use gets() and getchar().

while(gets(str)!=NULL){
	……
}
char ch=getcahr();
while(ch!='\n'){
        ……
        ch=getchar();
    }

It means that you enter a string of characters into the buffer area (press the Enter key to indicate the end of the input), you write ch=getchar() once, and ch will take a character from the buffer area, as long as the end mark \n is encountered.

2. The simplest structure to know when the input ends:

For example, if the loop needs to be executed T times, it can be written as:

while(T--){
	……
}

2. Getting Started - Getting Started Simulation

Simple simulation, finding elements, and graphic output are too simple to explain.

Let's talk about date processing, base conversion, and string processing.

Date processing : Given two dates, how to find the difference in days between them?

Analysis of ideas : If you follow the general mathematical thinking, you will consider the leap year, 31 days and 28 days in a normal year, etc. If you follow the computer thinking, put a counter, set the carry, let the previous date continue to increase by one, stop when it is equal to the next date, and record The number of times plus one is the number of days between them.

Base Conversion : How a P base number is converted to a Q base number. (P, Q<=10)

*Analysis of ideas: *It is divided into two steps. The first step is to convert the P-base number into a decimal number, and the second step is to convert the decimal number into a Q-base number.

1. Convert P base numbers to decimal numbers

For a decimal number y=d1d2d3d4...dn, it can be written in this form:
wumiaosu

For a P-base number y=d1d2d3d4...dn, it can be written in this form:
insert image description here

And this formula is easy to implement with a loop:

int y=0,product=1;
while(x!=0){
	y=y+(x%10)*product;
	x/=10;
	product=product*P;
}

2. Convert decimal numbers to Q numbers

Divide base remainder code: convert decimal y to Q base

int z[40],num=0;
do{
	z[num++] = y % Q;
	y = y / Q;
} while(y!=0)

String processing : Read in a string of characters to determine whether it is a palindrome.

Analysis of ideas : traverse from subscript 0 to len/2 (for example, 10 numbers, it is good to reach 4, and then traverse the subscript to 01234, such as 9 numbers, it is good to 3, the traversal subscript is 0123, the middle 5 is not considered, and then the latter 6789 corresponds to the front).

Then just judge whether str[i] and str[len-1-i] are equal.

Sort important ideas:

Topic: A primary school has recently received a sponsorship, and intends to use part of it to give scholarships to the top 5 students with excellent grades. At the end of the term, each student has three subjects: Chinese, mathematics, and English. *If two students have the same total score, they will be sorted in descending order of language. If the total score and language are the same, the student with the smaller student number will be ranked first. *According to the above requirements, program to realize its function.

Analysis of ideas: Make good use of the sort function in the algorithm header file, write a cmp function, and use the conditional statement in the function to satisfy the meaning of the question.

#include<iostream>
#include<algorithm>
using namespace std;

struct score {
	int chinese;
	int math;
	int english;
	int num;
};
bool cmp(score x, score y) {//这招niubi
	if (x.chinese + x.math + x.english != y.chinese + y.math + y.english)
		return x.chinese + x.math + x.english > y.chinese + y.math + y.english;
	else {
		if (x.chinese != y.chinese) {
			return  x.chinese > y.chinese;
		}
		else
			return x.num < y.num;
	}
}

int main() {
	score a[1000];
	int N;
	cin >> N;//输入学生个数
	for (int i = 0; i < N; i++) {
		cin >> a[i].chinese >> a[i].math >> a[i].english;//按照语数英的顺序输入成绩
		a[i].num = i;
	}
	sort(a, a + N, cmp);
	for (int i = 0; i < N; i++) {
		cout << a[i].chinese << " " << a[i].math << " " << a[i].english << " " << a[i].num <<" " << a[i].chinese + a[i].math + a[i].english<<endl;
	}
	return 0;
}

Joseph Ring:

Question : [Problem description] There are n cards, recorded as 1, 2, 3, ..., n (n<1000), how should they be arranged so that the first card is 1, and then the two cards are placed at the end in turn; Open the top one, which is exactly 2, and then put the three cards at the end in turn, and open the top one, which is exactly 3; and so on, until the last one is n.
[Input example]
8
[Output example]
1 7 5 2 6 8 4 3
Thought analysis : observe the output example, put two unassigned numbers after 1 and put 2, and then empty three unassigned numbers after Put in 3, then enter the Joseph ring (the remainder of the subscript can be used to realize the cycle), then empty the four unassigned numbers and put in 4..., and so on.

#include<iostream>
using namespace std;

int main() {
	int n; cin >> n; //n<1000
	int arr[1001] = { 0 };
	int pos = 0, count = 3;//pos指向放数的位置,count表示pos进入到下一个pos的步长,比如1在下标1的位置,2在下标4的位置
	for (int i = 0; i <n; i++) {
		arr[pos%n] = i+1;  //约瑟夫环的思想
		if (i == n-1) break;
		int j = 0;
		while (j!=count) {  //该循环意思为经过count个不为零的数。
			pos++;
			if (arr[pos%n] == 0) {
				j++;
			}
		}
		count++;
		
	}

	for (int i = 0; i <n; i++) {
		cout << arr[i] << "  ";
	}
	return 0;
}

Continuing to update...

Guess you like

Origin blog.csdn.net/Dai_sir_man/article/details/120182597