Elementary school group algorithm competition must master the knowledge points

Elementary school group algorithm competition must master the knowledge points

Basic framework of the program
#include <bits/stdc++.h>
using namespace std;

int main() {
    
    
	freopen("file.in", "r", stdin);
	freopen("file.out", "w", stdout);


	return 0;
}

Find the best value
	int n, x;
	//最大值和最小值变量一定要初始化 
	int minn = 100, maxx = 0;
	for (int i = 1; i <= n; i ++) {
    
    
		cin >> x;
		minn = min(minn, x);
		maxx = max(maxx, x);
	}
Find the best value and record the position
	int n, x;
	//最大值和最小值变量一定要初始化 
	int minn_pos, minn = 100;
	int maxx_pos, maxx = 0;
	for (int i = 1; i <= n; i ++) {
    
    
		cin >> x;
		
		if (x < minn) {
    
    
			minn = x;
			minn_pos = i;
		}
		
		if (x > maxx) {
    
    
			maxx = x;
			maxx_pos = i;
		}
	}
String input and case conversion
	string s;
	getline(cin, s);

	for (int i = 0; i < s.size(); i ++) {
    
    
		if (s[i] > 'A' && s[i] < 'Z') {
    
    
			s[i] += 32;
		}
		else if (s[i] > 'a' && s[i] < 'z') {
    
    
			s[i] -= 32;
		}
	}
	
	cout << s;
Base conversion (string -> decimal)
	//字符串s表示一个m进制正整数,将其转换成十进制数 
	string s;
	int m;
	cin >> s >> m;
	
	int ans = 0;
	for (int i = 0; i < s.size(); i ++){
    
    
		if (s[i] >= 'A') {
    
    
			ans = ans * m + (s[i] - 'A' + 10);
		}
		else {
    
    
			ans = ans * m + (s[i] - '0');
		}
	}
	cout << ans;	
Base conversion (decimal -> string)
	//n是一个十进进制正整数,将其转换成m进制(字符串)
	int n, m;
	cin >> n >> m;
	
	int a[50], len = 0;
	while (n > 0) {
    
    
		a[++len] = n % m;
		n /= m;
	}
	
	for (int i = len; i >= 1; i --) {
    
    
		if (a[i] >= 10) {
    
    
			cout << char(a[i] - 10 + 'A');
		}
		else {
    
    
			cout << a[i];
		}
	}
Judging the prime number
bool isPrime(int x) {
    
    
	if (x < 2) return false;
	
	for (int i = 2; i <= x/i; i ++) {
    
    
		if (x%i == 0) return false;
	}
	
	return true;
}
Find the greatest common divisor
int gcd(int a, int b) {
    
    
	if (b == 0) return a;
	return gcd(b, a%b);
}
Judge palindrome
bool huiwen(string s) {
    
    
	for (int i = 0, j = s.size()-1; i < j; i ++, j --) {
    
    
		if (s[i] != s[j]) return false;
	}
	
	return true;
}
Array sort
const int N = 1010;
int n, a[N];

bool cmp(int x, int y) {
    
    
	return x > y;
}

int main() {
    
    
	cin >> n;
	
	for (int i = 1; i <= n; i ++) cin >> a[i];
	
	//从小到大排序 
	sort(a+1, a+1+n);
	
	//从大到小排序
	sort(a+1, a+1+n, cmp);
	
	return 0;
}
Structure ordering
const int N = 1010;
int n, a[N];

struct info{
    
    
	int ttl, math;
}a[N];

bool cmp(info x, info y) {
    
    
	if (x.ttl > y.ttl) return true;
	
	if (x.ttl == y.ttl) {
    
    
		if (x.math > y.math) return true;
	}
	
	return false;
}

int main() {
    
    
	cin >> n;
	
	for (int i = 1; i <= n; i ++) {
    
    
		cin >> a[i].ttl >> a[i].math;
	} 
	
	//总分高的排在前面;如果总分相同,则数学成绩高的排在前面 
	sort(a+1, a+1+n, cmp);
	
	return 0;
}

Guess you like

Origin blog.csdn.net/davidliule/article/details/110523701