10-16
出入栈序列是否合法
//
// Created by 86184 on 2024/10/16.
//
#include <stdio.h>//IIOOOIO
int jude(char s[]) {int count = 0, i = 0;while (s[i] != '\0') {if (s[i] == 'I') count++;else if (s[i] == 'O') count--;else return 0;if (count < 0) return 0;i++;}if (count != 0) {return 0;}return 1;
}int main() {char s[128] = {"IIOOO"};int flag = 0;flag = jude(s);if (flag == 0) {printf("不合法序列");} else {printf("合法序列");}return 0;
}
数字转字符
//
// Created by 86184 on 2024/10/16.
//
#include <stdio.h>//408 ---> "408"
int NumToSt(int n, char s[], int *i) {if (n == 0) return 0;int v;char c;v = n % 10;n /= 10;c = (char) (v + '0');s[*i] = c;*i = *i + 1;return NumToSt(n, s, i);
}int main() {int i = 0, n = 0;char s[128] = {'\0'};scanf("%d", &n);if (n == 0) s[0] = '0';else {NumToSt(n, s, &i);i = i - 1;};while (i > -1) {printf("%c", s[i]);i--;}}
鞍点
//
// Created by 86184 on 2024/10/16.
//
//鞍点
#include <stdio.h>
int main() {int i, j, k, max = 0, Max, flag = 1;int array[3][3] = {{7, 3, 4},{7, 4, 5},{6, 5, 4}};for (i = 0; i < 3; i++) {for (j = 0; j < 3; j++) {if (array[i][j] > array[i][max])max = j;}Max = array[i][max];for (k = 0; k < 3; k++) {if (Max > array[k][max]) {flag = 0;break;}}printf("%d", flag);if (flag == 1)printf("鞍点为:(%d,%d) : %d", i, max, Max);//归位flag = 1;}return 0;
}
10-28
1-n 中1的个数 同源异形体 矩阵局部最值 直接插入排序
//
// Created by 86184 on 2024/10/28.
//
#include <stdio.h>void Q1();void Q2();void Q3();void Q4();void Q5();int main() {int n = 0;scanf("%d", &n);switch (n) {case 1:Q1();break;case 2:Q2();break;case 3:Q3();break;case 4:Q4();break;case 5:Q5();break;default:break;}return 0;
}
//1 - n 中1的个数
void Q1() {int n = 0, count = 0;scanf("%d", &n);for (int i = 1; i <= n; ++i) {int temp = i;while (temp != 0) {if (temp % 10 == 1) count++;temp /= 10;}}printf("%d中有%d个1", n, count);
}
//同源异形体
void Q2() {int array[128] = {0};int n1 = 0, n2 = 0;scanf("%d%d", &n1, &n2);while (n1 != 0) {array[n1 % 10]++;n1 /= 10;}while (n2 != 0) {array[n2 % 10]--;n2 /= 10;}for (int i = 0; i < 10; ++i) {if (array[i] != 0) {printf("不为异形体");return;}}printf("为异形体");
}
//矩阵局部最值
void Q3() {int array[128][128] = {0};int m = 0, n = 0;int i, j;//m行n列scanf("%d%d", &m, &n);for (i = 0; i < m; i++) {for (j = 0; j < n; j++) {scanf("%d", &array[i][j]);}}for (i = 1; i < m - 1; i++) {for (j = 1; j < n - 1; j++) {if (array[i][j] > array[i][j + 1] && array[i][j] > array[i][j - 1]&& array[i][j] > array[i - 1][j] && array[i][j] > array[i + 1][j])printf("array[%d][%d]= %d", i, j, array[i][j]);}}
}
//直接插入排序
void Q4() {int array[128] = {0};int n = 0;int j, i;scanf("%d", &n);for (int i = 1; i <= n; ++i) {scanf("%d", &array[i]);}for (i = 2; i <= n; ++i) {if (array[i] < array[i - 1]) {array[0] = array[i];for (j = i - 1; array[j] > array[0]; j--) {array[j + 1] = array[j];}array[j + 1] = array[0];}}for (i = 1; i <=n; ++i) {printf("%d ", array[i]);}
}//最大公约数
void Q5() {}
10-29
素数 矩阵移动 整数拆分 数字反转 大公约数 公倍数
//
// Created by 86184 on 2024/10/29.
//
#include <stdio.h>
#include <math.h>#define MAXSIZEvoid Q1();void Q2();void Q3();void Q4();void Q5();int dfs(int cur, int sum, int last);int main() {int n = 0;scanf("%d", &n);switch (n) {case 1:Q1();break;case 2:Q2();break;case 3:Q3();break;case 4:Q4();break;case 5:Q5();break;default:break;}return 0;
}//判断素数
void Q1() {int i, j, flag = 0;for (i = 2; i <= 100; i++) {for (j = 2; j < i; ++j)if (i % j == 0) flag = 1;if (flag != 1)printf("%d ", i);flag = 0;}
}//矩阵移动
/*1 2 3 42 3 4 1*/void Q2() {int array[128][128] = {0};int n = 0; //数组长度int i, j;scanf("%d", &n);for (i = 0; i < n; ++i) {for (j = 0; j < n && i == 0; ++j)array[i][j] = j + 1;for (j = 0; j < n && i != 0; ++j)//上一行的每个元素向前移动一位array[i][(j - 1 + n) % n] = array[i - 1][j];}for (i = 0; i < n; ++i) {for (j = 0; j < n; ++j) {printf("%d ", array[i][j]);}putchar('\n');}}//整数拆分
int n; // 待分解的数
int nums[100]; // 存储每个组成部分的数值
int size = 0; // 当前组成部分的数量
int cnt = 0; // 分解方案的数量void Q3() {scanf("%d", &n); // 输入待分解的数nums[0] = 0; // 初始化组成部分,第一个数为0size = 0; // 初始化组成部分数量dfs(n, 0, 1); // 开始分解printf("%d", cnt); // 输出分解方案数量
}int dfs(int cur, int sum, int last) {if (cur == 0 && sum == n) {cnt++;printf("%d = %d ", n, nums[0]);for (int j = 1; j < size; ++j) {printf("+ %d ", nums[j]);}printf("\n");} else {for (int i = 1; i <= cur; i++) {if (i >= last && sum + i <= n && i < n) {nums[size] = i;size++;dfs(cur - i, sum + i, i);size--;}}}
}//数字反转
void Q4() {int x = 0, n = 0, sum = 0;scanf("%d", &n);while (n > 0) {x = n % 10;n /= 10;sum = sum * 10 + x;}printf("%d", sum);
}//最大公约数 公倍数
void Q5() {int a, b, t, c, m, n;scanf("%d%d", &a, &b);m = a;n = b;if (a < b) {t = b;b = a;a = t;}while (b != 0) {c = a % b;a = b;b = c;}printf("最大公约数为:%d\n", a);printf("最大公倍数为:%d", m * n / a);
}//质因数分解:
//
//32 = 2 × 2 × 2 × 2 × 2
//40 = 2 × 2 × 2 × 5
//求最大公约数:
//
//最大公约数是两个数的公有质因数的乘积。在这里,32和40的公有质因数是2、2和2,
//因此最大公约数 = 2 × 2 × 2 = 812。
//求最小公倍数:
//
//最小公倍数是两个数的所有质因数的乘积,包括独有的质因数。在这里,
//32独有的质因数是2 × 2,40独有的质因数是5,
//因此最小公倍数 = 2 × 2 × 2 × 2 × 2 × 5 = 160
10-30
磁盘读写 判断素数 冒泡
//
// Created by 86184 on 2024/10/30.
//
#include <stdio.h>typedef struct Book {char BookName[128];char Editor[32];double Price;char Date[32];int number;
} Book;void Q1();void Q2();void Q3();void Q4();int Q5();int main() {int n = 0;scanf("%d", &n);switch (n) {case 1:Q1();break;case 2:Q2();break;case 3:Q3();break;case 4:Q4();break;case 5:Q5();break;default:break;}return 0;
}
//磁盘读写
void Q1() {FILE *p, *rp;Book data[128];int i, N;scanf("%d", &N);if (((p = fopen("C:\\Users\\86184\\Desktop\\code_22\\test\\in.text", "rb")) == NULL) ||((rp = fopen("C:\\Users\\86184\\Desktop\\code_22\\test\\out.text", "wb")) == NULL)) {printf("Error!");return;}for (i = 0; i < N; ++i) {fread(&data[i], sizeof(data[0]), 1, p);if (data[i].Editor == "HanMeiMei")if (fwrite(&data[i], sizeof(data[0]), 1, rp) != 1)printf("error");printf("%s %s %f %s %d\n",data[i].BookName, data[i].Editor,data[i].Price, data[i].Date, data[i].number);}fclose(p);fclose(rp);
}void Q2() {int array[128] = {1, 9, 5, 4, 2};Q3(array, 5);
}//冒泡排序
void Q3(int array[], int n) {int i, j, flag, temp;for (i = n - 1; i > 0; i--) {flag = 0;for (j = 0; j < i; ++j) {if (array[j + 1] < array[j]) {temp = array[j + 1];array[j + 1] = array[j];array[j] = temp;flag = 1;}}if (flag == 0) break;}for (i = 0; i < n; ++i) {printf("%d ", array[i]);}
}//整数拆分素数
void Q4() {int n, flag = 0;scanf("%d", &n);for (int i = 2; i < n; ++i) {if (Q5(i) == 1)if (Q5(n - i) == 1) {printf("%d = %d + %d\n", n, i, n - i);flag = 1;}}if (flag == 0) printf("不可以拆分为两个素数");
}int Q5(int n) {int isPrime = 1;for (int i = 2; i < n / 2; ++i) {if (n % i == 0) {isPrime = 0;break;}}return isPrime;
}
10-31
字符串排序
//
// Created by 86184 on 2024/10/31.
//
#include <stdio.h>
#include <string.h>
#include <malloc.h>void string_sort(char *s[], int n) {int i, j, min;char *temp;for (i = 0; i < n - 1; ++i) {min = i;for (j = i + 1; j < n; ++j) {if (strcmp(s[min], s[j]) > 0)min = j;}if (min != i) {temp = s[min];s[min] = s[i];s[i] = temp;}}
}void print(char *s[], int n) {for (int i = 0; i < n; ++i) {printf("%s ", s[i]);}
}void Q1() {char *s[128];char S[128];int n = 3;scanf("%d", &n);for (int i = 0; i < n; ++i) {s[i] = (char *) malloc(sizeof(char[128]));scanf("%s", s[i]);}string_sort(s, n);print(s, n);
}
int main() {Q1();return 0;
}
11-1
常用函数
//判断素数
int CheckPrime(int n) {int isPrime = 1;for (int i = 2; i < n / 2; ++i) {if (n % 2 == 0) {isPrime = 0;break;}}return isPrime;
}//第n个斐波那契值
int Fibonacci(int n) {if (n == 1 || n == 2) {return 1;} else {int f1 = 1, f2 = 1, f3;for (int i = 3; i <= n; ++i) {f3 = f1 + f2;f1 = f2;f2 = f3;}return f3;}
}//字符转数字
int CharToNum(char c) {if (c >= 'a' && c <= 'z')return c - 'a' + 1;else if (c >= 'A' && c <= 'Z') {return c - 'A' + 1;} else {return c - '0';}
}//数字转字符
char NumToChar(int n) {if (n >= 0 && n <= 9) {return n + '0';}// 如果 n 是小写字母的 ASCII 值(97-122)else if (n >= 97 && n <= 122) {return n;}// 如果 n 是大写字母的 ASCII 值(65-90)else if (n >= 65 && n <= 90) {return n;}// 其他情况,返回默认值或错误标识符else {return '?'; // 可以选择返回一个特定的错误字符}
}
删除子串
//
// Created by 86184 on 2024/11/1.
//
#include <stdio.h>
#include <string.h>int Index(char s[], char t[]) {int i = 0, j = 0;int l1 = strlen(s);int l2 = strlen(t);while (i < l1 && j < l2) {if (s[i] == t[j]) {i++;j++;} else {i = i - j + 1;j = 0;}}if (j >= l2) return i - j;else return -1;
}void delete(char s[], int index, int lt) {while (lt > 0) {for (int i = index; i < strlen(s); ++i) {s[i] = s[i + 1];}lt--;}
}
//a b c d
//0 1 2 3
//a c d
//a d
//b cint main() {char s[128] = "Tomcat is a male ccatat";char t[] = "cat";int index = 0;index = Index(s, t);while (1) {if (index == -1) break;
// printf("%d\n", index);delete(s, index, strlen(t));printf("%s ", s);index = Index(s, t);}return 0;
}
//3
//Tom is a male ccatat 15
//Tom is a male cat 14
//Tom is a male
11-3
扑克牌
//
// Created by 86184 on 2024/11/3.
//
#include <stdio.h>
#include <stdlib.h>typedef struct Card {int type; //0--红桃 1--方砖 2--黑桃 3--梅花 4--小王 5--大王int point;//1--12 小王--13 大王--14
} Card;int Compare(struct Card c1, struct Card c2) {if (c1.point > c2.point) return 1;else if (c1.point < c2.point) return -1;else {if (c1.type > c2.type) return 1;elsereturn -1;}
}void Swap(struct Card *c1, struct Card *c2) {Card temp;temp = *c1;*c1 = *c2;*c2 = temp;
}void Sort(struct Card c[], int n) {int flag = 0;for (int i = n - 1; i > 0; i--) {flag = 1;for (int j = 0; j < i; j++) {if (Compare(c[j], c[j + 1]) == 1) {Swap(&c[j], &c[j + 1]);flag = 0;}}if (flag == 1) break;}
}void Print(struct Card c[], int n) {for (int i = 0; i < n; ++i) {char *type = (char *) malloc(sizeof(char) * 128);switch (c[i].type) {case 0:type = "红桃";break;case 1:type = "方砖";break;case 2:type = "黑桃";break;case 3:type = "梅花";break;case 4:type = "小王";break;case 5:type = "大王";break;}if (c[i].point == 13)printf("%s\n", "小王");else if (c[i].point == 14)printf("%s\n", "大王");elseprintf("%s:%d\n", type, c[i].point);}
}int main() {struct Card c[128] = {{1, 1},{2, 10},{2, 6},{3, 6},{4, 13},{4, 14}};Sort(c, 6);Print(c, 6);return 0;
}
奇偶交替
//
// Created by 86184 on 2024/11/3.
//
#include <stdio.h>void JudgeNum(int n) {int flag = 1;//2321//12321int temp = n;while (temp > 10) {if (temp % 10 == temp / 10 % 10) {flag = 0;break;}temp /= 10;}if (flag == 1) printf("%d是奇偶交替数!\n", n);if (flag == 0) printf("%d不是奇偶交替数!\n", n);
}int main() {JudgeNum(12321);JudgeNum(2321);JudgeNum(232);JudgeNum(22);
}