Browse Source

增加加强训练任务

Pchen. 10 months ago
parent
commit
542c22466e
55 changed files with 1465 additions and 0 deletions
  1. BIN
      code/加强训练任务/gmon.out
  2. 38 0
      code/加强训练任务/sort-指针例子.c
  3. 31 0
      code/加强训练任务/乒乓球比赛.c
  4. 15 0
      code/加强训练任务/交换a和b的数值.c
  5. BIN
      code/加强训练任务/交换a和b的数值.exe
  6. 48 0
      code/加强训练任务/从文件data.txt读入若干实数并将统计结果存入文件result.txt中.c
  7. 41 0
      code/加强训练任务/使用数组管理学生成绩.c
  8. 25 0
      code/加强训练任务/分数化简.c
  9. 39 0
      code/加强训练任务/删除子串.c
  10. 25 0
      code/加强训练任务/删除字符串中指定n个字符.c
  11. 57 0
      code/加强训练任务/判断矩阵.c
  12. 35 0
      code/加强训练任务/反弹.c
  13. 17 0
      code/加强训练任务/各位数字之和.c
  14. 48 0
      code/加强训练任务/合并有序数组.c
  15. 79 0
      code/加强训练任务/合数分解.c
  16. 15 0
      code/加强训练任务/合数分解1.c
  17. 16 0
      code/加强训练任务/四舍五入.c
  18. 31 0
      code/加强训练任务/回文数.c
  19. BIN
      code/加强训练任务/回文数.exe
  20. 33 0
      code/加强训练任务/字符串中整数.c
  21. 25 0
      code/加强训练任务/字符串拼接.c
  22. 36 0
      code/加强训练任务/字符串转换成十进制整数.c
  23. 38 0
      code/加强训练任务/字符查找.c
  24. 42 0
      code/加强训练任务/学生总成绩排序.c
  25. 45 0
      code/加强训练任务/小数分数转换.c
  26. 57 0
      code/加强训练任务/打印正六边形.c
  27. BIN
      code/加强训练任务/打印正六边形.exe
  28. 35 0
      code/加强训练任务/换码序列的拷贝.c
  29. 26 0
      code/加强训练任务/换钱交易.c
  30. 17 0
      code/加强训练任务/搬砖问题.c
  31. 23 0
      code/加强训练任务/整数三位分节.c
  32. 25 0
      code/加强训练任务/整数分解为若干项之和.c
  33. BIN
      code/加强训练任务/整数分解为若干项之和.exe
  34. 46 0
      code/加强训练任务/整数的n进制字符串表示.c
  35. 16 0
      code/加强训练任务/整数组合.c
  36. 25 0
      code/加强训练任务/最大公约数和最小公倍数.c
  37. 19 0
      code/加强训练任务/求A,B.c
  38. 27 0
      code/加强训练任务/求cosx计算公式.c
  39. 31 0
      code/加强训练任务/用函数嵌套调用实现回文数.c
  40. 71 0
      code/加强训练任务/矩阵运算.c
  41. 29 0
      code/加强训练任务/给一个整型数组编号b.c
  42. 42 0
      code/加强训练任务/统计两个一维数组中a和b的关系.c
  43. 23 0
      code/加强训练任务/编写程序 创建文本文件mul_table.txt其内容为九九乘法表.c
  44. BIN
      code/加强训练任务/编写程序 创建文本文件mul_table.txt其内容为九九乘法表.exe
  45. 19 0
      code/加强训练任务/计算n个a相减.c
  46. BIN
      code/加强训练任务/计算n个a相减.exe
  47. 20 0
      code/加强训练任务/计算公式b.c
  48. 25 0
      code/加强训练任务/计算过道和栏杆的造价.c
  49. BIN
      code/加强训练任务/计算过道和栏杆的造价.exe
  50. 37 0
      code/加强训练任务/输入年月日求第几天.c
  51. 14 0
      code/加强训练任务/输入秒数转换成几分几秒.c
  52. BIN
      code/加强训练任务/输入秒数转换成几分几秒.exe
  53. 40 0
      code/加强训练任务/连续正整数的和.c
  54. 19 0
      code/加强训练任务/限定条件求x3+y3.c
  55. BIN
      code/加强训练任务/限定条件求x3+y3.exe

BIN
code/加强训练任务/gmon.out


+ 38 - 0
code/加强训练任务/sort-指针例子.c

@@ -0,0 +1,38 @@
+#include <stdio.h>
+
+void swap(float *a, float *b) {
+    float temp = *a;
+    *a = *b;
+    *b = temp;
+}
+
+void sort(float *arr, int n) {
+	int i,j;
+    for (i = 0; i < n - 1; i++) {
+        for (j = 0; j < n - i - 1; j++) {
+            if (arr[j] < arr[j + 1]) {
+                swap(&arr[j], &arr[j + 1]);
+            }
+        }
+    }
+}
+
+int main() {
+    float arr[10];
+	int i;
+	
+    for (i = 0; i < 10; i++) {
+        scanf("%f", &arr[i]);
+    }
+
+    sort(arr, 10);
+
+    for (i = 0; i < 10; i++) {
+        printf("%.2f", arr[i]);
+        if (i != 9) {
+            printf(",");
+        }
+    }
+
+    return 0;
+}

+ 31 - 0
code/加强训练任务/乒乓球比赛.c

