兰盾模拟卷代码
luffy 12/9/2024 考研代码题
# 模拟卷 1
# 单链表插入删除查找
太简单了就不赘述了
# C++密文加密
#include <iostream>
#include <string>
using namespace std;
class CaesarCipher{
private:
string plaintext;//明文
int key;//偏移量
string ciphertext;//密文
public:
CaesarCipher(string text,int k):plaintext(text),key(k){
encrypt();
}
~CaesarCipher(){
}
void encrypt(){
ciphertext = "";//初始化
for(char c:plaintext){
if(isalpha(c)){
char shift = isupper(c)?'A':'a';
c = (c-shift+key)%26;
if(c<0)c+=26; //负值处理
c+=shift;
}
ciphertext+=c;
}
}
//获取密文
string getCiphertext(){
return ciphertext;
}
};
int main(){
string text;
int key;
cout<<"请输入明文:";
getLine(cin,text);//获取用户输入明文
cout<<"输入偏移量:"
cin>>key;
CaesarCipher cipher(text,key);
cout<<"Ciphertext:"<<cipher.getCiphertext()<<endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 销售文件操作
兰盾代码有点复杂,不如我这个
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char id[50];
char name[100];
int saleNum;
double totalAmount;
}Product;
int main(){
FILE *file1 = fopen("sales_data.csv","r");
FILE *file2 = fopen("product_tatols.csv","w");
if(file1 == NULL || file2 == NULL){
ferror("error");
exit(1);
}
Product product;
while(fscanf(file1,"%s,%s,%d,%lf",product.id,product.name,&product.saleNum,&product.totalAmount)!=EOF){
fprintf(file1,"%s,%s,%d,%lf",product.id,product.name,product.saleNum,product.totalAmount);
}
fclose(file1);
fclose(file2);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 股票利润问题
int maxProfit(int prices[],int n){
if(n==0){
return 0; //没有股票数据,利润0
}
int buy[n]; //买入最大利润
int sell[n]; //卖出最大利润
buy[0] = -prices[0]; //初始化第一天买入利润
sell[0] = 0;
for(int i=1;i<n;i++){
//不买入 >买入 最大利润?
buy[i] = buy[i-1]>(sell[i-1]-prices[i])?buy[i-1]:(sell[i-1]-prices[i]);
// 不卖出 > 卖出 最大利润?
sell[i] = sell[i-1]>(buy[i-1]+prices[i])?buy[i-1]:(buy[i-1]+prices[i]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 模拟卷 2
# 打印菱形
#include <stdio.h>
void printDimond(int n){
int i,j;
for(i=1;i<=n;i++){ //上半部分
for(j=1;j<=n-i;j++){
printf(" ");
}
for(j=1;j<=2*i-1;j++){
printf("*");
}
printf("\n");
}
for(i=n-1;i>=1;i--){
for(j=1;j<=n-i;j++){
printf(" ");
}
for(j=1;j<=2*i-1;j++){
printf("*");
}
printf("\n");
}
}
int main(){
int n;
printf("请输入菱形边长:");
scanf("%d",&n);
printDimond(n);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 对称素数
#include <stdio.h>
#include <math.h>
#include <string.h>
bool is_prime(int num){
if(num<=1){
return false;
}
if(num == 2){
return true;
}
if(num %2 == 0){
return false;
}
for(int i=3;i<=sqrt(num);i+=2){
if(num % i == 0){
return false;
}
}
return true;
}
bool is_palindrome(int num){ //判断是否对称
char str[20];
sprintf(str,"%d",num); //数字转到字符串
int len = strlen(str);
for(int i=0;i<len/2;i++){
if(str[i] != str[len-i-1]){
return false;
}
}
return true;
}
int main(){
int a,b;
printf("请输入区间[a,b]");
scanf("%d%d",&a,&b);
printf("区间ab之间的对称素数有:\n");
for(int i=a;i<=b;i++){
if(is_prime(i)&&is_palindrome(i)){
printf("%d\n",i);
}
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 判断对称二叉树
#include <stdio.h>
#include <string.h>
#include <math.h>
typedef struct TreeNode{
char data;
struct TreeNode *left;
struct TreeNode *right;
}TreeNode;
int isSymmetricHelper(TreeNode *left,TreeNode *right){
if(left == NULL && right == NULL){
return 1;
}
if(left == NULL || right == NULL){
return 0;
}
return isSymmetricHelper(left->left,righ->right) && isSymmetricHelper(left->right,right->left;)
}
int isSymmetric(TreeNode *root){
if(root==NULL){
return 1;
}
return isSymmetric(root->left,root->right);
}
int main(){
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 子序列问题
不是很懂没写有空可以看看
# 模拟卷 3
今天 11 月 13 号,写了第三套 828 模拟卷
第一个也是很棒棒啊!首当其冲就是单链表递归合并
递归递归递归,你最好考试也考递归
话不多说开始码代码
# 单链表递归合并
#include <stdio.h>
//一马当先把数据结构码上
struct ListNode {
int val;
struct LinkNode *next;
}
struct ListNode* mergeTwoLists(struct ListNode *l1,struct ListNode *l2){
if(l1 == NULL){ //如果l1为空 当然是返回l2作为合并的结果啦
return l2;
}
if(l2 == NULL){ //同理
return l1;
}
struct ListNode *head;
if(l1->val < l2->val){
head = l1;
head->next = mergeTwoLists(l1->next,l2);
}else{
head = l2;
head->next; = mergeTwoLists(l1,l2->next);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# C++大题代码,重载运算符直接不会开始抄答案
#include <iostream>
#include <cmath>
using namespace std;
class Point{
private:
double x,y;
public:
Point(double x=0.0,double y=0.0):x(x),y(y){}
//重载 + 运算符
Point operator+(const Point & other)const{
return Point(x+other.x,y+other.y);
}
//重载 - 运算符 计算两点距离
double operator-(const Point & other)const{
return sqrt(pow(x-other.x,2)+pow(y-other.y,2));
}
//重载 <<
friend ostream& operator<<(ostream &out,const Point &p){
out<<"("<<p.x<<","<<p.y<<")";
return out;
}
//重载 >>
friend ostream& operator<<(istream &in,Point &p){
in>>p.x>>p.y;
return in;
}
}
int main(){
Point p1,p2;
cout<<"输入第一个点(x,y):";
cin>>p1;
cout<<"输入第二个点(x,y):"
cin>>p2;
cout<<"Point1:"<<p1<<endl;
cout<<"Point1:"<<p1<<endl;
//平移
Point p3 = p1+p2;
cout<<"Point3:"<<p3<<endl;
//计算距离
double distance = p1-p2;
cout<<"两点的距离:"<<distance<<endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 最大公约数
int gcd(int m,int n){
if(n==0){
printf("%d\n",m);
return m;
}else{
int temp = m%n;
printf("%d%d\n",n,temp);
return gcd(n,temp);
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 是否平衡二叉树
#include <stdio.h>
#include <stdlib.h>
struct TreeNode{
int val;
struct TreeNode *left;
struct TreeNode *right;
}
int isBalancedUtil(struct TreeNode *root){ //用于递归计算树的高度并检查是否平衡
if(root == NULL) return 0;
// 递归计算左右子树高度
int left = isBalancedUtil(root->left);
int right = isBalancedUtil(root->right);
// 计算左右子树高度差
int num = left>right?(left-right):(right-left);
// 如果左右子树高度差超过 1,或者左右子树本身不平衡,返回 -1
if(left == -1 || right == -1 || num >1){
return -1;
}
// 返回当前子树的高度(较大子树高度 +1)
return (left > right ? left : right) + 1;
}
bool isBalanced(struct TreeNode *root){
if(root == NULL) return true;
return isBalancedUtil(root) !=-1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 背包问题
#include <stdio.h>
#include <string.h>
#define MAX_ITEMS 100
#define MAX_CAPACITY 1000
int dp[MAX_ITEMS+1][MAX_CAPACITY+1] = {0};
//n物品数量、W背包容量
int knapsack(int weights[], int values[], int n, int W) {
for(int i=0;i<=n;i++){
for(int j=0;j<W;j++){
if(i==0||j==0){
dp[i][j]=0;
}else if(weights[i-1]<=j){
//values[i-1]+dp[i-1][j-weights[i-1]] >dp[i-1][j] 当前的物品价值+背包容量减去当前物品容量的最大价值
//dp[i-1][j] 没装当前物品的最大价值
dp[i][j] = (values[i-1]+dp[i-1][j-weights[i-1]] >dp[i-1][j])
?values[i-1]+dp[i-1][j-weights[i-1]]
:dp[i-1][j];
}else{
dp[i][j] = dp[i-1][j];
}
}
}
}
int main() {
int weights[] = {2, 3, 4, 5};
int values[] = {3, 4, 5, 6};
int n = 4; // 物品数量
int W = 8; // 背包容量
printf("最大价值: %d\n", knapsack(weights, values, n, W));
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 模拟卷 4
# 账户金额
#include <iostream>
class Account{
private:
int money;
public:
Account(){};
Account(int money):money(money){};
void save(){
int saveMoney;
printf("请输入要存入的金额:");
scanf("%d",&saveMoney);
money += saveMoney;
}
void qu(){
int quMoney;
printf("请输入要取出的金额:");
scanf("%d",&quMoney);
if(money>quMoney) {
money-=quMoney;
}else{
printf("取款金额无效或余额不足\n");
}
}
void search(){
printf("您余额:%d\n",money);
}
};
int main(){
Account user(0);
int num;
printf("请选择操作:1存款、2取款、3查看余额、4退出");
scanf("%d",&num);
while(num != 4){
switch(num){
case 1:
user.save();break;
case 2:
user.qu();break;
case 3:
user.search();break;
}
printf("请选择操作:1存款、2取款、3查看余额、4退出");
scanf("%d",&num);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 二叉树各个层次权值累加最大值,相同返回层次深度小的
#include <stdio.h>
#include <math.h>
#define mansize 1000
void readInput(int *n,int values[]){
scanf("%d",n);
for(int i=0;i<*n;i++){
scanf("%d",&values[i]);
}
}
void calculateDepathSum(int n,int values[],int depthSum[],int *maxDepth){
for(int i=0;i<maxsize;i++){
depthSum[i] = 0;
}
for(int i=0;i<n;i++){
int depth =
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 计算所有学生人数、求指定班级平均分
//孩子兄弟表示法
typedef struct node{
char name[50];
float score;
struct node *child;
struct node *brother;
}
// 计算所有学生人数
int countStudent(TNode *node){
if(node == NULL){
return 0;
}
int count = 0;
while(node != NULL){
cout+=1;
count += countStudent(node->child);
node = node->brother;
}
}
//求指定班级平均分
#include <stdio.h>
#include <string.h>
// 定义树节点结构
typedef struct node {
char name[50]; // 名字:专业、班级或学生名
float score; // 分数(仅学生节点有分数)
struct node *child; // 指向第一个孩子
struct node *brother; // 指向下一个兄弟
} TNode;
// 统计所有学生人数(递归遍历)
int countStudents(TNode *root) {
if (root == NULL) return 0;
int count = 0;
// 当前节点是学生节点(没有子节点)
if (root->child == NULL) {
count = 1;
}
// 递归统计孩子和兄弟节点
return count + countStudents(root->child) + countStudents(root->brother);
}
// 查找指定班级并计算平均分
float averageScore(TNode *root, const char *className) {
if (root == NULL) return 0;
// 找到目标班级
if (strcmp(root->name, className) == 0) {
TNode *student = root->child; // 第一个学生节点
int count = 0;
float totalScore = 0;
// 遍历该班级所有学生
while (student != NULL) {
totalScore += student->score;
count++;
student = student->brother; // 访问下一个学生
}
return count > 0 ? totalScore / count : 0; // 计算平均分
}
// 在子树和兄弟中递归寻找班级
float result = averageScore(root->child, className);
if (result != 0) return result;
return averageScore(root->brother, className);
}
int main() {
// 构建测试树
TNode student1 = {"Alice", 85, NULL, NULL};
TNode student2 = {"Bob", 90, NULL, NULL};
TNode student3 = {"Charlie", 78, NULL, NULL};
TNode class1 = {"Class1", 0, &student1, NULL};
student1.brother = &student2;
student2.brother = &student3;
TNode root = {"CS", 0, &class1, NULL};
// 统计所有学生人数
printf("Total students: %d\n", countStudents(&root));
// 计算指定班级的平均分
printf("Average score of Class1: %.2f\n", averageScore(&root, "Class1"));
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 模拟卷五
# 候选人
#include <stdio.h>
int main() {
int m, n;
printf("请输入候选人数量(0 < m < 1000)和学生数量(0 < n < 3000):");
scanf("%d %d", &m, &n);
if (m <= 0 || m >= 1000 || n <= 0 || n >= 3000) {
printf("输入的候选人数量或学生数量超出范围!\n");
return 1; // 退出程序
}
int data[m]; // 声明长度为m的数组
for (int i = 0; i < m; i++) {
data[i] = 0; // 初始化数组
}
int vote;
for (int i = 0; i < n; i++) {
printf("请输入候选人编号(1 ~ %d):", m);
scanf("%d", &vote);
if (vote < 1 || vote > m) {
printf("无效的候选人编号!\n");
continue; // 跳过非法输入
}
data[vote - 1]++; // 将票数加在对应位置
}
int max = 0;
int maxIndex = 0;
for (int i = 0; i < m; i++) {
if (data[i] > max) {
max = data[i];
maxIndex = i;
}
}
printf("候选人编号:%d\n", maxIndex + 1);
printf("得票数:%d\n", max);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# C++程序 Vehicle 基类继承重载调用,虚函数
#include <iostream>
using namespace std;
class Vehicle{
private:
int m;
public:
virtual void move()=0;
virtual void stop()=0;
virtual ~Vehicle(){};
}
class Car:public Vehicle{
public:
void move() const{
cout<<"车辆移动"<<endl;
}
void stop(){
cout<<"车辆停止移动"<<endl;
}
}
class Bicycle:public Vehicle{
public:
void move(){
cout<<"自行车移动"<<endl;
}
void stop(){
cout<<"自行车停止移动"<<endl;
}
}
void simulateMovement(Vehicle *vehicles[],size){
for(int i=0;i<size;i++){
vehicles[i]->move();
vehicles[i]->stop();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 二叉链表寻找双亲结点
// 自己写的
typedef struct BNode{
int data;
struct BNode *parent;
struct BNode *left;
struct BNode *right;
}BNode,BiTree;
void setParent(BiTree T,BiTNode *parent){
if(T == NULL){
return ;
}
T->parent = parent;
setParent(T->left,T);
setParent(T->right,T);
}
setParent(T,NULL);
void searchParent(BiTree T,int x,BiTree *&p){
if(T == NULL){
p = NULL;
return ;
}
if(T->data == x){
p = T->parent;
return ;
}
searchParent(T->left,x,p);
searchParent(T->right,x,p);
}
//标准答案
typedef struct BTNode{
int data;
struct BTNode *lchild;
struct BTNode *rchild;
}BTNode;
void findparent(BTNode *b,int x BTNode *&p){
if(b == NULL){
p = NULL;
return ;
}
// 如果当前节点的左右子节点中有值为x的节点,则当前节点是目标节点的父节点
if((b->left !=NULL && b->left->data == x )|| (b->right != NULL && b->right->data = x)){
p = b;
}
// 递归查找左子树
findparent(b->left,x,p);
if(p == NULL) return;
// 递归查找右子树
findparent(b->right,x,p);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# C 语言文件处理系统
#include <stdio.h>
#include <stdlib.h>
#include <string .h>
typedef struct File{
void (*open)(void *);
void (*read)(void *);
void (*close)(void *);
}
typedef struct {
File base; //基类File
FILE *fp; //文件指针
char filename[256];
}TextFile;
typedef struct {
File base;
FILE *fp;
char filename[256];
}BinaryFile;
void openTextFile(void *self){
TextFile *file = (TextFile*)self;
file->fp = fopen(filefilename,"r");
if(file->fp){
printf("open success");
}else{
printf("error");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 交错字符串
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void interleave(char *str1, char *str2, int i, int j, char *result, int pos) {
if (i == strlen(str1) && j == strlen(str2)) {
result[pos] = '\0'; // 确保字符串正确结束
printf("%s\n", result); // 打印当前组合并换行
return;
}
if (i < strlen(str1)) { // 如果 str1 还有字符未被处理
result[pos] = str1[i];
interleave(str1, str2, i + 1, j, result, pos + 1); // 递归处理 str1 的下一个字符
}
if (j < strlen(str2)) { // 如果 str2 还有字符未被处理
result[pos] = str2[j];
interleave(str1, str2, i, j + 1, result, pos + 1); // 递归处理 str2 的下一个字符
}
}
int main() {
char str1[] = "AB";
char str2[] = "12";
int len = strlen(str1) + strlen(str2);
char *result = (char *)malloc((len + 1) * sizeof(char)); // 分配结果数组内存
interleave(str1, str2, 0, 0, result, 0); // 调用交织函数
free(result); // 释放内存
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 模拟六
# 将每个单词倒序输出,并根据小写、大写或数字进行转换处理。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void toggleCase(char *word){ //转换大小写
for(int i=0;word[i]!='\0';i++){
if(islower(word[i])){
word[i] = toupper(word[i]);
}else if(isupper(word[i])){
word[i] = tolower(word[i]);
}
}
}
void reverseString(char *word,int len){
int start =0,end = len-1;
while(start < end){
int temp = word[start];
word[start] = word[end];
word[end] = temp;
start++;
end--;
}
}
void processLine(char *line){
char words[100][100];
int wordCount = 0;
char *token = strtok(line," ");
while(token!=NULL){
strcpy(words[wordCount],token); //存入数组
wordCount++;
token = strok(NULL," "); //按照“ ”拆开每一个单词
}
for(int i=wordCount -1;i>=0;i--){ //逆序输出
if(isdigit(word[i][0])){
reverseString(words[i],strlen(words[i])); //反转数字
}else{
toggleCase(words[i]); //大小写转换
}
printf("%s",words[i]);
if(i>0){
printf(" ");
}
}
printf("\n");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 类
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
class Shape{
public:
virtual double calculateArea()=0;
virtual ~Shape(){}
}
//派生类
class Circlle:public Shape{
private:
double radius;
public:
Circle(double r):radius(r);
double calculateArea(){
return 3.14*radius*radius;
}
}
Class Rectangle:public Shape{
private:
double width,high;
public:
Rectangle(double w,double h):width(w),high(h){}
double calculateArea(){
return width*high;
}
}
Shape *createShape(string shapeType,vector<double>& params){
if(shapeType == "circle" && params.size() ==1){
return new Circle(params[0]);
}else if (shapeType == "rectangle" && params.size() == 2){
return new Rectangle(params[0],params[1]);
}else{
return NULL;
}
}
void processInput(){
string input;
while(true){
getline(cin,input);
if(input == "0") break;
istringstream iss(input);
string shapeType;
vector<double> params;
double param;
iss>>shapeType;
while(iss>>param){
params.push_back(param);
}
Shape *shape = createShape(shapeType,params);
if(shape){
cour<<shapeType<<"area is"<<shape->calculateArea()<<endl;
delete shape;
}else{
cout<<"Invalid shape type or paramters"<<endl;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 并查集应用
#include <stdio.h>
int parent[100005];
int rank[100005];
void initialize(int n){
for(int i=1;i<=0;i++){
parent[i] = i;
rank[i]=0;
}
}
int find(int x){
if(parent[x] != x){
parent[x] = find(parent[x]);
}
return parent[x];
}
int uniionSets(int a,int b){
int rootA = find(a);
int rootB = find(b);
if(rootA != rootB){
if(rank[rootA]>rank[rootB]){
parent[rootB] = rootA;
}else if(rank[rootA]<rank[rootB]){
parent[rootA] = rootB;
}else{
parent[rootB] = rootA;
rank[rootA]++;
}
}
}
void processInput(int n,int m){
char operation[2];
int a,b;
for(int i=0;i<m;i++){
scanf("%s %d %d",operation,&a,&b);
if(operation[0] == 'M'){
unionSets(a,b);
}else if(operation[0] == 'Q'){
if(find(a) == find(b)){
printf("Yes\n");
}else{
printf("No\n");
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 最长连续出现字符以及次数
#include <stdio.h>
#include <string.h>
// 查找最长连续字符及其出现次数
void findLongest(char str[], char *charResult, int *count) {
int maxCount = 0;
char maxChar = str[0];
char currentChar = str[0];
int num = 1;
int len = strlen(str); // 计算字符串长度
for (int i = 1; i < len; i++) {
if (str[i] == currentChar) {
num++;
} else {
if (num > maxCount) {
maxCount = num;
maxChar = currentChar;
}
// 重置新的字符和计数
currentChar = str[i];
num = 1;
}
}
// 检查最后一次计数是否为最大值
if (num > maxCount) {
maxCount = num;
maxChar = currentChar;
}
*charResult = maxChar; // 修改指针指向的值
*count = maxCount;
}
int main() {
int n;
printf("请输入要测试的数据组数: ");
scanf("%d", &n);
char str[200]; // 存储输入字符串
char charResult; // 最长连续字符
int count; // 出现次数
for (int i = 0; i < n; i++) {
printf("请输入字符串: ");
scanf("%199s", str); // 限制输入字符串长度为199字符
findLongest(str, &charResult, &count); // 调用函数查找
printf("最长连续字符:%c,出现次数:%d\n", charResult, count);
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 列表动态和
输入 1 2 3 4 输出 1,3,6,10 ->[1,1+2,1+2+3,1+2+3+4];
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
char input[1000];
scanf("%s",input);
int count =0;
int nums[100];
char *token = strtok(input," ");
while(token != NULL){
nums[count] = atoi(token); //转换为整数存储
token = strtok(NULL," ");
count++;
}
int len = strlen(nums);
printf("%d",nums[1]);
for(int i=1;i<len;i++){
printf(",");
nums[i]+=nums[i-1];
printf("%d",num[i]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 模拟七
# 汉诺塔
#include <stdio.h>
//汉诺塔问题
void print(char x,char y){
printf("%c->%c\n",x,y);
}
void move(int n,char start,char temp,char target){ //n:汉诺塔层数,start: 开始位置,temp:中转位置 end:目标位置
if(n==1){
print(start,target); //只有一个盘子,直接放目标位置上
}
else{
move(n-1,start,target,temp); //递归调用,将n-1个盘子移动到辅助位置上
print(start,target); //将第n个盘子放目标位置上面
move(n-1,temp,start,target); //递归调用将n-1个盘子从辅助位置上移动到目标位置
}
}
int main(){
move(3,'A','B','C');
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# add 函数
#include <iostream>
using namespace std;
int add(int a,int b){
return a+b;
}
float add(float a,float b){
return a+b;
}
double add(double a,double b){
return a+b;
}
int main(){
int intResult = add(3,4);
cout<<"3+4 = "<<intResult<<endl;
float floatResult = add(3.5f,4.2f);
cout<<"3.5+4.2= "<<floatResult<<endl;
double doubleResult = add(3.1,4.8);
cout<<"3.1+4.8 = "<<doubleResult<<endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 改变双向链表某个结点和前驱结点的顺序
#include <stdio.h>
#include <malloc.h>
//双向链表默认结构体 __________________________________________________
typedef struct DNode{
int data;
struct DNode *next;
struct DNode *prior;
}DNode,*DinkList;
void change(DNode *p){
if(p == NULL || p->prior == NULL) return ;
DNode *prev = p->prior;
DNode *prevPrev = prev->prior;
DNode *next = p->next;
prev->next = next;
if(next != NULL) next->prev = prev;
p->prior = prevPrev;
if(prevPrev!=NULL) prevPrev->next = p;
p->next = prev;
prev->prior=p;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 任务调度
#include <stdio.h>
#include <limits.h>
#define MAX_N 20
#define MAX_K 20
int n, k;
int tasks[MAX_N]; // 存储每个任务的时间
int machines[MAX_K]; // 存储每台机器的当前工作时间
int min_max_time = INT_MAX; // 最小的最大工作时间
// 计算当前分配方案中最大工作时间
int calculate_max_time() {
int max_time = 0;
for (int i = 0; i < k; i++) {
if (machines[i] > max_time) {
max_time = machines[i];
}
}
return max_time;
}
// 回溯函数:分配任务
void assign_tasks(int task_index) {
if (task_index == n) { // 所有任务已分配完成
int max_time = calculate_max_time(); // 当前分配方案的最大时间
if (max_time < min_max_time) {
min_max_time = max_time; // 更新最优解
}
return;
}
for (int i = 0; i < k; i++) {
machines[i] += tasks[task_index]; // 将当前任务分配给第 i 台机器
if (machines[i] < min_max_time) { // 剪枝:只有当前机器时间小于已知最优解时才递归
assign_tasks(task_index + 1);
}
machines[i] -= tasks[task_index]; // 回溯,撤销分配
}
}
int main() {
// 输入任务数和机器数
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &tasks[i]);
}
// 初始化机器工作时间为 0
for (int i = 0; i < k; i++) {
machines[i] = 0;
}
// 回溯分配任务
assign_tasks(0);
// 输出最小的最大时间
printf("%d\n", min_max_time);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 模拟卷八
# 递归逆序输出整数
void reversePrintf(int num[],int size,int index){
if(index<size){
reversePrintf(num,size,index+1);
printf("%d",num[index]);
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 函数指针加减乘除
#include <stdio.h>
double add(double a,double b) return a+b;
double subtract(double a,double b) return a-b;
double multiply(double a,double b) return a*b;
double divide(double a,double b) return a/b;
int main(){
double (*funStr)(double,double);
double num1 = 100.0,nums2 = 10.0;
funStr = add;
printf("加:%lf",(*funstr)(num1,num2));
funStr = subtract;
printf("减:%lf",(*funstr)(num1,num2));
funStr = multiply;
printf("乘:%lf",(*funstr)(num1,num2));
funStr = divide;
printf("除:%lf",(*funstr)(num1,num2));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 重载类加法和减法
#include <iostream>
using namespace std;
class Complex{
private:
double real;
double imaginary
public:
Complex(double a,double b):real(a),imaginary(b){}
~Complex(){}
friend Complex operator+(Complex1 obj1,Complex2); //扩展 友元的方式
Complex operator+(Complex obj1){ //成员函数形式
return Complex(this.real+obj1.real,this.imaginary+obj1.imaginary);
}
Complex operator-(Complex obj1){ //成员函数形式
return Complex(this.real-obj1.real,this.imaginary-obj1.imaginary);
}
void display(){ //输出
cout<<real<<imaginary<<endl;
}
}
Complex operator+(Complex obj1,Complex obj2){
return Complex(obj1.real+obj2.real,obj1.imaginary+obj2.imaginary);
}
int main(){
Complex A(3.0,2.0);
Complex B(1.0,2.0);
Complex addAB = A+B;
Complex subAB = A-B;
addAB.display();
subAB.display();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 矩阵乘法
#include <stdio.h>
void multiplyMatrices(int A[][], int B[][], int C[][], int m, int n, int p) {
for(int i=0;i<m;i++){
for(int j=0;j<p;j++){
C[i][j] = 0;
}
}
//矩阵乘法计算
for(int i=0;i<m;i++){
for(int j=0;j<p;j++){
for(int k=0;k<n;k++){
C[i][j] += A[i][k]*B[k][j];
}
}
}
}
int main(){
int m,n,p;
int A[MAX][MAX],B[MAX],C[MAX];
printf("请输入A几行几列");
scanf("%d %d",&m,&n);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
scanf("%d",&A[i][j]);
}
}
printf("请输入B的列数");
scanf("%d",&p);
for(int i=0;i<n;i++){
for(int j = 0;j<p;j++){
scanf("%d",&B[i][j]);
}
}
multiplyMatrices(A,B,C,m,n,p);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 最长回文字符串
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int isPalindrome(char *str){
int left = 0,right = strlen(str)-1;
while(left < right){
if(str[left] != str[right]){
return 0;
}
left++;
right--
}
return 1;
}
int main(){
char input[2000];
char words[100][100];
int wordCount = 0;
printf("请输入字符串");
fgets(input,sizeof(input),stdin);
char token = strtok(str,","); //通过 "," 分割
while(token!=NULL){
strcpy(words[wordCount],token);
wordCount++;
token = strtok(NULL,",");
}
int maxLen = 0;
char result[100][100];
int resultCount = 0;
for(int i=0;i<wordCount;i++){
if(isPalindrome(words[i])){ //是否是回文字符串
int len = strlen(words[i]);
if(len > maxLen){
maxLen = len;
resultCount = 0;
strcpy(result[resultCount++],words[i]); //存入当前字符
}else if(len == maxLen){
strcpy(result[resultCount++],words[i]); //长度相同顺序存入
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50