【注】
long long
字母大写变小写+=32
分离整数的各个位数
int main()
{
int a; cin >> a;
while (a>0)
{
cout << a % 10 << " ";
a /= 10;
}
return 0;
}
素数
试除法
bool check(int n)
{
if (n < 2)return false;
for (int i = 2; i <= sqrt(n); i++)
{
if (n % i == 0)
return false;
}
return true;
}
int main()
{
int a, b; cin >> a >> b;
for (int i = a; i <= b; i++)
{
if (check(i))
cout << i << endl;
}
return 0;
}
埃氏筛
原理:每次将素数的倍数全部标记上
const int N = 1e3 + 10;
bool vis[N];//标记数组
int prime[N];//素数表 0是素数 1不是素数
int id = 1;
//埃氏筛--O(nlonglongn)
void E_sieve(int n)
{
//标记 0 1
vis[0] = vis[1] = 1;
for (int i = 2;i<=n; i++)
{
if (vis[i] == 0)//是素数
{
prime[id++] = i;
//筛掉素数i的所有倍数
for (int j = i * i; j <= n; j += i)
{
vis[j] = 1;
}
}
}
}
int main()
{
int n; cin >> n;
E_sieve(n);
for (int i = 1; i < id; i++)
cout << prime[i] << endl;
return 0;
}
完全平方数
int main()
{
int a; cin >> a;
if (sqrt(a) == int(sqrt(a)))
cout << "Y";
else
cout << "N";
return 0;
}
分解质因数
int main()
{
int n; cin >> n;
int i = 2, flag = 0;
cout << n << "=";
while (n != 1)
{
if (n % i == 0)
{
if (flag == 0)
{
cout << i, flag = 1;
}
else
cout << "*" << i;
n /= i;
}
else
i++;
}
return 0;
}
质因数的分解
int main()
{
int n; cin >> n;
for (int i = 2;; i++)
{
if (n % i == 0)
{
cout << n / i;
break;
}
}
return 0;
}
求第n小的质数
bool check(int n)
{
if (n < 2) return false;
for (int i = 2; i <= sqrt(n); i++)
{
if (n % i == 0)
return false;
}
return true;
}
int main()
{
int n; cin >> n;
int cnt = 0;
for (int i = 2;; i++)
{
if (check(i))
{
cnt++;
if (cnt == n)
{
cout << i;
break;
}
}
}
return 0;
}
数组求最大值(第一个元素作为最大小值)
const int N = 1e3 + 10;
int a[N];
int main()
{
int n; cin >> n;
cin >> a[1];
int mx = a[1];
int mxid = 1;
for (int i = 2; i <= n; i++)
{
cin >> a[i];
if (a[i] > mx)
{
mx = a[i];
mxid = i;
}
}
cout << mxid << endl;
return 0;
}
***最长连续相同子序列-双指针版
#include<iostream>
#include<vector>
using namespace std;
int main() {
int n; cin >> n;
vector<int> v;
for (int i = 0; i < n; i++) {
int x; cin >> x;
v.push_back(x);
}
v.push_back(0);
int maxlen = 1, l = 0;
for (int r = 1; r < v.size(); r++) {
if (r == v.size()-1) {
if (r - l > maxlen) maxlen = r - l;
break;
}
if(v[l]!=v[r]){
if (r - l > maxlen) maxlen = r - l;
l = r;
}
}
cout << maxlen << endl;
return 0;
}
整数去重
const int N = 2e4 + 10,M=5e3+10;
int a[N],cnt[M];
int main()
{
int n; cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (cnt[a[i]] == 0)
cout << a[i]<<" ";
cnt[a[i]]++;
}
return 0;
}
矩阵乘法(三层for循环)
const int N = 1e2+10;
int a[N][N], b[N][N],c[N][N];
int main()
{
int n, m, k; cin >> n>>m>>k;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++)
cin >> b[i][j];
//n*k
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= k; j++)
{
for (int k = 1; k <= m; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
cout << c[i][j]<<" ";
}
cout << endl;
}
return 0;
}
字符串排序
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int N = 1e2 + 10;
//字符串数组
string a[N];
int main()
{
int n; cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort(a+1,a+1+n);// 参1--数组首元素地址
for (int i = 1; i <= n; i++)
cout << a[i] << endl;
return 0;
}
线性序列变环
int main()
{
string s1; string s2;
cin >> s1 >> s2;
if (s1.length() < s2.length())
swap(s1, s2);
s1 += s1;
if (s1.find(s2) != -1)
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
判断回文
反转函数
int main()
{
string s,r;
getline(cin, s);
r = s;
reverse(s.begin(), s.end());
if (s == r)
cout << "yes";
else
cout << "no";
return 0;
}
冒泡排序
const int N = 1e2 + 10;
int a[N];
/*冒泡排序*/
int main()
{
int n; cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int j = n; j > 1; j--) {
for (int i = 1; i < j; i++)
{
if (a[i] < a[i + 1])
swap(a[i], a[i + 1]);
}
}
for (int i = 1; i <= n; i++)
{
cout << a[i]<<endl;
}
return 0;
}
选择排序(最小交换次数)
const int N = 1e2 + 10;
int a[N];
int main() {
int n; cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
//默认第i个元素是最小的
int maxId = i;
for (int j = i + 1; j <= n; j++) {
if (a[j] > a[maxId]) {
maxId = j;
}
}
swap(a[i], a[maxId]);
}
for (int i = 1; i <= n; i++) cout << a[i] << endl;
return 0;
}
计数排序
const int N = 1e2 + 10;
int a[N],cnt[1010];
int main()
{
int n; cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
cnt[a[i]]++;
}
for (int i = 1000; i >= 0; i--)
{
while(cnt[i])
{
cout << i << endl;
cnt[i]--;
}
}
return 0;
}
表达式问题--前中后
前缀表达式
double fun()
{
string s; cin >> s;
if (s[0] == '+')return fun() + fun();
else if (s[0] == '-')return fun() - fun();
else if (s[0] == '*')return fun() * fun();
else if (s[0] == '/')return fun() / fun();
else return stod(s);
}
int main()
{
printf("%lf", fun());
return 0;
}
搜索与回溯
abc全排列
#include<iostream>
using namespace std;
const int N = 1e2 + 10;
string s;
bool vis[N];
char cnt[N];
void dfs(int dep)
{
if (dep == s.size())
{
for (int i = 0; i < dep; i++)
cout << cnt[i];
cout << endl;
return ;
}
for (int i = 0; i < s.size(); i++)
{
if (!vis[i])
{
vis[i] = 1;
cnt[dep] = s[i];
dfs(dep + 1);
vis[i] = 0;
}
}
}
int main()
{
cin >> s;
dfs(0);
return 0;
}
组合问题
#include<iostream>
using namespace std;
const int N = 10;
bool vis[N];
int cnt[N];
int n,r;
void dfs(int dep) {
//5.终止条件+结果处理
if (dep == r+1) {
for (int i = 1; i < dep; i++) {
printf("%3d", cnt[i]);
}cout << endl;
return;
}
//1.枚举方案
for (int i = cnt[dep-1]+1; i <= n; i++) {
//2.标记-防止重复搜索
if (!vis[i]) {
//3.搜索
vis[i] = 1;
cnt[dep] = i;
dfs(dep+1);
//4.回溯
vis[i] = 0;
}
}
}
int main() {
cin >> n >> r;;
dfs(1);
return 0;
}
广搜求迷宫问题最短路
char g[N][N];//迷宫数组
bool vis[N][N];//标记数组
int sx, sy, tx, ty;
int dx[] = { 0,0,1,-1 }, dy[] = { 1,-1,0,0 };
int n, m;
struct point { int x, y, step; };
int ans;
void bfs(point s) {
queue<point> q;
q.push(s); vis[s.x][s.y] = 1;
while (!q.empty()) {
point cur = q.front(); q.pop();
if (cur.x == tx && cur.y == ty) {
ans= cur.step - 1 ;
return;
}
for (int i = 0; i < 4; i++) {
int bx = cur.x + dx[i], by = cur.y + dy[i];
if (bx<1 || bx>n || by<1 || by>m) continue;
if (g[bx][by] == '#') continue;
if (vis[bx][by]) continue;
q.push({ bx,by,cur.step + 1 }); vis[bx][by] = 1;
}
}
}
int main() {
while (cin >> n >> m&&n&&m) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> g[i][j];
if (g[i][j] == '@') sx = i, sy = j;
if (g[i][j] == '*') tx = i, ty = j;
}
}
ans = -1;
memset(vis, 0, sizeof vis);
bfs({ sx,sy,1 });
cout << ans << endl;
}
return 0;
}