@@ -0,0 +1,31 @@
+#include <stdio.h>
+
+int main() {
+    char player;
+    scanf("%c", &player);
+
+    switch (player) {
+        case 'a':
+            printf("z\n");
+            break;
+        case 'b':
+            printf("x\n");
+            break;
+        case 'c':
+            printf("y\n");
+            break;
+        case 'x':
+            printf("b\n");
+            break;
+        case 'y':
+            printf("c\n");
+            break;
+        case 'z':
+            printf("a\n");
+            break;
+        default:
+            printf("Invalid input\n");
+    }
+
+    return 0;
+}

+ 15 - 0
code/加强训练任务/交换a和b的数值.c

@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+int main() {
+    int a,b, temp;
+
+	scanf("a=%d,b=%d",&a,&b);
+    // ½»»»aºÍbµÄÖµ
+    temp = a;
+    a = b;
+    b = temp;
+
+    printf("a=%d,b=%d\n", a, b);
+
+    return 0;
+}

BIN
code/加强训练任务/交换a和b的数值.exe


+ 48 - 0
code/加强训练任务/从文件data.txt读入若干实数并将统计结果存入文件result.txt中.c

@@ -0,0 +1,48 @@
+#include <stdio.h>
+
+int main() {
+    FILE *inputFile, *outputFile;
+    float num;
+    int count = 0;
+    float sum = 0;
+
+    inputFile = fopen("data.txt", "r");
+    if (inputFile == NULL) {
+        printf("无法打开输入文件\n");
+        return 1;
+    }
+
+    while (fscanf(inputFile, "%f", &num) == 1) {
+        count++;
+        sum += num;
+    }
+
+    float average = sum / count;
+
+    fclose(inputFile);
+
+    outputFile = fopen("result.txt", "w");
+    if (outputFile == NULL) {
+        printf("无法打开输出文件\n");
+        return 1;
+    }
+
+    inputFile = fopen("data.txt", "r");
+
+    int i = 0;
+    while (fscanf(inputFile, "%f", &num) == 1) {
+        fprintf(outputFile, "%.2f ", num);
+        i++;
+        if (i == 5) {
+            fprintf(outputFile, "\n");
+            i = 0;
+        }
+    }
+
+    fprintf(outputFile, "\n实数个数:%d\n平均值:%.2f", count, average);
+
+    fclose(inputFile);
+    fclose(outputFile);
+
+    return 0;
+}

+ 41 - 0
code/加强训练任务/使用数组管理学生成绩.c

@@ -0,0 +1,41 @@
+#include <stdio.h>
+
+int main() {
+    int n;
+    scanf("%d", &n);
+
+    int stu[100][4];
+    int t[100];
+    int i,j;
+
+    for (i = 0; i < n; i++) {
+        for (j = 0; j < 4; j++) {
+            scanf("%d", &stu[i][j]);
+        }
+    }
+
+    for (i = 0; i < n; i++) {
+        t[i] = 0;
+        for (j = 0; j < 4; j++) {
+            t[i] += stu[i][j];
+        }
+    }
+
+    double totalAvg = 0;
+    for (i = 0; i < n; i++) {
+        totalAvg += t[i];
+        printf("%d ", t[i]);
+    }
+    printf("\n");
+
+    for (i = 0; i < n; i++) {
+        double avg = (double)t[i] / 4;
+        printf("%.2f ", avg);
+    }
+    printf("\n");
+
+    totalAvg /= n;
+    printf("%.2f\n", totalAvg/4);
+
+    return 0;
+}

+ 25 - 0
code/加强训练任务/分数化简.c

@@ -0,0 +1,25 @@
+#include <stdio.h>
+
+// 计算最大公约数
+int gcd(int a, int b) {
+    if (b == 0) {
+        return a;
+    }
+    return gcd(b, a % b);
+}
+
+int main() {
+    int numerator, denominator;
+    scanf("%d %d", &numerator, &denominator);
+
+    // 计算最大公约数
+    int greatest_common_divisor = gcd(numerator, denominator);
+
+    // 化简分数
+    numerator /= greatest_common_divisor;
+    denominator /= greatest_common_divisor;
+
+    printf("%d %d\n", numerator, denominator);
+
+    return 0;
+}

+ 39 - 0
code/加强训练任务/删除子串.c

@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <string.h>
+
+int main() {
+    char str[100], sub[100];
+    fgets(str, 100, stdin);
+    fgets(sub, 100, stdin);
+
+    // Remove newline characters
+    str[strcspn(str, "\n")] = '\0';
+    sub[strcspn(sub, "\n")] = '\0';
+
+    int str_len = strlen(str);
+    int sub_len = strlen(sub);
+
+    char result[100];
+    int result_index = 0;
+	int i,j;
+    for (i = 0; i < str_len; i++) {
+        int match = 1;
+        for (j = 0; j < sub_len; j++) {
+            if (str[i + j] != sub[j]) {
+                match = 0;
+                break;
+            }
+        }
+
+        if (match) {
+            i += sub_len - 1;
+        } else {
+            result[result_index++] = str[i];
+        }
+    }
+
+    result[result_index] = '\0';
+    printf("%s\n", result);
+
+    return 0;
+}

+ 25 - 0
code/加强训练任务/删除字符串中指定n个字符.c

@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <string.h>
+
+void del(char s[], int i, int num) {
+    int len = strlen(s);
+    int j;
+    for (j = i-1; j < len - num; j++) {
+        s[j] = s[j + num];
+    }
+    s[len - num] = '\0';
+}
+
+int main() {
+    char s[50];
+    scanf("%s", s);
+
+    int i, num;
+    scanf("%d %d", &i, &num);
+
+    del(s, i, num);
+
+    printf("%s\n", s);
+
+    return 0;
+}

