十进制转换为其他进制:
①
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef struct stack {
int data[1000];
int rear, top;
}*Stack,stack;
Stack Create_stack() {
Stack stack = (Stack)malloc(sizeof(stack));
stack->rear = 0;
stack->top = stack->rear;
}
void Push(Stack stack,int n) {
stack->data[stack->top++] = n;
}
int Pop(Stack stack) {
int n = stack->data[stack->top - 1];
stack->top--;
return n;
}
int Empty(Stack stack) {
if (stack->rear == stack->top)return 1;
else return 0;
}
int main() {
Stack stack = Create_stack();
int m;
long long n;
scanf("%lld%d", &n, &m);
if (n == 0)Push(stack, n);
else
while (n) {
Push(stack, n % m);
n = n / m;
}
while (!Empty(stack)) {
printf("%d ",Pop(stack) );
}
}
②
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void Zh(int n,int m) {
if (n < m) printf("%d",n);
else {
Zh(n / m, m); printf("%d", n % m);
}
}
int main() {
int n, m;
scanf("%d%d", & n, &m);
Zh(n, m);
}
括号匹配:
#define _CRT_SECURE_NO_WARNINGS
#define Init_Max_Size 10
#define Increasement 20
#include<stdio.h>
#include<stdlib.h>
typedef struct stack
{
char data[Init_Max_Size];
int top, rear;
}Stack;
Stack* InitStack() {
Stack *stack= (Stack*)malloc(sizeof(Stack));
stack->rear = 0;
stack->top = 0;
return stack;
}
void Push(Stack* stack,char data) {
stack->data[stack->top++] = data;
}
void Pop(Stack* stack) {
if (!Empty(stack)) stack->top--;
else printf("栈为空,无法出栈!\n");
}
void Print(Stack* stack) {
if (Empty(stack)) printf("栈为空,无法打印!\n");
else
{
for (int i = stack->rear; i < stack->top; i++)
printf("%c\n", stack->data[i]);
}
}
int Empty(Stack* stack) {
if (stack->rear == stack->top) return 1;
else return 0;
}
char GetTop(Stack* stack) {
if (!Empty(stack)) return stack->data[stack->top - 1];
else printf("栈为空,无栈顶元素!\n");
}
void Khpp(char s[]) {
Stack* stack = InitStack();
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == '(') {
Push(stack, s[i]);
}
else if (s[i] == ')' && GetTop(stack) == '(') {
Pop(stack);
}
else {
printf("匹配失败!\n");
return;
}
}
if (Empty(stack)) {
free(stack);
printf("匹配成功!\n");
}
else printf("匹配失败!\n");
}
int main() {
char s[100];
scanf("%s", s);
Khpp(s);
return 0;
}
链式前向星(静态邻接表)存图:
#include<iostream>
using namespace std;
typedef struct edge{
int next, to, cost;
}Edge;
int head[10];
Edge edges[10];
int i;
void Add(int from, int to, int cost) {
edges[++i].next = head[from];
edges[i].to = to;
edges[i].cost = cost;
head[from] = i;
}
int main() {
int n;
cin >> n;
while (n--) {
int from, to, cost;
cin >> from >> to >> cost;
Add(from, to, cost);
}
for ( int j = head[1]; j; j = edges[j].next) {
printf("%d %d\n", edges[j].to, edges[j].cost);
}
}
递归
递归汉诺塔:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void Hnod(int n, char a, char b, char c) {
if (n == 1)printf("%c->%c\n", a, c);
else {
Hnod(n-1,a,c,b);
printf("%c->%c\n", a, c);
Hnod(n - 1, b, a, c);
}
}
int main() {
Hnod(10,'A','B','C');
}
快速排序:
//#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<Algorithm>
#include<time.h>
using namespace std;
int a[100];
void qsort(int l, int r) {
if (l >= r)return;
int i = l, j = r, k = a[(l+r)/2];
while (i <= j) {
while (i <= j && a[j] > k)j--;
while (i <=j && a[i] < k)i++;
if (i > j)break;
swap(a[i++], a[j--]);
}
qsort(l, j); qsort(i, r);
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
srand(time(0));
qsort(1, n);
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
return 0;
}
归并排序:
#include<iostream>
using namespace std;
int a[100], b[100];
void merge_sort(int l, int r) {
if (l >= r)return;
int mid = (l + r) / 2;
merge_sort(l, mid); merge_sort(mid + 1, r);
int i = l, j = mid + 1, k = l;
while (i <= mid && j <= r) {
if (a[i] <= a[j]) {
b[k++] = a[i++];
}
else {
b[k++] = a[j++];
}
}
while (i <= mid) {
b[k++] = a[i++];
}
while (j <= r) {
b[k++] = a[j++];
}
for (int i = l; i <= r; i++)
a[i] = b[i];
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
merge_sort(1, n);
for (int i = 1; i <= n; i++)cout << a[i]<<" ";
return 0;
}
逆序对对个数:
#include<iostream>
using namespace std;
int a[100], b[100],cnt;
void merge_sort(int l, int r) {
if (l >= r)return;
int mid = (l + r) / 2;
merge_sort(l, mid); merge_sort(mid + 1, r);
int i = l, j = mid + 1, k = l;
while (i <= mid && j <= r) {
if (a[i] <= a[j]) {
b[k++] = a[i++];
}
else {
b[k++] = a[j++];
cnt += (mid - i + 1);
}
}
while (i <= mid) {
b[k++] = a[i++];
}
while (j <= r) {
b[k++] = a[j++];
}
for (int i = l; i <= r; i++)
a[i] = b[i];
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
merge_sort(1, n);
for (int i = 1; i <= n; i++)cout << a[i]<<" ";
cout << endl;
cout << cnt;//逆序对个数
return 0;
}
全排列:
#include<iostream>
using namespace std;
int vis[100],pl[100],n;
void DFS(int step) {
if(step==n+1)
{
for(int i=1; i<=n; i++)
cout << pl[i] << ' ';
cout<< endl;
return;
}
else {
for(int i=1; i<=n; i++) {
if(!vis[i])
{
vis[i]=1;
pl[step]=i;
DFS(step+1);
vis[i]=0;
}
}
}
}
int main() {
cin >> n;
DFS(1);
return 0;
}
n皇后问题:
#include<iostream>
using namespace std;
int lie[200], zd[200], fd[100],n,a[200];
bool check(int i, int j) {
if (!lie[j] && !zd[i - j + 100] && !fd[i + j]) return true;
else return false;
}
void dfs(int hang) {
if (hang == n + 1) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (a[i] == j)cout << 1<<" ";
else cout << 0 << " ";
}
cout << endl;
}
cout << "------------------------------------"<<endl;
}
else {
for (int j = 1; j <= n; j++) {
if (check(hang, j)) {
lie[j] = 1;
zd[hang - j + 100] = 1;
fd[hang + j] = 1;
a[hang] = j;
dfs(hang + 1);
lie[j] = 0;
zd[hang - j + 100] = 0;
fd[hang + j] = 0;
}
}
}
}
int main() {
cout << "请输入皇后个数:";
cin >> n;
dfs(1);
}
字符串图:
#include<iostream>
using namespace std;
char map[100][100];
int n, m;
int main() {
cin >> n;
for (int i = 1; i <= n; i++)scanf("%s", map[i] + 1);//存图,行间用空格分开
cout << endl;
for (int i = 1; i <= n; i++)printf("%s\n", map[i] + 1);
}
油田问题:
①
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
char map[100][100];
bool vis[100][100];//标记(x,y)格是否被遍历
int dir[4][2] = { -1,0,1,0,0,-1,0,1 };
int n, m,ans;
bool check(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= n;
}
void dfs(int x, int y) {
for (int i = 0; i < 4; i++) {
int xx = dir[i][0] + x;
int yy = dir[i][1] + y;
if (map[xx][yy] == '@' && !vis[xx][yy] && check(xx, yy)) {
vis[xx][yy] = true;
dfs(xx, yy);//递归遍历(x,y)的上下左右四个格并标记是否为油田
}
}
}
int main() {
cin >> n>>m;
for (int i = 1; i <= n; i++)scanf("%s", map[i]);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (map[i][j] == '@' && !vis[i][j]) {//如果vis[i][j]为true则被遍历过而且为油田,且可能与其他油田相连
vis[i][j] = true;
ans++;
dfs(i, j);
}
}
}
cout << ans;
return 0;
}
②
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
char map[100][100];
bool vis[100][100];//标记(x,y)格是否被遍历
int dir[4][2] = { -1,0,1,0,0,-1,0,1 };
int n, m, k;
inline bool check(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= m;
}
void dfs(int x, int y) {
for (int i = 0; i < 4; i++) {
int xx = dir[i][0] + x;
int yy = dir[i][1] + y;
if (map[xx][yy] == '.' && !vis[xx][yy] && check(xx, yy) && k) {
vis[xx][yy] = true;
dfs(xx, yy);//递归至叶子结点
if (k)k--,map[xx][yy] = 'X';
}
}
}
int main() {
cin >> n >> m>>k;
for (int i = 1; i <= n; i++) scanf("%s", map[i]+1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (map[i][j] == '.' && !vis[i][j] && k) {//如果vis[i][j]为true则被遍历过而且为油田,且可能与其他油田相连
vis[i][j] = true;
dfs(i, j);
}
}
}
for (int i = 1; i <= n; i++) {
cout << map[i]+1 << endl;
}
return 0;
}
城市连通:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
using namespace std;
vector<int>map[100];
bool ans, vis[100];
void dfs(int from, int to) {
if (from == to) {
ans = true;
return;
}
else {
for (int i : map[from]) {
if (!vis[i]) {
vis[i] = true;
dfs(i, to);
}
}
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int from, to;
cin >> from >> to;
map[from].push_back(to);
}
dfs(2, 1);
if (ans == true)cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}
或
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
bool ans, vis[100];
int head[100],cnt;
struct edge {
int next, to;
}edges[100];
void add(int from, int to) {
edges[++cnt].next = head[from];
edges[cnt].to = to;
head[from] = cnt;
}
void dfs(int from, int to) {
if (from == to) {
ans = true;
return;
}
else {
for (int i = head[from]; i; i = edges[i].next) {
if (!vis[i]) {
vis[i] = true;
dfs(i, to);
}
}
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int from, to;
cin >> from >> to;
add(from, to);
}
dfs(4, 1);
if (ans == true) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}
数字转化:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int n,m,step,vis[1000];
bool ans;
void dfs(int step, long long value) {
if (value > m) return;
else if (value == m&&!ans) {
ans = true;
cout << "YES" << endl;
cout << n << ' ';
for (int i = 1; i <= step-1; i++) {
if (vis[i] == 1) {
n = n * 10 + 1;
cout << n << ' ';
}
else {
n *= 2;
cout << n << ' ';
}
}
return;
}
else {
vis[step] = true;//二叉树,vis[]记录分支
dfs(step + 1, value * 10 + 1);
vis[step] = false;
dfs(step + 1, value * 2);
}
}
int main() {
cin >> n >> m;
dfs(1, n);
if (!ans)cout << "NO" << endl;
return 0;
}
树染色次数最少:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
using namespace std;
vector<int>map[20];
int step,color[20];
void dfs(int num, int frontcol) {//frontcol为父节点的颜色
if (color[num] != frontcol)step++;//当父节点颜色与当前节点的颜色不同时step++
for (int i : map[num]) dfs(i, color[num]);
}
int main() {
int n;cin >> n;
for (int i = 2; i <= n; i++) {//读入除根节点外的其他节点的父节点
int from; cin >> from;
map[from].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> color[i];//读入每个节点的颜色,颜色代码1,2
dfs(1, 0);
cout << step << endl;
return 0;
}