Browse Source

增加了部分题目

Pchen. 3 months ago
parent
commit
c9029ede11

+ 6 - 0
README.md

@@ -1,11 +1,17 @@
 # 高级程序设计(C&C++)题库
 
+此项目包含了希冀平台高级程序设计(C&C++)的大部分题目,所有源代码均在code目录下。
+
 ## 贡献指南
 
 我们欢迎您为该项目做出贡献!如果您想提交代码或改进项目,我们非常乐意接受您的帮助。请按照以下步骤操作:
 
 ### 如何贡献
 
+针对发现的问题,直接修改并提交 Pull request 即可。
+
+在提交新题时,请确保代码已通过希冀平台评判,且无编译警告和格式错误。文件命名应与题目名称完全一致,不包含题号。
+
 1. **克隆仓库**:将项目克隆到您的本地环境中。
    
    ```bash

+ 52 - 0
code/CPU类的定义和使用——9-02.cpp

@@ -0,0 +1,52 @@
+#include <iostream>
+using namespace std;
+
+enum CPU_Rank { P1 = 1, P2, P3, P4, P5, P6, P7 };
+
+class CPU {
+private:
+    CPU_Rank rank;
+    int frequency;
+    float voltage;
+
+public:
+    // 构造函数
+    CPU(CPU_Rank r = P1, int f = 100, float v = 2.0) : rank(r), frequency(f), voltage(v) {
+        cout << "one CPU is created!" << endl;
+    }
+
+    // 析构函数
+    ~CPU() {
+        cout << "one CPU is distoried!" << endl;
+    }
+
+    // 运行
+    void run() {
+        cout << "CPU is running!" << endl;
+    }
+
+    // 停止
+    void stop() {
+        cout << "CPU stop!" << endl;
+    }
+
+    // 显示CPU信息
+    void display() const {
+        cout << "rank:P" << rank << endl;
+        cout << "frequency:" << frequency << endl;
+        cout << "voltage:" << voltage << endl;
+    }
+};
+
+int main() {
+    int f;
+    float v;
+    cin >> f >> v;
+
+    CPU myCPU(P6, f, v);
+    myCPU.run();
+    myCPU.display();
+    myCPU.stop();
+
+    return 0;
+}

+ 51 - 0
code/二维整型数组的“最大点”.c

@@ -0,0 +1,51 @@
+#include <stdio.h>
+
+int main() {
+    int n, m, i, j;
+    // 读取二维数组的行数和列数
+    scanf("%d %d", &n, &m);
+
+    int arr[n][m];
+    // 读取二维数组的元素
+    for (i = 0; i < n; i++) {
+        for (j = 0; j < m; j++) {
+            scanf("%d", &arr[i][j]);
+        }
+    }
+
+    // 查找每行的最大值和最大值所在的列
+    int row_max[n];
+    int row_max_col[n];
+    for (i = 0; i < n; i++) {
+        row_max[i] = arr[i][0];
+        row_max_col[i] = 0;
+        for (j = 1; j < m; j++) {
+            if (arr[i][j] > row_max[i]) {
+                row_max[i] = arr[i][j];
+                row_max_col[i] = j;
+            }
+        }
+    }
+
+    // 查找每列的最大值
+    int col_max[m];
+    for (j = 0; j < m; j++) {
+        col_max[j] = arr[0][j];
+        for (i = 1; i < n; i++) {
+            if (arr[i][j] > col_max[j]) {
+                col_max[j] = arr[i][j];
+            }
+        }
+    }
+
+    // 查找最大点
+    for (i = 0; i < n; i++) {
+        int max_val = row_max[i];
+        int col_idx = row_max_col[i];
+        if (max_val == col_max[col_idx]) {
+            printf("%d %d %d\n", max_val, i + 1, col_idx + 1);
+        }
+    }
+
+    return 0;
+}

+ 58 - 0
code/判断两个数组是否包含相同元素.c

@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int same_set(int a[], int b[], int len) {
+    // 创建两个哈希表,记录每个数组中元素的出现次数
+    int count_a[101] = {0}; // 假设数组元素值的范围是0-100
+    int count_b[101] = {0};
+	int i;
+	
+    // 记录数组a中每个元素的出现次数
+    for (i = 0; i < len; i++) {
+        count_a[a[i]]++;
+    }
+
+    // 记录数组b中每个元素的出现次数
+    for (i = 0; i < len; i++) {
+        count_b[b[i]]++;
+    }
+
+    // 比较两个哈希表中的元素出现次数
+    for (i = 0; i < 101; i++) {
+        if (count_a[i] != count_b[i]) {
+            return 0;
+        }
+    }
+
+    return 1;
+}
+
+int main() {
+    int len;
+
+    // 读取数组元素个数
+    scanf("%d", &len);
+
+    int a[len];
+    int b[len];
+    int i;
+
+    // 读取第一个数组的各个值
+    for (i = 0; i < len; i++) {
+        scanf("%d", &a[i]);
+    }
+
+    // 读取第二个数组的各个值
+    for (i = 0; i < len; i++) {
+        scanf("%d", &b[i]);
+    }
+
+    // 调用same_set函数并输出结果
+    if (same_set(a, b, len)) {
+        printf("1\n");
+    } else {
+        printf("0\n");
+    }
+
+    return 0;
+}

+ 48 - 0
code/合并字符串.c

@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <string.h>
+
+void str_bin(char str1[], char str2[]) {
+    int len1 = strlen(str1);
+    int len2 = strlen(str2);
+    char result[len1 + len2 + 1];  // 合并后的字符串
+    int i = 0, j = 0, k = 0;
+
+    // 合并两个有序字符串
+    while (i < len1 && j < len2) {
+        if (str1[i] < str2[j]) {
+            result[k++] = str1[i++];
+        } else {
+            result[k++] = str2[j++];
+        }
+    }
+
+    // 将剩余的字符复制到结果字符串
+    while (i < len1) {
+        result[k++] = str1[i++];
+    }
+
+    while (j < len2) {
+        result[k++] = str2[j++];
+    }
+
+    result[k] = '\0';  // 添加字符串结束符
+
+    // 将结果复制回str1
+    strcpy(str1, result);
+}
+
+int main() {
+    char str1[201];  // 确保足够的空间存储合并后的结果
+    char str2[101];
+
+    scanf("%100s", str1);
+    scanf("%100s", str2);
+
+    // 调用合并函数
+    str_bin(str1, str2);
+
+    // 输出合并后的有序字符串
+    printf("%s\n", str1);
+
+    return 0;
+}

+ 28 - 0
code/回文判断.c

@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <string.h>
+
+int main() {
+    char sentence[51]; // 句子最大长度不超过50
+    scanf("%s", sentence);
+
+    int length = strlen(sentence);
+    int is_palindrome = 1;
+
+    // 判断是否是回文
+    int i;
+    for (i = 0; i < length / 2; i++) {
+        if (sentence[i] != sentence[length - 1 - i]) {
+            is_palindrome = 0;
+            break;
+        }
+    }
+
+    // 输出结果
+    if (is_palindrome) {
+        printf("Yes\n");
+    } else {
+        printf("No\n");
+    }
+
+    return 0;
+}

+ 53 - 0
code/序列的中间数.c

@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+// 辅助函数:比较函数,用于qsort排序
+int compare(const void *a, const void *b) {
+    return (*(int *)a - *(int *)b);
+}
+
+int findMedianNumber(int arr[], int n) {
+    // 先对数组进行排序
+    qsort(arr, n, sizeof(int), compare);
+
+    // 检查中位数是否符合要求
+    int medianIndex = n / 2;
+    int median = arr[medianIndex];
+
+    // 计算比中位数小和大的元素数量
+    int smaller = 0, larger = 0, i;
+    for (i = 0; i < n; i++) {
+        if (arr[i] < median) {
+            smaller++;
+        } else if (arr[i] > median) {
+            larger++;
+        }
+    }
+
+    // 检查条件
+    if (smaller == larger) {
+        return median;
+    } else {
+        return -1;
+    }
+}
+
+int main() {
+    int n;
+    // 读取整数序列的数量
+    scanf("%d", &n);
+
+    int arr[n], i;
+    // 读取整数序列
+    for (i = 0; i < n; i++) {
+        scanf("%d", &arr[i]);
+    }
+
+    // 找到中间数
+    int result = findMedianNumber(arr, n);
+
+    // 输出结果
+    printf("%d\n", result);
+
+    return 0;
+}

+ 33 - 0
code/排序-sort指针例子.c

@@ -0,0 +1,33 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+// 比较函数,用于qsort排序
+int compare(const void *a, const void *b) {
+    float abs_a = fabs(*(float*)a);
+    float abs_b = fabs(*(float*)b);
+    return (abs_b > abs_a) - (abs_b < abs_a);
+}
+
+int main() {
+	int i;
+    // 输入10个float实数
+    float numbers[10];
+    for (i = 0; i < 10; i++) {
+        scanf("%f", &numbers[i]);
+    }
+
+    // 使用qsort对数组进行排序
+    qsort(numbers, 10, sizeof(float), compare);
+
+    // 以小数点后两位有效数字输出从大到小数列
+    for (i = 0; i < 10; i++) {
+        printf("%.2f", numbers[i]);
+        if (i < 9) {
+            printf(",");
+        }
+    }
+    printf("\n");
+
+    return 0;
+}

+ 48 - 0
code/数组元素循环右移问题(有样例代码).c

@@ -0,0 +1,48 @@
+#include <stdio.h>
+
+void rightRotate(int arr[], int n, int m) {
+    int temp[n], i;
+    // 计算实际移动的位置
+    m = m % n;
+
+    // 将数组最后m个元素移到临时数组的前面
+    for (i = 0; i < m; i++) {
+        temp[i] = arr[n - m + i];
+    }
+
+    // 将数组前n-m个元素移动到临时数组的后面
+    for (i = 0; i < n - m; i++) {
+        temp[m + i] = arr[i];
+    }
+
+    // 将临时数组的内容复制回原数组
+    for (i = 0; i < n; i++) {
+        arr[i] = temp[i];
+    }
+}
+
+int main() {
+    int n, m;
+    // 读取N和M
+    scanf("%d %d", &n, &m);
+
+    int arr[n], i;
+    // 读取N个整数
+    for (i = 0; i < n; i++) {
+        scanf("%d", &arr[i]);
+    }
+
+    // 右移M位
+    rightRotate(arr, n, m);
+
+    // 输出结果
+    for (i = 0; i < n; i++) {
+        if (i > 0) {
+            printf(" ");
+        }
+        printf("%d", arr[i]);
+    }
+    printf("\n");
+
+    return 0;
+}

+ 34 - 0
code/最大公约数和最小公倍数.c

@@ -0,0 +1,34 @@
+#include <stdio.h>
+
+// 欧几里得算法计算最大公约数
+int gcd(int a, int b) {
+    while (b != 0) {
+        int temp = b;
+        b = a % b;
+        a = temp;
+    }
+    return a;
+}
+
+// 计算最小公倍数
+int lcm(int a, int b, int gcd_value) {
+    return (a / gcd_value) * b;
+}
+
+int main() {
+    int a, b;
+
+    // 从标准输入读取两个整数
+    scanf("%d %d", &a, &b);
+
+    // 计算最大公约数
+    int gcd_value = gcd(a, b);
+
+    // 计算最小公倍数
+    int lcm_value = lcm(a, b, gcd_value);
+
+    // 输出结果
+    printf("%d %d\n", gcd_value, lcm_value);
+
+    return 0;
+}

+ 51 - 0
code/船和卡车的重量计算——友元函数——11-02.cpp

@@ -0,0 +1,51 @@
+#include <iostream>
+using namespace std;
+
+class car;
+
+class boat {
+private:
+    int weight;
+public:
+    boat(int w) : weight(w) {}
+    friend int totalweight(const boat& b, const car& c);
+    void display() const {
+        cout << "boat's weight:" << weight << endl;
+    }
+    ~boat() {
+        cout << "boat is destruction" << endl;
+    }
+};
+
+class car {
+private:
+    int weight;
+public:
+    car(int w) : weight(w) {}
+    friend int totalweight(const boat& b, const car& c);
+    void display() const {
+        cout << "car's weight:" << weight << endl;
+    }
+    ~car() {
+        cout << "car is destruction" << endl;
+    }
+};
+
+int totalweight(const boat& b, const car& c) {
+    b.display();
+    c.display();
+    cout << "total weight:" << b.weight + c.weight << endl;
+    return b.weight + c.weight;
+}
+
+int main() {
+    int boat_weight, car_weight;
+    cin >> boat_weight >> car_weight;
+
+    boat b(boat_weight);
+    car c(car_weight);
+
+    totalweight(b, c);
+
+    return 0;
+}