+ 57 - 0
code/加强训练任务/判断矩阵.c

@@ -0,0 +1,57 @@
+#include <stdio.h>
+
+#define MAX_SIZE 10
+
+int main() {
+    int N, M;
+    int matrix_N[MAX_SIZE][MAX_SIZE];
+    int matrix_M[MAX_SIZE][MAX_SIZE];
+    int i, j, k, l;
+    int flag, found;
+
+    scanf("%d", &N);
+    for (i = 0; i < N; i++) {
+        for (j = 0; j < N; j++) {
+            scanf("%d", &matrix_N[i][j]);
+        }
+    }
+
+    scanf("%d", &M);
+    for (i = 0; i < M; i++) {
+        for (j = 0; j < M; j++) {
+            scanf("%d", &matrix_M[i][j]);
+        }
+    }
+
+    found = 0;
+    for (i = 0; i <= N - M; i++) {
+        for (j = 0; j <= N - M; j++) {
+            flag = 1;
+            for (k = 0; k < M; k++) {
+                for (l = 0; l < M; l++) {
+                    if (matrix_N[i + k][j + l] != matrix_M[k][l]) {
+                        flag = 0;
+                        break;
+                    }
+                }
+                if (!flag) {
+                    break;
+                }
+            }
+            if (flag) {
+                printf("%d,%d\n", i + 1, j + 1);
+                found = 1;
+                break;
+            }
+        }
+        if (found) {
+            break;
+        }
+    }
+
+    if (!found) {
+        printf("-1\n");
+    }
+
+    return 0;
+}

+ 35 - 0
code/加强训练任务/反弹.c

@@ -0,0 +1,35 @@
+#include <stdio.h>
+
+int main() {
+    int n, m;
+    int i;
+    // 从键盘输入整数n和m
+    scanf("%d %d", &n, &m);
+
+    // 初始高度为n
+    double height = n;
+    // 总路程
+    double total_distance = 0;
+
+    // 第一次落地
+    total_distance += height;
+    // 反弹至原高度的四分之一
+    height /= 4;
+
+    // 从第二次落地开始,循环m-1次
+    for (i = 1; i < m; ++i) {
+        // 落地
+        total_distance += height;
+        // 反弹至原高度的四分之一
+        height /= 4;
+        // 再次下落
+        total_distance += height;
+    }
+
+    // 输出总路程,保留两位小数
+    printf("%.2f\n", total_distance);
+    // 输出第m次落地后反弹的高度,保留两位小数
+    printf("%.2f\n", height);
+
+    return 0;
+}

+ 17 - 0
code/加强训练任务/各位数字之和.c

@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+int main() {
+    int num, digit1, digit2, digit3, sum;
+
+    scanf("%d", &num);
+
+    digit1 = num / 100;
+    digit2 = (num % 100) / 10;
+    digit3 = num % 10;
+
+    sum = digit1 + digit2 + digit3;
+
+    printf("%d\n",sum);
+
+    return 0;
+}

+ 48 - 0
code/加强训练任务/合并有序数组.c

@@ -0,0 +1,48 @@
+#include <stdio.h>
+
+int main() {
+    int n1, n2,m;
+    scanf("%d", &n1);
+
+    int arr1[n1];
+    int i;
+    for (i = 0; i < n1; i++) {
+        scanf("%d", &arr1[i]);
+    }
+
+    scanf("%d", &n2);
+
+    int arr2[n2];
+    for (i = 0; i < n2; i++) {
+        scanf("%d", &arr2[i]);
+    }
+
+    int merged[n1 + n2];
+	int j = 0;
+	int k = 0;
+
+    while (i < n1 && j < n2) {
+        if (arr1[i] < arr2[j]) {
+            merged[k++] = arr1[i++];
+        } else {
+            merged[k++] = arr2[j++];
+        }
+    }
+
+    while (i < n1) {
+        merged[k++] = arr1[i++];
+    }
+
+    while (j < n2) {
+        merged[k++] = arr2[j++];
+    }
+
+    for (m = 0; m < n1 + n2; m++) {
+        printf("%d", merged[m]);
+        if (m < n1 + n2 - 1) {
+            printf(" ");
+        }
+    }
+
+    return 0;
+}

+ 79 - 0
code/加强训练任务/合数分解.c

@@ -0,0 +1,79 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+// 函数:判断一个数是否为素数
+int is_prime(int num) {
+	int i;
+    if (num <= 1) return 0;
+    if (num == 2) return 1;
+    if (num % 2 == 0) return 0;
+    for (i = 3; i * i <= num; i += 2) {
+        if (num % i == 0) return 0;
+    }
+    return 1;
+}
+
+// 函数:获取一个数的所有素数因子
+void get_prime_factors(int num, int *factors, int *count) {
+    int factor = 2;
+    while (num > 1) {
+        if (num % factor == 0) {
+            factors[*count] = factor;
+            (*count)++;
+            num /= factor;
+        } else {
+            factor++;
+            while (!is_prime(factor)) {
+                factor++;
+            }
+        }
+    }
+}
+
+int main() {
+	int i,j;
+    int num;
+
+    scanf("%d", &num);
+
+    if (num <= 1) {
+        printf("输入的数必须大于1且为合数。\n");
+        return 1;
+    }
+
+    int factors[32];  // 假设最大int的素数因子数量不会超过32个
+    int count = 0;
+
+    get_prime_factors(num, factors, &count);
+
+    int occurrences[32] = {0};  // 用于记录每个素数因子的出现次数
+    int unique_factors[32];     // 记录所有唯一的素数因子
+    int unique_count = 0;
+
+    for (i = 0; i < count; i++) {
+        int factor = factors[i];
+        int found = 0;
+        for (j = 0; j < unique_count; j++) {
+            if (unique_factors[j] == factor) {
+                occurrences[j]++;
+                found = 1;
+                break;
+            }
+        }
+        if (!found) {
+            unique_factors[unique_count] = factor;
+            occurrences[unique_count] = 1;
+            unique_count++;
+        }
+    }
+
+    // 输出只出现一次的素数因子
+    for (i = 0; i < unique_count; i++) {
+        if (occurrences[i] == 1) {
+            printf("%d ", unique_factors[i]);
+        }
+    }
+    printf("\n");
+
+    return 0;
+}

