NO.34十六届蓝桥杯备战函数十道练习|max|min|素数|完全数|素数对|素数回文数|真素数(C++)

B2130 简单算术表达式求值 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int calc(int a, char c, int b)
{
    switch(c)
    {
        case '+':
            return a + b;
        case '-':
            return a - b;
        case '*':
            return a * b;
        case '/':
            return a / b;
        case '%':
            return a % b;
    }
    
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int a, b;
    char c;
    cin >> a >> c >> b;
    int r = calc(a, c, b);
    cout << r << endl;
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

int calc(int a, char c, int b)
{
    switch(c)
    {
        case '+':
            return a + b;
        case '-':
            return a - b;
        case '*':
            return a * b;
        case '/':
            return a / b;
        case '%':
            return a % b;
    }
    
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int a, b;
    char c;
    scanf("%d %c%d", &a, &c, &b)
    int r = calc(a, c, b);
    cout << r << endl;
    
    return 0;
}
B2129 最大数 max(x,y,z) - 洛谷
#include <bits/stdc++.h>
using namespace std;

int Max(int x, int y, int z)
{
    int m = (x > y ? x : y);
    m = (m > z ? m : z);
    return m;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int a, b, c;
    cin >> a >> b >> c;
    double m = Max(a, b, c) * 1.0 / (Max(a+b, b, c) * Max(a, b, b+c));
    printf("%.3lf", m);
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

int Max(int x, int y, int z)
{
    int m = max(x, y);
    return max(m, z);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int a, b, c;
    cin >> a >> b >> c;
    double m = Max(a, b, c) * 1.0 / (Max(a+b, b, c) * Max(a, b, b+c));
    printf("%.3lf", m);
    
    return 0;
}
库函数 max 和 min
max

在C++中, max 函数⽤于返回两个值中的较⼤值。它是C++标准库 <algorithm>头⽂件中的⼀个函数。
max 函数可以⽤于各种类型,包括内置类型(如 int 、 double )以及⽤⼾⾃定义类型(如类或结构体),只要这些类型⽀持⽐较操作。

#include <algorithm>  

template <class T>  
const T& max(const T& a, const T& b); //默认⽐较  

template <class T, class Compare>  
const T& max(const T& a, const T& b, Compare comp); //⾃定义⽐较器
  • a: 要⽐较的第⼀个值。
  • b: 要⽐较的第⼆个值。
  • comp (可选): ⾃定义⽐较函数对象或⽐较函数,⽤于确定“较⼤”值的标准。
  • ⽐较函数应当返回⼀个布尔值,表⽰第⼀个参数是否 “⼩于” 第⼆个参数。
    返回 a 和 b 中较⼤的那个值。如果两个值相等,则返回 a
#include <iostream>  
#include <algorithm>  
using namespace std;  

int main()  
{  
	int x = 10;
	int y = 20;  
	int m = max(x, y);  
	cout << "较⼤值是: " << m << endl;  
	
	return 0;  
}
#include <iostream>  
#include <algorithm>  
#include <string>  
bool compareLength(const string &a, const string &b)  
{  
	return a.size() < b.size(); //这⾥必须给⼀个判断⼩于的标准  
}  

int main()  
{  
	string str1 = "apple";  
	string str2 = "banana";  
	string max_str = max(str1, str2, compareLength);  
	cout << "长度更长的字符串是" << max_str << endl;  
	
	return 0;  
}
min

在C++中, min 函数⽤于返回两个值中的较⼩值。它和 max 函数类似,也是在C++标准库
<algorithm> 头⽂件中的⼀个函数。使⽤和max函数⼀_⼀样,只是实现的效果恰好相反。

#include <iostream>  
#include <algorithm>  
#include <string>  
bool compareLength(const string &a, const string &b)  
{  
	return a.size() < b.size(); //这⾥必须给⼀个判断⼩于的标准  
}  
int main()  
{
	string str1 = "apple";  
	string str2 = "banana";  
	string min_str = min(str1, str2, compareLength);  
	cout << "ï度0ï的字符串是" << min_str << endl;  
	
	return 0;  
}  
最低分与最高分之差
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n = 0;
    cin >> n;
    int _max;
    int _min;
    int t = 0;
    cin >> t;
    _max = _min = t;
    for (int i = 1; i < n; i++)
    {
        cin >> t;
        _max = max(_max, t);
        _min = min(_min, t);
    }
    cout << _max - _min << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n = 0;
    cin >> n;
    int _max = 0;
    int _min = 100;
    int t = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> t;
        _max = max(_max, t);
        _min = min(_min, t);
    }
    cout << _max - _min << endl;
    return 0;
}
P5738 【深基7.例4】歌唱比赛 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    int s;
    double top = 0;
    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        int _max = 0;
        int _min = 10;
        int sum = 0;
        for (int j = 0; j < m; j++)
        {
            cin >> s;
            sum += s;
            _max = max(_max, s);
            _min = min(_min, s);
        }
        double r = (sum - _max - _min) * 1.0 / (m - 2);
        top = max(r, top);
    }
    printf("%.2lf\n", top);
    
    return 0;
}
B2127 求正整数 2 和 n 之间的完全数 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int perfect_num(int m)
{
    int sum = 0;
    for (int i = 1; i < m; i++)
    {
        if (m % i == 0)
            sum += i;
    }
    if (sum == m)
        return 1;
    else
        return 0;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    for (int i = 2; i <= n; i++)
    {
        if (perfect_num(i))
        {
            cout << i << endl;
        }
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

bool perfect_num(int m)
{
    int sum = 0;
    for (int i = 1; i < m; i++)
    {
        if (m % i == 0)
            sum += i;
    }
    return sum == m;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    for (int i = 2; i <= n; i++)
    {
        if (perfect_num(i))
        {
            cout << i << endl;
        }
    }
    return 0;
}
B2131 甲流病人初筛 - 洛谷
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    string s;
    float t;
    int k;
    int cnt = 0;
    while (n--)
    {
        cin >> s >> t >> k;
        if (t >= 37.5 && k)
        {
            cout << s << endl;
            cnt++;
        }
    }
    cout << cnt << endl;
    
    return 0;
}
#include <iostream>  
using namespace std;
bool check(float wen, int flag)  
{  
	return (wen >= 37.5 && flag);  
}  
int main()  
{  
	string name;  
	float wen;  
	int flag;  
	int n = 0;  
	int t = 0;  
	cin >> n;  
	while (n--)  
	{  
		cin >> name >> wen >> flag;  
		if (check(wen, flag))  
		{  
			cout << name << endl;  
			t++;  
		}  
	}  
	cout << t << endl;  
	return 0;  
}
B2128 素数个数 - 洛谷
#include <bits/stdc++.h>
using namespace std;

bool is_primer(int m)
{
    if (m <= 1)
        return false;

    for (int i = 2; i * i <= m; i++)
    {
        if (m % i == 0)
            return false;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    int cnt = 0;
    for (int i = 2; i <= n; i++)
    {
        if (is_primer(i))
        {
            cnt++;
        }
    }
    cout << cnt << endl;
    
    return 0;
}
B2132 素数对 - 洛谷
#include <bits/stdc++.h>
using namespace std;

bool is_primer(int m)
{
    if (m <= 1)
        return false;

    for (int i = 2; i * i <= m; i++)
    {
        if (m % i == 0)
            return false;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    int cnt = 0;
    for (int i = 2; i+2 <= n; i++)
    {
        if (is_primer(i) && is_primer(i+2))
        {
            cout << i << " " << i + 2 << endl;
	        cnt++;
        }
    }
    if (cnt == 0)
	    cout << "empty" << endl;
    
    return 0;
}
B2136 素数回文数的个数 - 洛谷
#include <bits/stdc++.h>
using namespace std;

bool is_primer(int m)
{
    if (m <= 1)
        return false;

    for (int i = 2; i * i <= m; i++)
    {
        if (m % i == 0)
            return false;
    }
    return true;
}

bool is_pa(int m)
{
    string t = to_string(m);
    string t1 = t;
    reverse(t1.begin(), t1.end());
    if (t == t1)
        return true;
    else
        return false;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    int cnt = 0;
    for (int i = 11; i <= n; i++)
    {
        if (is_primer(i) && is_pa(i))
        {
            cnt++;
        }
    }
    cout << cnt << endl;
    
    return 0;
}
#include <iostream>  
#include <cmath>  
using namespace std;  
bool is_prime(int n)  
{  
	int j = 0;  
	if (n < 2)  
	return false;  
	for (j = 2; j <= sqrt(n); j++)  
	{  
		if (n % j == 0)  
		return false;  
	}  
	return true;  
}  

int ishuiwen(int n)  
{  
	int tmp = n;  
	int ret = 0;  
	while (tmp)  
	{  
		ret = ret * 10 + tmp % 10;
		tmp /= 10;  
	}  
	return ret == n;
}  

int main()  
{  
	int n = 0;  
	cin >> n;  
	int i = 0;  
	int cnt = 0;  
	for (i = 11; i <= n; i++)  
	{  
		if (is_prime(i) && ishuiwen(i))  
		cnt++;  
	}  
	cout << cnt << endl;  
	return 0;  
}
B2139 区间内的真素数 - 洛谷
#include <bits/stdc++.h>
using namespace std;

bool is_primer(int m)
{
    if (m <= 1)
        return false;

    for (int i = 2; i * i <= m; i++)
    {
        if (m % i == 0)
            return false;
    }
    return true;
}

int res(int m)
{
    int ret = 0;
    while (m)
    {
        ret = ret * 10 + m % 10;
        m /= 10;
    }
    return ret;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int m, n;
    cin >> m >> n;
    int flg = 0;
    for (int i = m; i <= n; i++)
    {
        int t = res(i);
        if (is_primer(i) && is_primer(t))
        {
            if (flg)
                cout << ",";
            cout << i;
            flg = 1;
        }
    }
    if (flg == 0)
        cout << "No" << endl;
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

bool is_primer(int m)
{
    if (m <= 1)
        return false;

    for (int i = 2; i * i <= m; i++)
    {
        if (m % i == 0)
            return false;
    }
    return true;
}

int res(int m)
{
	string s = to_string(m);
	reverse(s.begin(), s.end());
	int ret = stoi(s);
	return ret;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int m, n;
    cin >> m >> n;
    int flg = 0;
    for (int i = m; i <= n; i++)
    {
        int t = res(i);
        if (is_primer(i) && is_primer(t))
        {
            if (flg)
                cout << ",";
            cout << i;
            flg = 1;
        }
    }
    if (flg == 0)
        cout << "No" << endl;
    
    return 0;
}
### 如何在 Windows 系统中安装 PythonQt Designer 并配置环境 #### 安装 PyQt6 和 Qt Designer 为了在 Windows 上使用 Qt Designer,可以通过 `pip` 命令安装最新的 PyQt6 模块。PyQt6 是一个用于创建图形用户界面 (GUI) 应用程序的工具包,并附带了 Qt Designer 工具。 运行以下命令可以完成 PyQt6 及其相关组件的安装: ```bash pip install pyqt6-tools ``` 此命令会自动下载并安装必要的依赖项,其中包括 Qt Designer[^1]。 #### 配置路径以便访问 Qt Designer 安装完成后,通常可以在以下目录找到 Qt Designer 文件(具体位置取决于 Python 解释器的位置): - **对于标准安装**:`C:\Users\<用户名>\AppData\Local\Programs\Python\<版本号>\Lib\site-packages\pyqt6_tools` - 或者通过脚本启动:`python -m pyqt6_designer`. 如果希望直接从文件资源管理器或桌面快捷方式打开 Qt Designer,则需将其可执行文件所在路径添加到系统的环境变量 PATH 中。操作方法如下: 1. 打开控制面板 -> 系统和安全 -> 系统 -> 高级系统设置。 2. 单击“环境变量”,在“系统变量”部分找到名为 “Path”的条目并编辑它。 3. 添加上述提到的设计工具所在的完整路径至列表末尾。 这样处理之后,在任意 CMD 终端窗口输入 designer.exe 就能调用该应用程序。 #### 测试安装成功与否 验证是否正确设置了所有内容的一个简单办法就是尝试加载设计模式本身或者利用 PyQT 创建一个小项目来看看能否正常渲染 UI 元素。下面给出一段简单的例子展示如何载入由设计师保存下来的 .ui 文件并通过 python 运行起来: ```python from PyQt6 import uic import sys from PyQt6.QtWidgets import QApplication, QMainWindow class MyUI(QMainWindow): def __init__(self): super(MyUI,self).__init__() # 加载 ui 文件 uic.loadUi('your_ui_file.ui', self) if __name__ == '__main__': app = QApplication(sys.argv) window = MyUI() window.show() try: sys.exit(app.exec()) except SystemExit: pass ``` 以上代码片段假设存在一个叫做 'your_ui_file.ui' 的文件位于当前工作目录下,它是之前通过 Qt Designer 构建出来的界面布局定义文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值