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;
}