+ 15 - 0
code/加强训练任务/合数分解1.c

@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+int main() {
+    int num,i;
+    scanf("%d", &num);
+
+    for (i = 2; i <= num; i++) {
+        while (num % i == 0) {
+            printf("%d ", i);
+            num /= i;
+        }
+    }
+
+    return 0;
+}

+ 16 - 0
code/加强训练任务/四舍五入.c

@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <math.h>
+
+int main() {
+    double num;
+    int rounded_num;
+
+    scanf("%lf", &num);
+
+    // 使用round函数将小数四舍五入为最近的整数
+    rounded_num = (int)round(num);
+
+    printf("%d\n", rounded_num);
+
+    return 0;
+}

+ 31 - 0
code/加强训练任务/回文数.c

@@ -0,0 +1,31 @@
+#include <stdio.h>
+
+int isPalindrome(int x) {
+    int reversed = 0;
+    int original = x;
+
+    while (x > 0) {
+        reversed = reversed * 10 + x % 10;
+        x /= 10;
+    }
+
+    return original == reversed;
+}
+
+void loop(int a, int b) {
+	int i;
+    for (i = a; i <= b; i++) {
+        if (isPalindrome(i)) {
+            printf("%d\n", i);
+        }
+    }
+}
+
+int main() {
+    int a, b;
+    scanf("%d %d", &a, &b);
+
+    loop(a, b);
+
+    return 0;
+}

BIN
code/加强训练任务/回文数.exe


+ 33 - 0
code/加强训练任务/字符串中整数.c

@@ -0,0 +1,33 @@
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+
+int main() {
+    char input_str[256];
+    fgets(input_str, sizeof(input_str), stdin);
+
+    int sum = 0;
+    char num_str[256] = "";  
+    int num = 0;
+	int i;
+    for (i = 0; i < strlen(input_str); i++) {
+        if (isdigit(input_str[i])) {
+            strncat(num_str, &input_str[i], 1);  
+        } else {
+            if (strlen(num_str) > 0) {
+                num = atoi(num_str);  
+                sum += num;  
+                memset(num_str, 0, sizeof(num_str));  
+            }
+        }
+    }
+
+    if (strlen(num_str) > 0) {
+        num = atoi(num_str);  
+        sum += num;  
+    }
+
+    printf("%d\n", sum);
+
+    return 0;
+}

+ 25 - 0
code/加强训练任务/字符串拼接.c

@@ -0,0 +1,25 @@
+#include <stdio.h>
+
+int main() {
+    char str1[100], str2[100];
+    scanf("%[^\n]%*c", str1);
+    scanf("%[^\n]%*c", str2);
+
+    int i = 0, j = 0;
+
+    while (str1[i] != '\0') {
+        i++;
+    }
+
+    while (str2[j] != '\0') {
+        str1[i] = str2[j];
+        i++;
+        j++;
+    }
+
+    str1[i] = '\0';
+
+    printf("%s\n", str1);
+
+    return 0;
+}

+ 36 - 0
code/加强训练任务/字符串转换成十进制整数.c

@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdlib.h>
+
+int main() {
+    char str[100];
+    fgets(str, 100, stdin);
+
+    // Remove newline character
+    str[strcspn(str, "\n")] = '\0';
+
+    int is_negative = 0;
+    int hex_found = 0;
+    int i;
+    unsigned long long decimal = 0;
+
+    for (i = 0; str[i] != '#'; i++) {
+        if (str[i] == '-') {
+            is_negative = 1;
+        } else if (isxdigit(str[i])) {
+            hex_found = 1;
+            char hex_char = toupper(str[i]);
+            int hex_value = isdigit(hex_char) ? hex_char - '0' : hex_char - 'A' + 10;
+            decimal = decimal * 16 + hex_value;
+        }
+    }
+
+    if (is_negative) {
+        decimal = -decimal;
+    }
+
+    printf("%lld\n", decimal);
+
+    return 0;
+}

+ 38 - 0
code/加强训练任务/字符查找.c

@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <string.h>
+
+int main() {
+    char str[100];
+    char ch;
+    int count[100];
+    int pos = 0;
+    int flag = 0;
+    int i;
+
+    fgets(str, 100, stdin);
+    scanf("%c", &ch);
+
+    int len = strlen(str);
+    int ch_count = 0;
+
+    for (i = 0; i < len; i++) {
+        if (str[i] == ch) {
+            count[ch_count + 1] = i;
+            ch_count++;
+            flag = 1;
+        }
+    }
+
+    if (flag) {
+        count[0] = ch_count;
+        printf("%d\n", count[0]);
+        for (i = 1; i <= ch_count; i++) {
+            printf("%d ", count[i]);
+        }
+        printf("\n");
+    } else {
+        printf("No match!\n");
+    }
+
+    return 0;
+}

+ 42 - 0
code/加强训练任务/学生总成绩排序.c

@@ -0,0 +1,42 @@
+#include <stdio.h>
+
+typedef struct {
+    int student_id;
+    int math;
+    int english;
+    int chinese;
+    int total;
+} Student;
+
+int main() {
+    int n;
+    int i,j;
+    scanf("%d", &n);
+    
+    if (n >= 10) {
+        printf("Please input 1-9.\n");
+        return 0;
+    }
+
+    Student students[10];
+    for (i = 0; i < n; i++) {
+        scanf("%d %d %d %d", &students[i].student_id, &students[i].math, &students[i].english, &students[i].chinese);
+        students[i].total = students[i].math + students[i].english + students[i].chinese;
+    }
+
+    for (i = 0; i < n; i++) {
+        for (j = 0; j < n - i - 1; j++) {
+            if (students[j].total < students[j + 1].total || (students[j].total == students[j + 1].total && students[j].student_id > students[j + 1].student_id)) {
+                Student temp = students[j];
+                students[j] = students[j + 1];
+                students[j + 1] = temp;
+            }
+        }
+    }
+
+    for (i = 0; i < n; i++) {
+        printf("%4d%10d%5d%5d%5d\n", i + 1, students[i].student_id, students[i].math, students[i].english, students[i].chinese);
+    }
+
+    return 0;
+}

+ 45 - 0
code/加强训练任务/小数分数转换.c

@@ -0,0 +1,45 @@
+#include <stdio.h>
+
+// 求最大公约数
+int gcd(int a, int b) {
+    return b == 0 ? a : gcd(b, a % b);
+}
+
+int main() {
+    char str[20];
+    scanf("%s", str);
+
+    // 提取整数部分和小数部分
+    int integerPart = 0, decimalPart = 0, decimalLength = 0;
+    int i = 0;
+    while (str[i] != '.') {
+        integerPart = integerPart * 10 + (str[i] - '0');
+        i++;
+    }
+    i++;
+    while (str[i] != '\0') {
+        decimalPart = decimalPart * 10 + (str[i] - '0');
+        decimalLength++;
+        i++;
+    }
+
+    // 将小数部分化为最简分数
+    int numerator = decimalPart;
+    int denominator = 1;
+    int j;
+    for (j = 0; j < decimalLength; j++) {
+        denominator *= 10;
+    }
+    int commonDivisor = gcd(numerator, denominator);
+    numerator /= commonDivisor;
+    denominator /= commonDivisor;
+
+    // 输出结果
+    if (integerPart == 0) {
+        printf("0 %d %d\n", numerator, denominator);
+    } else {
+        printf("%d %d %d\n", integerPart, numerator, denominator);
+    }
+
+    return 0;
+}

+ 57 - 0
code/加强训练任务/打印正六边形.c

@@ -0,0 +1,57 @@
+#include <stdio.h>
+
+// 打印正六边形的函数
+void printHexagon(int N) {
+	int i,j;
+    if (N < 1) {
+        return;
+    }
+
+    // 上半部分
+    for (i = 0; i < N; i++) {
+        // 打印前导空格
+        for (j = 0; j < N - 1 - i; j++) {
+            printf(" ");
+        }
+        // 打印第一个星号
+        printf("*");
+
+        // 打印中间的空格
+        if (i != 0) {
+            for (j = 0; j < 2 * i - 1; j++) {
+                printf(" ");
+            }
+            // 打印第二个星号
+            printf("*");
+        }
+        printf("\n");
+    }
+
+    // 下半部分
+    for (i = N - 2; i >= 0; i--) {
+        // 打印前导空格
+        for (j = 0; j < N - 1 - i; j++) {
+            printf(" ");
+        }
+        // 打印第一个星号
+        printf("*");
+
+        // 打印中间的空格
+        if (i != 0) {
+            for (j = 0; j < 2 * i - 1; j++) {
+                printf(" ");
+            }
+            // 打印第二个星号
+            printf("*");
+        }
+        printf("\n");
+    }
+}
+
+int main() {
+    int N;
+    printf("请输入正整数N: ");
+    scanf("%d", &N);
+    printHexagon(N);
+    return 0;
+}

BIN
code/加强训练任务/打印正六边形.exe


+ 35 - 0
code/加强训练任务/换码序列的拷贝.c

@@ -0,0 +1,35 @@
+#include <stdio.h>
+
+void escape(char s[], char t[]) {
+    int i, j;
+    for (i = 0, j = 0; t[i] != '\0'; i++) {
+        switch (t[i]) {
+            case '\n':
+                s[j++] = '\\';
+                s[j++] = 'n';
+                break;
+            case '\t':
+                s[j++] = '\\';
+                s[j++] = 't';
+                break;
+            default:
+                s[j++] = t[i];
+                break;
+        }
+    }
+    s[j] = '\0';
+}
+
+int main() {
+    char t[50];
+    char s[100]; // Assuming the escaped string length won't exceed 100
+    
+
+    fgets(t, 50, stdin);
+
+    escape(s, t);
+
+    printf("%s\n", s);
+
+    return 0;
+}

+ 26 - 0
code/加强训练任务/换钱交易.c

@@ -0,0 +1,26 @@
+#include <stdio.h>
+
+int main() {
+    int n,day;
+    scanf("%d", &n); // 输入天数n
+
+    long long stranger_money = 0; // 陌生人总共给富翁的钱
+    long long millionaire_money = 0; // 富翁总共给陌生人的钱
+    long long temp_stranger_money = 1; // 当天陌生人给富翁的钱
+
+    for (day = 1; day <= n; day++) {
+        // 富翁给陌生人的钱
+        millionaire_money += 100000;
+        
+        // 更新陌生人给富翁的钱
+        
+        stranger_money+=temp_stranger_money;
+        temp_stranger_money *= 2;
+    }
+
+    // 输出结果
+    printf("%lld\n", millionaire_money); // 输出富翁给陌生人的钱
+    printf("%lld\n", stranger_money); // 输出陌生人给富翁的钱
+
+    return 0;
+}

+ 17 - 0
code/加强训练任务/搬砖问题.c

@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+int main() {
+    int n,man,woman,child;
+    scanf("%d", &n);
+
+    for (man = 0; man <= n / 4; man++) {
+        for (woman = 0; woman <= n / 3; woman++) {
+            child = (n - 4 * man - 3 * woman) * 2;
+            if (child >= 0 && man + woman + child == n) {
+                printf("%d %d %d\n", man, woman, child);
+            }
+        }
+    }
+
+    return 0;
+}

+ 23 - 0
code/加强训练任务/整数三位分节.c

@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <string.h>
+
+int main() {
+	int len,comma_count,i;
+    char num[20]; // 假设正整数的最大长度为20位
+    scanf("%s", num);
+
+   	len = strlen(num);
+	comma_count = len % 3 == 0 ? len / 3 - 1 : len / 3; // 计算逗号的个数
+
+    for (i = 0; i < len; i++) {
+        printf("%c", num[i]);
+        if ((len - i - 1) % 3 == 0 && comma_count > 0) {
+            printf(",");
+            comma_count--;
+        }
+    }
+
+    printf("\n");
+
+    return 0;
+}

+ 25 - 0
code/加强训练任务/整数分解为若干项之和.c

@@ -0,0 +1,25 @@
+#include<stdio.h>
+
+void find_partitions(int n, int start, int* path, int len) {
+	int i;
+    if (n == 0) {
+        printf("7=");
+        for (i = 0; i < len - 1; i++) {
+            printf("%d+", path[i]);
+        }
+        printf("%d\n", path[len - 1]);
+        return;
+    }
+    for (i = start; i <= n; i++) {
+        path[len] = i;
+        find_partitions(n - i, i, path, len + 1);
+    }
+}
+
+int main() {
+    int n;
+    scanf("%d", &n);
+    int path[30];
+    find_partitions(n, 1, path, 0);
+    return 0;
+}

BIN
code/加强训练任务/整数分解为若干项之和.exe


+ 46 - 0
code/加强训练任务/整数的n进制字符串表示.c

@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <string.h>
+
+void itob(int n, char *s, int b) {
+    int i = 0;
+    int sign = n;
+
+    if (sign < 0) {
+        n = -n;
+    }
+
+    do {
+        int digit = n % b;
+        s[i++] = (digit < 10) ? digit + '0' : digit - 10 + 'a';
+        n /= b;
+    } while (n > 0);
+
+    if (sign < 0) {
+        s[i++] = '-';
+    }
+    
+    s[i] = '\0';
+
+    int start = 0;
+    int end = strlen(s) - 1;
+    while (start < end) {
+        char temp = s[start];
+        s[start] = s[end];
+        s[end] = temp;
+        start++;
+        end--;
+    }
+}
+
+int main() {
+    int n, b;
+    char s[100];
+
+    scanf("%d %d", &n, &b);
+
+    itob(n, s, b);
+
+    printf("%s\n", s);
+
+    return 0;
+}

+ 16 - 0
code/加强训练任务/整数组合.c

@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+int main() {
+    unsigned short x, y, z;
+
+    // 从键盘输入x和y的值
+    scanf("%hu %hu", &x, &y);
+
+    // 将x的高8位作为z的高8位,y的高8位作为z的低8位
+    z = ((x >> 8) << 8) | (y >> 8);
+
+    // 输出z的值
+    printf("%hu\n", z);
+
+    return 0;
+}

+ 25 - 0
code/加强训练任务/最大公约数和最小公倍数.c

@@ -0,0 +1,25 @@
+#include <stdio.h>
+
+int gcd(int a, int b) {
+    if (b == 0) {
+        return a;
+    }
+    return gcd(b, a % b);
+}
+
+int lcm(int a, int b) {
+    return a / gcd(a, b) * b;
+}
+
+int main() {
+    int a, b;
+
+    scanf("%d %d", &a, &b);
+    
+    int greatest_common_divisor = gcd(a, b);
+    int least_common_multiple = lcm(a, b);
+
+    printf("%d %d\n", greatest_common_divisor, least_common_multiple);
+
+    return 0;
+}

+ 19 - 0
code/加强训练任务/求A,B.c

@@ -0,0 +1,19 @@
+#include <stdio.h>
+
+int main() {
+    int N,A,B;
+    scanf("%d", &N);
+
+    for (A = 1; A <= 8; A++) {
+        for (B = A + 1; B <= 9; B++) {
+            int AB = A * 10 + B;
+            int BA = B * 10 + A;
+            if (AB * BA == N) {
+                printf("%d%d\n", A, B);
+                break;
+            }
+        }
+    }
+
+    return 0;
+}

+ 27 - 0
code/加强训练任务/求cosx计算公式.c

@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <math.h>
+int n;
+double factorial(n) {
+    if (n == 0) {
+        return 1;
+    } else {
+        return n * factorial(n - 1);
+    }
+}
+
+int main() {
+    double x;
+    int n,i;
+    double cos_approx = 0;
+
+    scanf("%lf %d", &x, &n);
+
+    for (i = 0; i <= n; i++) {
+        double term = pow(-1, i) * pow(x, 2 * i) / factorial(2 * i);
+        cos_approx += term;
+    }
+
+    printf("%.8f\n", cos_approx);
+
+    return 0;
+}

+ 31 - 0
code/加强训练任务/用函数嵌套调用实现回文数.c

@@ -0,0 +1,31 @@
+#include <stdio.h>
+
+int InvertSequence(int num) {
+    int reversed = 0;
+    while (num > 0) {
+        reversed = reversed * 10 + num % 10;
+        num /= 10;
+    }
+    return reversed;
+}
+
+int loop(int x) {
+    if (x == InvertSequence(x)) {
+        return 1; 
+    } else {
+        return 0; 
+    }
+}
+
+int main() {
+    int a, b,i;
+    scanf("%d %d", &a, &b);
+
+    for (i = a; i <= b; i++) {
+        if (loop(i)) {
+            printf("%d\n", i);
+        }
+    }
+
+    return 0;
+}

+ 71 - 0
code/加强训练任务/矩阵运算.c

@@ -0,0 +1,71 @@
+#include <stdio.h>
+
+#define MAX_SIZE 10
+
+void matrix_add(int A[][MAX_SIZE], int B[][MAX_SIZE], int N) {
+	int i,j;
+    for (i = 0; i < N; i++) {
+        for (j = 0; j < N; j++) {
+            A[i][j] += B[i][j];
+        }
+    }
+}
+
+void matrix_subtract(int A[][MAX_SIZE], int B[][MAX_SIZE], int N) {
+	int i,j;
+    for (i = 0; i < N; i++) {
+        for (j = 0; j < N; j++) {
+            A[i][j] -= B[i][j];
+        }
+    }
+}
+
+void print_matrix(int A[][MAX_SIZE], int N) {
+	int i,j;
+    for (i = 0; i < N; i++) {
+        for (j = 0; j < N; j++) {
+            printf("%5d", A[i][j]);
+        }
+        printf("\n");
+    }
+}
+
+int main() {
+    int N;
+    int A[MAX_SIZE][MAX_SIZE];
+    int B[MAX_SIZE][MAX_SIZE];
+    char op;
+	int i,j;
+    scanf("%d", &N);
+
+    for (i = 0; i < N; i++) {
+        for (j = 0; j < N; j++) {
+            scanf("%d", &A[i][j]);
+        }
+    }
+
+    while (1) {
+        getchar(); 
+        scanf("%c", &op); 
+
+		        if (op == '#') {
+            break;
+        }
+
+        for (i = 0; i < N; i++) {
+            for (j = 0; j < N; j++) {
+                scanf("%d", &B[i][j]);
+            }
+        }
+
+        if (op == '+') {
+            matrix_add(A, B, N);
+        } else if (op == '-') {
+            matrix_subtract(A, B, N);
+        }
+    }
+
+    print_matrix(A, N);
+
+    return 0;
+}

+ 29 - 0
code/加强训练任务/给一个整型数组编号b.c

@@ -0,0 +1,29 @@
+#include <stdio.h>
+
+int main() {
+    int n;
+    int i,j;
+    scanf("%d", &n);
+
+    int arr[20];
+    for (i = 0; i < n; i++) {
+        scanf("%d", &arr[i]);
+    }
+
+    int count = 1;
+    for (i = 0; i < n; i++) {
+        if (arr[i] != -9999) {
+            for (j = i + 1; j < n; j++) {
+                if (arr[j] == arr[i]) {
+                    arr[j] = -9999;
+                }
+            }
+            printf("%d ", count);
+            count++;
+        }
+    }
+
+    printf("\n");
+
+    return 0;
+}

+ 42 - 0
code/加强训练任务/统计两个一维数组中a和b的关系.c

@@ -0,0 +1,42 @@
+#include <stdio.h>
+
+void count_elements(int a[], int b[], int c[], int size_a, int size_b) {
+    int i, j, count;
+    for (i = 0; i < size_b - 1; i++) {
+        count = 0;
+        for (j = 0; j < size_a; j++) {
+            if (b[i] < a[j] && a[j] < b[i + 1]) {
+                count++;
+            }
+        }
+        c[i] = count;
+    }
+}
+
+int main() {
+    int a[10];
+    int b[5];
+    int c[4]; // Assuming c has 4 elements based on the size of b
+	int i;
+    // Input a and b
+   
+    for (i = 0; i < 10; i++) {
+        scanf("%d", &a[i]);
+    }
+
+   
+    for (i = 0; i < 5; i++) {
+        scanf("%d", &b[i]);
+    }
+
+    count_elements(a, b, c, 10, 5);
+
+    // Output array c
+    printf("c = ");
+    for (i = 0; i < 4; i++) {
+        printf("%d ", c[i]);
+    }
+    printf("\n");
+
+    return 0;
+}

+ 23 - 0
code/加强训练任务/编写程序 创建文本文件mul_table.txt其内容为九九乘法表.c

@@ -0,0 +1,23 @@
+#include <stdio.h>
+
+int main() {
+    int i, j;
+
+    // 输出表头
+    printf("      ");
+    for (i = 1; i <= 9; i++) {
+        printf("%4d", i);
+    }
+    printf("\n");
+
+    // 输出九九乘法表
+    for (i = 1; i <= 9; i++) {
+        printf("%4d", i);
+        for (j = 1; j <= 9; j++) {
+            printf("%4d", i * j);
+        }
+        printf("\n");
+    }
+
+    return 0;
+}

BIN
code/加强训练任务/编写程序 创建文本文件mul_table.txt其内容为九九乘法表.exe


+ 19 - 0
code/加强训练任务/计算n个a相减.c

@@ -0,0 +1,19 @@
+#include <stdio.h>
+
+int main() {
+    int a, n;
+    scanf("%d %d", &a, &n);
+
+    int num = 0;
+    int i;
+    long long sum = 0;
+    
+    for (i = 0; i < n; i++) {
+        num = num * 10 + a;
+        sum += num;
+    }
+
+    printf("%lld\n", sum);
+    
+    return 0;
+}

BIN
code/加强训练任务/计算n个a相减.exe


+ 20 - 0
code/加强训练任务/计算公式b.c

@@ -0,0 +1,20 @@
+#include <stdio.h>
+
+int main() {
+    int n,i,j;
+    double sum = 0.0;
+
+    scanf("%d", &n);
+
+    for (i = 1; i <= n; i++) {
+        double temp = 0.0;
+        for (j = 1; j <= i; j++) {
+            temp += j;
+        }
+        sum += 1.0 / temp;
+    }
+
+    printf("%.4f\n", sum);
+
+    return 0;
+}

+ 25 - 0
code/加强训练任务/计算过道和栏杆的造价.c

@@ -0,0 +1,25 @@
+#include <stdio.h>
+
+int main() {
+    double radius, pool_area, outer_radius, fence_cost, pathway_cost;
+
+    const double PI = 3.14159;
+    const double fence_price_per_meter = 55.0;
+    const double pathway_price_per_sqm = 40.0;
+    const double pathway_width = 3.0;
+
+    scanf("%lf", &radius);
+
+    pool_area = PI * radius * radius;
+
+    outer_radius = radius + pathway_width;
+
+    fence_cost = 2 * PI * outer_radius * fence_price_per_meter+1;
+
+    double pathway_area = PI * outer_radius * outer_radius - pool_area;
+    pathway_cost = pathway_area * pathway_price_per_sqm+1;
+
+    printf("%d %d\n", (int)pathway_cost, (int)fence_cost);
+
+    return 0;
+}

BIN
code/加强训练任务/计算过道和栏杆的造价.exe


+ 37 - 0
code/加强训练任务/输入年月日求第几天.c

@@ -0,0 +1,37 @@
+#include <stdio.h>
+
+int isLeapYear(int year) {
+    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
+        return 1; // ÊÇÈòÄê
+    } else {
+        return 0; // ²»ÊÇÈòÄê
+    }
+}
+
+int getDayOfYear(int year, int month, int day) {
+    int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+    int dayOfYear = 0;
+	int i;
+    for (i = 1; i < month; i++) {
+        dayOfYear += daysInMonth[i];
+    }
+
+    dayOfYear += day;
+
+    if (isLeapYear(year) && month > 2) {
+        dayOfYear++;
+    }
+
+    return dayOfYear;
+}
+
+int main() {
+    int year, month, day;
+
+    scanf("%d %d %d", &year, &month, &day);
+
+    int dayOfYear = getDayOfYear(year, month, day);
+    printf("%d\n", dayOfYear);
+
+    return 0;
+}

+ 14 - 0
code/加强训练任务/输入秒数转换成几分几秒.c

@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main() {
+    int s, min, remaining_seconds;
+
+    scanf("%d", &s);
+
+    min = s / 60; // 计算分钟数
+    remaining_seconds = s % 60; // 计算剩余的秒数
+
+    printf("%dmin%ds\n", min, remaining_seconds);
+
+    return 0;
+}

BIN
code/加强训练任务/输入秒数转换成几分几秒.exe


+ 40 - 0
code/加强训练任务/连续正整数的和.c

@@ -0,0 +1,40 @@
+#include <stdio.h>
+
+int main() {
+    int x;
+    FILE *input = fopen("scpi.in", "r");
+    FILE *output = fopen("scpi.out", "w");
+
+    fscanf(input, "%d", &x);
+
+    int start = 1, end = 2;
+    int sum = start + end;
+	int i;
+    while (start < end) {
+        if (sum == x) {
+            fprintf(output, "%d=", x);
+            for (i = start; i <= end; i++) {
+                fprintf(output, "%d", i);
+                if (i < end) {
+                    fprintf(output, "+");
+                }
+            }
+            break;
+        } else if (sum < x) {
+            end++;
+            sum += end;
+        } else {
+            sum -= start;
+            start++;
+        }
+    }
+
+    if (start >= end) {
+        fprintf(output, "%d:NOANSWER", x);
+    }
+
+    fclose(input);
+    fclose(output);
+
+    return 0;
+}

+ 19 - 0
code/加强训练任务/限定条件求x3+y3.c

@@ -0,0 +1,19 @@
+#include <stdio.h>
+
+int main() {
+    long a, b;
+    
+    // 从标准输入读取两个整数
+    scanf("%ld %ld", &a, &b);
+
+    // 计算 xy
+    long xy = (a * a - b) / 2;
+
+    // 计算 x^3 + y^3
+    long x3y3 = a * (b - xy);
+
+    // 输出结果
+    printf("%ld\n", x3y3);
+
+    return 0;
+}

BIN
code/加强训练任务/限定条件求x3+y3.exe