河北专升本(c语言各种编程题)

目录

第一类、递归调用

第二类、特殊数字

第三类、多维数组

第四类、字符处理

第五类、数学问题

第六类、排序算法

第七类、循环问题

 第八类、进制转换

 第九类、实际应用

第十类、图形输出


第一类、递归调用

1.汉诺塔:请输入盘子数,输出盘子移动的操作步骤。

#include <stdio.h> 
void move(char from, char to) {
    printf("%c to %c\n", from, to);
}
void hanoi(int n, char a, char b, char c) {
    if (n == 1)
       move(a, c);
    else {
       hanoi(n - 1, a, c, b);
       move(a, c);
       hanoi(n - 1, b, a, c);
    }
}
void main() {
    int n;
    scanf("%d", &n);
    hanoi(n, 'A', 'B', 'C');
}

2.爬楼梯:树老师爬楼梯,他可以每次走 1 级或者 2 级,输入楼梯的级数,求不同的走法数。

#include <stdio.h> 
int stair(int n) {
    if (n == 1) return 1;
    if (n == 2) return 2;
    return stair(n - 1) + stair(n - 2);
}
void main() {
    int n;
    scanf("%d", &n);
    printf("%d", stair(n));
}

3.爬楼梯:树老师爬楼梯,他可以每次走 1 级、2 级或者 3 级,输入楼梯的级数,求不同的走法数。

#include <stdio.h> 
int stair(int n) {
    if (n == 1) return 1;
    if (n == 2) return 2;
    if (n == 3) return 4;
    return stair(n - 1) + stair(n - 2) + stair(n - 3);
}
void main() {
    int n;
    scanf("%d", &n);
    printf("%d", stair(n));
}

4.斐波那契数列:请输入项数,输出具体数列。

#include <stdio.h> 
int fibonacci(int n) {
    if (n == 1 || n == 2)
    return 1;
    return fibonacci(n - 1) + fibonacci(n - 2);
}
void main() {
    int n, i;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    printf("%d,", fibonacci(i));
}

5.求阶乘:请输入整数 n,求 1!+2!+3!+4!+5!+6!+7!+...+n!的和。

#include <stdio.h> 
int factorial(int n) {
    if (n == 1) return 1;
    return n * factorial(n - 1);
}
void main() {
    int n, i, sum = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
       sum += factorial(i);
       printf("sum=%d", sum);
}

6.取球问题:在 n 个球中,任意取 m 个(不放回),求有多少种不同取法。

#include <stdio.h> 
int ball(int n, int m) {
    if (n < m) return 0;
    if (n == m) return 1;
    if (m == 0) return 1;
    return ball(n - 1, m - 1) + ball(n - 1, m);
}
void main() {
    int n, m;
    scanf("%d%d", &n, &m);
    printf("%d", ball(n, m));
}

7.杨辉三角:输入要打印的层数,打印杨辉三角。

#include <stdio.h> 
int triangle(int m, int n) {
      if (m == 0 || n == 0 || m == n)
          return 1;
      return triangle(m - 1, n) + triangle(m - 1, n - 1);
}
void main() {
     int n, i, j;
     scanf("%d", &n);
     for (i = 0; i < n; i++) {
         for (j = 0; j <= i; j++) {
             printf("%d ", triangle(i, j));
         }
         printf("\n");
      }
}

8.求年龄:有 5 个人坐在一起,问第 5 个人多少岁,他说比第 4 个人大 2 岁。问 第 4 个人多少岁,他说比第 3 个人大 2 岁。问第 3 个人多少岁,他说比第 2 个 人大 2 岁。问第 2 个人多少岁,他说比第 1 个人大 2 岁。最后问第 1 个人,他 说是 10 岁。请问第 5 个人多大?

#include <stdio.h> 
int age(int n) {
    if (n == 1) return 10;
    return age(n - 1) + 2;
}
void main() {
    printf("%d", age(5));
}

9.猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃 了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都 吃了前一天剩下的一半多一个。到第十天早上想再吃时,见只剩下一个桃子了。 问最初有多少个桃子。 递归:

递归:
#include <stdio.h> 
int peach(int n) {
    if (n == 10) return 1;
    return (peach(n + 1) + 1) * 2;
}
void main() {
    printf("%d", peach(1));
}

循环:
#include <stdio.h> 
void main() {
    int i, s = 1;
    for (i = 9; i >= 1; i--) {
        s = (s + 1) * 2;
    }
    printf("%d", s);
}

10.猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多 吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上 都吃了前一天剩下的一半多一个。第十天同样是吃了前一天的一半加一个,最后剩下一个桃子。问最初有多少个桃子。 递归:

递归:
#include <stdio.h> 
int peach(int n) {
    if (n == 11) return 1;
    return (peach(n + 1) + 1) * 2;
}
void main() {
    printf("%d", peach(1));
}

循环:
#include <stdio.h> 
void main() {
    int i, s = 1;
    for (i = 10; i >= 1; i--) {
        s = (s + 1) * 2;
    }
    printf("%d", s);
}

11.最大公约数:利用递归算法求两个数的最大公约数。

#include <stdio.h> 
int gcd(int a, int b) {
    int t;
    if (a < b) {
       t = a;
       a = b;
       b = t;
    }
    if (b == 0) {
       return a;
    }
    return gcd(b, a % b);
}
void main() {
    int a, b;
    scanf("%d%d", &a, &b);
    printf("gcd=%d", gcd(a, b));
}

12.逆序输出:输入一个正整数,将该正整数逆序输出。

#include <stdio.h> 
void printDigit(int n) {
    printf("%d", n % 10);
    if (n > 10) {
       printDigit(n / 10);
    }
}
void main() {
    int n;
    scanf("%d", &n);
    printDigit(n);
}

13.逆序输出:输入一个字符串,将该字符串逆序输出。

#include <stdio.h> 
void printStr(char *str) {
    if (*str != '\0')
       printStr(str + 1);
    if (*str != '\0')
       printf("%c", *str);
}
void main() {
    char str[100];
    gets(str);
    printStr(str);
}

第二类、特殊数字

1.奇数:输出 1-1000 之间所有的奇数。

#include <stdio.h> 
void main() {
    int i;
    for (i = 1; i <= 1000; i++){
        if (i % 2 == 1)
           printf("%d,", i);
    }
}

2.偶数:输出 1-1000 之间所有的偶数。

#include <stdio.h> 
void main() {
    int i;
    for (i = 1; i <= 1000; i++)
        if (i % 2 == 0)  printf("%d,", i);
}

3.素数:输出 1-1000 之间所有的素数。

素数一般指质数。质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。
#include <stdio.h> 
void main() {
    int i, j;
    for (i = 1; i <= 1000; i++) {
        for (j = 2; j < i; j++){
            if (i % j == 0) break;
        }
        if (i == j) printf("%d,", i);
 }
}

4.完数:输出 1-1000 之间所有的完数。

完数,也称为完美数或完备数,是指一个正整数,它的所有因子(除了它本身)的和等于它本身
#include <stdio.h> 
void main() {
    int i, j, s;
    for (i = 1; i <= 1000; i++) {
        s = 0;
       for (j = 1; j < i; j++){
           if (i % j == 0) s += j;
       }
     if (i == s)  printf("%d,", i);
 }
}

5.回文数:输出 100-999 之间所有的回文数。

回文数是指在正读和反读时都保持一致的数字,即数字的左右两边或者上下两边对称
#include <stdio.h> 
void main() {
    int i, g, b;
    for (i = 100; i <= 999; i++) {
        g = i % 10;
        b = i / 100;
        if (g == b) printf("%d,", i);
 }
}

6.水仙花数:输出 100-999 之间所有的水仙花数。

水仙花数是指一个 3 位数,它的每个数位上的数字的 3次幂之和等于它本身
#include <stdio.h> 
void main() {
    int i, g, s, b;
    for (i = 100; i <= 999; i++) {
        g = i % 10;
        s = i / 10 % 10;
        b = i / 100;
        if (g * g * g + s * s * s + b * b * b == i) printf("%d,", i);
     }
}

7.中位数:计算有限个数的数据的中位数的方法是:把所有的同类数据按照大小 的顺序排列。如果数据的个数是奇数,则中间那个数据就是这群数据的中位数; 如果数据的个数是偶数,则中间那 2 个数据的算术平均值就是这群数据的中位 数。现在给出 n 个正整数,求它们的中位数。

#include <stdio.h> 
/* 冒泡排序 */
void sort(int a[], int n) {
     int i, j, t;
     for (i = 0; i < n - 1; i++) {
         for (j = 0; j < n - 1 - i; j++) {
             if (a[j] > a[j + 1]) {
                t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
             }
          }
     }
}
void main() {
    int a[100], n, i;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
         scanf("%d", &a[i]);
    }  
    sort(a, n);
    if (n % 2 == 1) {
       printf("%d", a[n / 2]);
    } else {
       printf("%.2f", (a[n / 2] + a[n / 2 - 1]) / 2.0);
    }
}

8.完全平方数:若一个数能表示成某个数的平方的形式,则称这个数为完全平方数。

#include <stdio.h> 
#include <math.h> 
int ISquare(int n) {
    int m = (int) sqrt(n);
    if (m * m == n)
       return m;
    return 0;
}
void main() {
    int n, p;
    scanf("%d", &n);
    if (p = ISquare(n))
       printf("%d 是完全平方数,%d=%d*%d\n", n, n, p, p);
    else 
       printf("%d 非完全平方数\n", n);
}

9.随机数:生成 100 个 1(含 1)-100(含 100)之间的随机数。

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
int getRand(int min, int max) {
    return rand() % (max - min + 1) + min;
}
void main() {
    int i;
    srand(time(NULL));
    for (i = 1; i <= 100; i++) {
        printf("%d\n", getRand(1, 100));
    }
}

10.求年份:输出 2000(含 2000)-2020(含 2020)之间所有的闰年年份。

能被4整除但不能被100整除,或能被400整除
#include <stdio.h> 
void main() {
    int y;
    for (y = 2000; y <= 2020; y++)
       if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))  printf("%d,", y);
}

11.求数字:求两个整数的最大公约数及最小公倍数。

#include <stdio.h>  
/* 最大公约数 */
int gcd(int a, int b) {
    int t;
    if (a < b) {
       t = a;
       a = b;
       b = t;
    }
    while (b != 0) {
       t = a % b;
       a = b;
       b = t;
    }
    return a;
}
/* 最小公倍数 */
int lcm(int a, int b, int c) {
    return a * b / c;
}
void main() {
    int x, y, a, b;
    scanf("%d%d", &a, &b);
    x = gcd(a, b);
    y = lcm(a, b, x);
    printf("gcd=%d,lcm=%d", x, y);
}

12.求数字:一个整数,它加上 100 后是一个完全平方数,它加上 168 又是一个 完全平方数,请问该数是多少?

#include <stdio.h> 
#include <math.h> 
void main() {
    int i = 1, x, y;
    while (1) {
       x = (int) sqrt(i + 100);
       y = (int) sqrt(i + 168);
       if ((x * x == i + 100) && (y * y == i + 168)) {
          printf("%d", i);
          break;
       }
       i++;
    }
}

14.求数字:求所有的四位数中,原数的 9 倍与其逆序相等的数。

#include <stdio.h> 
void main() {
    int i, a, b, c, d;
    for (i = 1000; i <= 9999; i++) {
        a = i % 10;
        b = i / 10 % 10;
        c = i / 100 % 10;
        d = i / 1000;
        if (9 * i == a * 1000 + b * 100 + c * 10 + d) printf("%d", i);
    }
}

15.求数字:输出 2000(含 2000)-3000(含 3000)之间所有十位数是 m(0<=m<=9) 且是 n 的倍数的数。

#include <stdio.h> 
void main() {
    int m, n, i;
    scanf("%d %d", &m, &n);
    for (i = 2000; i <= 3000; i++)
        if (i / 10 % 10 == m && i % n == 0)  printf("%d,", i);
}

16.求数字:输入一个整数 n,输出 100(含 100)-999(含 999)之间所有各位 数字之和等于 n 的数。

#include <stdio.h> 
void main() {
    int n, a, b, c, i;
    scanf("%d", &n);
    for (i = 100; i <= 999; i++) {
        a = i / 100;
        b = i / 10 % 10;
        c = i % 10;
        if (a + b + c == n) printf("%d,", i);
     }
}

17.求数字:求 1(含 1)-200(含 200)中,能同时被 2、5 除余 1 的整数。

#include <stdio.h> 
void main() {
    int i;
    for (i = 1; i <= 200; i++)
        if (i % 2 == 1 && i % 5 == 1) printf("%d,", i);
}

18.求数字:输出 100(含 100)-999(含 999)之间所有是 7 的倍数的回文数。

#include <stdio.h> 
void main() {
    int i, a, c;
    for (i = 100; i <= 999; i++) {
        a = i / 100;
        c = i % 10;
        if (a == c && i % 7 == 0)  printf("%d,", i);
    }
}

19.求数字:输出 100(含 100)-200(含 200)以内的十位数字为 5,百位和个位的和是 6 的倍数的所有的数。

#include <stdio.h> 
void main() {
    int i, a, b, c;
    for (i = 100; i <= 200; i++) {
        a = i / 100;
        b = i / 10 % 10;
        c = i % 10;
     if (b == 5 && (a + c) % 6 == 0)  printf("%d,", i);
    }
}

20.求数字:输出 100(含 100)-200(含 200)以内的满足以下条件的数,条件为:这个数与 3 的和是 5 的倍数,与 3 的差是 6 的倍数,输出这样的数。

#include <stdio.h> 
void main() {
    int i;
    for (i = 100; i <= 200; i++)
       if ((i + 3) % 5 == 0 && (i - 3) % 6 == 0)  printf("%d,", i);
}

21.求数字:找出乘积为 399 的两个相邻奇数。

#include <stdio.h> 
void main() {
    int i = 1;
    while (i * (i + 2) != 399) i = i + 2;
    printf("%d,%d", i, i + 2);
}

22.求位数:输入一个正整数,输出它是几位数?

#include <stdio.h> 
void main() {
   int n, count = 1;
   scanf("%d", &n);
   while (n = n / 10) {
         count++;
   }
   printf("%d", count);
}

23.求数和:输入一个正整数,求其各位之和。

#include <stdio.h> 
void main() {
    int n, sum = 0;
    scanf("%d", &n);
    while (n > 0) {
      sum += n % 10;
      n = n / 10;
    }
    printf("%d", sum);
}

24.相反数:输入一个正整数,输出它的相反数。

#include <stdio.h> 
void main() {
    int n, sum = 0;
    scanf("%d", &n);
    while (n > 0) {
       sum = sum * 10 + n % 10;
       n = n / 10;
    }
    printf("%d", sum);
}

第三类、多维数组

1.上下对称反转

#include <stdio.h> 
void main() {
    int arr[4][5] = {{1, 2, 3, 4, 5},
                     {6, 7, 8, 9, 10},
                     {11, 12, 13, 14, 15},
                     {16, 17, 18, 19, 20},};
    int i, j, t;
    int rows = sizeof(arr) / sizeof(arr[0]);
    int cols = sizeof(arr[0]) / sizeof(arr[0][0]);
    for (i = 0; i < rows / 2; i++) {
         for (j = 0; j < cols; j++) {
             t = arr[rows - 1 - i][j];
             arr[rows - 1 - i][j] = arr[i][j];
             arr[i][j] = t;
          }
     }
     for (i = 0; i < rows; i++) {
         for (j = 0; j < cols; j++)  printf("%-5d", arr[i][j]);
         printf("\n");
     }
}

2.左右对称反转

#include <stdio.h> 
void main() {
    int arr[4][5] = {{1, 2, 3, 4, 5},
                     {6, 7, 8, 9, 10},
                     {11, 12, 13, 14, 15},
                     {16, 17, 18, 19, 20},};
    int i, j, t;
    int rows = sizeof(arr) / sizeof(arr[0]);
    int cols = sizeof(arr[0]) / sizeof(arr[0][0]);
    for (i = 0; i < rows; i++) {
         for (j = 0; j < cols / 2; j++) {
             t = arr[i][cols - 1 - j];
             arr[i][cols - 1 - j] = arr[i][j];
             arr[i][j] = t;
          }
     }
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) printf("%-5d", arr[i][j]);
        printf("\n");
     }
}

3.上下左右反转

#include <stdio.h> 
void main() {
    int arr[4][5] = {{1, 2, 3, 4, 5},
                     {6, 7, 8, 9, 10},
                     {11, 12, 13, 14, 15},
                     {16, 17, 18, 19, 20},};
    int i, j, t;
    int rows = sizeof(arr) / sizeof(arr[0]);
    int cols = sizeof(arr[0]) / sizeof(arr[0][0]);
    /* 上下对称反转 */
    for (i = 0; i < rows / 2; i++) {
         for (j = 0; j < cols; j++) {
              t = arr[rows - 1 - i][j];
              arr[rows - 1 - i][j] = arr[i][j];
              arr[i][j] = t;
          }
     }
     /* 左右对称反转 */
     for (i = 0; i < rows; i++) {
         for (j = 0; j < cols / 2; j++) {
             t = arr[i][cols - 1 - j];
             arr[i][cols - 1 - j] = arr[i][j];
             arr[i][j] = t;
         }
     }
     /* 二维数组打印 */
     for (i = 0; i < rows; i++) {
         for (j = 0; j < cols; j++) printf("%-5d", arr[i][j]);
         printf("\n");
      }
}

4.判断 a[N][N]是否关于主对角线对称(左斜),若对称输出 1,否则输出 0。

#include <stdio.h> 
#define N 5
void main() {
    int a[N][N], i, j, flag = 1;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++) scanf("%d", &a[i][j]);
    for (i = 0; i < N; i++)
        for (j = 0; j < i; j++)
            if (a[i][j] != a[j][i]) {
               flag = 0;
               break;
            }
    if (flag == 1)
        printf("1");
    else 
        printf("0");
}

5.判断 a[N][N]是否关于次对角线对称(右斜),若对称输出 1,否则输出 0。

#include <stdio.h> 
#define N 5
void main() {
    int a[N][N], i, j, flag = 1;
    for (i = 0; i < N; i++)
         for (j = 0; j < N; j++)  scanf("%d", &a[i][j]);
    for (i = 0; i < N; i++)
        for (j = 0; j < N - 1 - i; j++)
            if (a[i][j] != a[N - 1 - j][N - 1 - i]) {
                flag = 0;
                break; 
            }
    if (flag == 1)
        printf("1");
    else 
        printf("0");
}

6.分别求出 N 阶方阵 a 中两个对角线上元素之和。

#include <stdio.h> 
#define N 5
void main() {
    int a[N][N], i, j, k = N, pr1 = 0, pr2 = 0;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)  scanf("%d", &a[i][j]);
    for (i = 0; i < N; i++) {
         pr1 += a[i][i];
         k = k - 1;
         pr2 += a[i][k];
    }
    printf("pr1=%d\n", pr1);
    printf("pr2=%d\n", pr2);
}

7.N 阶方阵 a 进行转置,输出行列互换后的方阵 a。

#include <stdio.h> 
#define N 5
void main() {
    int a[N][N], i, j, t;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)   scanf("%d", &a[i][j]);
    for (i = 0; i < N; i++)
         for (j = 0; j < i; j++) {
             t = a[i][j];
             a[i][j] = a[j][i];
             a[j][i] = t;
          }
     for (i = 0; i < N; i++) {
         for (j = 0; j < N; j++) printf("%-5d", a[i][j]);
         printf("\n");
     }
}

8.N 阶方阵 a 加上方阵 a 的转置存放在方阵 b 中,输出方阵 b。

#include <stdio.h> 
#define N 5
void main() {
    int a[N][N], b[N][N], i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)  scanf("%d", &a[i][j]);
    for (i = 0; i < N; i++)
         for (j = 0; j < N; j++) b[i][j] = a[i][j] + a[j][i];
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) printf("%d ", b[i][j]);
        printf("\n");
 }
}

第四类、字符处理

1.编写函数:int cmp(char *str1,char *str2),实现字符串的比较。

#include <stdio.h> 
int cmp(char *str1, char *str2) {
    while (*str1 == *str2 && *str1 != '\0') {
          str1++;
          str2++;
    }
    return *str1 - *str2;
}
void main() {
    char s1[100] = "ABCDEF1";
    char s2[100] = "ABCDEF0";
    printf("%d", cmp(s1, s2));
}

2.编写函数:void cpy(char *dest,char *src),实现字符串的拷贝。

#include <stdio.h> 
void cpy(char *dest, char *src) {
    while (*dest++ = *src++);
}
void main() {
    char s1[100] = "AABCD123ABCD123ABCDA";
    char s2[100] = "123";
    cpy(s1, s2);
    puts(s1);
}

3.编写函数:void cat(char *dest,char *src),实现字符串的追加。

#include <stdio.h> 
void cat(char *dest, char *src) {
    while (*dest++);
    dest--;
    while (*dest++ = *src++);
}
void main() {
    char s1[100] = "AABCD123ABCD123ABCDA";
    char s2[100] = "123";
    cat(s1, s2);
    puts(s1);
}

4.编写函数:void reverse(char *str),实现字符串的反转。

#include <stdio.h> 
void reverse(char *str) {
    char *start = str, *end = str, t;
    while (*end++);
    end -= 2;
    while (start < end) {
         t = *start;
         *start++ = *end;
         *end-- = t;
 }
}
void main() {
    char str[100] = "123456789";
    reverse(str);
    puts(str);
}

5.编写函数:int len(char *str),判断字符串的长度。

#include <stdio.h> 
int len(char *str) {
    char *p = str;
    while (*str++);
    return str - 1 - p;
}
void main() {
    printf("%d", len("AABCD123ABCD123ABCDA"));
}

6.编写函数:int strchc(char *str, char c),实现统计 str 字符串中指定字符出现的个数。

#include <stdio.h> 
int strchc(char *str, char c) {
    int count = 0;
    while (*str) {
      if (*str == c) {
           count++;
      }
      str++;
    }
    return count;
}
void main() {
    printf("%d", strchc("AABCD123ABCD123ABCDA", 'A'));
}

7.编写函数:int strstrc(char *str, char *substr),实现统计 str 字符串 中指定字符串出现的个数。

#include <stdio.h> 
int strstrc(char *str, char *substr) {
    int count = 0;
    char *pb, *ps;
    while (*str) {
       pb = str;
       ps = substr;
       while (*ps) {
           if (*pb == *ps) {
           pb++;
           ps++;
           } else {
              break;
           }
       }
       if (*ps == '\0') {
          count++;
       }
       str++;
    }
    return count;
}
void main() {
    printf("%d", strstrc("AABCD123ABCD123ABCDA", "ABCD"));
}

8.编写函数:void delch(char *str, char c),实现删除 str 字符串中出现所有指定字符。

#include <stdio.h> 
void delch(char *str, char c) {
    char *p = str;
    while (*str) {
       if (*str != c) {
       *p++ = *str;
       }
    str++;
    }
    *p = '\0';
}
void main() {
    char s1[100] = "AABCD123ABCD123ABCDA";
    char c = 'A';
    delch(s1, c);
    puts(s1);
}

9.编写函数:char *delstr(char *str, char *substr),实现删除 str 字符串 中出现所有指定字符串。

#include <stdio.h> 
#include <string.h> 
char *delstr(char *str, char *substr) {
    char *pb = str, *q, *c;
    int n = strlen(substr);
    while (q = strstr(pb, substr)) {
          c = q + n;
          *q = '\0';
          pb = strcat(pb, c);
     }
    return pb;
}
void main() {
    char s1[100] = "AABCD123ABCD123ABCDA";
    char s2[100] = "ABCD";
    delstr(s1, s2);
    puts(s1);
}

10.编写函数:void repch(char *str, char subch, char repch),将 str 字 符串中出现的所有字符 subch 替换为 repch。

#include <stdio.h> 
void repch(char *str, char subch, char repch) {
    while (*str) {
        if (*str == subch) {
           *str = repch;
        }
        str++;
    }
}
void main() {
    char str[100] = "abcdefabcdefabcdef";
    repch(str, 'a', 'A');
    puts(str);
}

11.编写函数:void repstr(char *str, char *substr, char *repstr),将 str 字符串中出现的所有子串 substr 替换为 repstr。

#include <stdio.h> 
void repstr(char *str, char *substr, char *repstr) {
    char *pb, *ps;
    while (*str) {
         pb = str;
         ps = substr;
         while (*ps) {
             if (*pb == *ps) {
                pb++;
                ps++;
             } else {
                 break;
             }
         }
         if (*ps == '\0') {
             ps = repstr;
             while (*ps) *str++ = *ps++;
         } else {
             str++;
         }
     }
}
void main() {
    char str[100] = "abcdefabcdefabcdef";
    repstr(str, "abc", "XYZ");
    puts(str);
}

12.编写函数:char *strch(char *str, char c),如果指定字符是 str 字符串 的元素,返回元素所在的首地址,否则,返回 NULL。

char *strch(char *str, char c) {
    while (*str) {
      if (*str == c) {
         return str;
      }
     str++;
    }
    return NULL;
}
void main() {
    char s1[100] = "AABCD123ABCD123ABCDA";
    char c = '1';
    printf("%s", strch(s1, c));
}

13.编写函数:char *strstr(char *str, char *substr),如果指定字符串是 str 字符串的元素,返回元素所在的首地址,否则,返回 NULL。

#include <stdio.h> 
char *strstr(char *str, char *substr) {
    char *pb, *ps;
    while (*str) {
      pb = str;
      ps = substr;
      while (*ps) {
         if (*pb == *ps) {
            pb++;
            ps++;
         } else { 
           break;
         }
      }
      if (*ps == '\0') {
         return str;
      }
      str++;
    }
    return NULL;
}
void main() {
    char s1[100] = "AABCD123ABCD123ABCDA";
    char s2[100] = "123";
    printf("%s", strstr(s1, s2));
}

14.输入多个字符串,输出其中最短的第一个字符串。

#include <stdio.h> 
#include <string.h> 
void main() {
    char str[100] = {0}, min[100] = {0};
    while (1) {
       printf("Please enter a string:\n");
       gets(str);
       if (str[0] == '\0') {
           break;
       }
       if (strlen(str) >= strlen(min)) {
           strcpy(min, str);
       }
    }
    printf("min=%s\n", min);
}

第五类、数学问题

1.鸡兔同笼:假设鸡兔共 30 只,脚 88 只,问鸡兔各有多少只?

#include <stdio.h> 
void main() {
   int i, j;
   for (i = 0; i <= 30; i++) {
       j = 30 - i;
       if (2 * i + 4 * j == 88)  printf("%d,%d", i, j);
   }
}

2.百钱百鸡:100 元钱买 100 只鸡,公鸡 5 块钱一只,母鸡 3 块钱一只,小鸡一 块钱 3 只,请输出所有组合。

#include <stdio.h> 
void main() {
    int a, b, c;
    for (a = 0; a <= 20; a++) {
        for (b = 0; b <= 33; b++) {
            c = 100 - a - b;
           if (a * 5 + b * 3 + c / 3 == 100 && c % 3 == 0)
               printf("%d,%d,%d\n", a, b, c);
        }
    }
}

3.斐波那契数列:请用循环输出斐波那契数列的前 20 项。

#include <stdio.h>
void main() {
    int i, a = 1, b = 1;
    for (i = 1; i <= 10; i++) {
         printf("%d,%d,", a, b);
         a = a + b;
         b = a + b;
 }
}

4.兔子数列(又称斐波那契数列):有一对兔子,从出生后第 3 个月起每个月都 生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问 前 10 个月,每个月有兔子多少对?

#include <stdio.h> 
int fibonacci(int n) {
     if (n == 1 || n == 2)
     return 1;
     return fibonacci(n - 1) + fibonacci(n - 2);
}
void main() {
    int i;
    for (i = 1; i <= 10; i++) {
       printf("the %d month has %d\n", i, fibonacci(i));
    }
}

5.兔子数列(又称斐波那契数列):有一对兔子,从出生后第三个月起每个月都 生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,请 问第一个月出生的一对兔子,至少需要繁衍到第几个月时兔子总数才可以达到 N 对?

#include <stdio.h> 
int fibonacci(int n) {
    if (n == 1 || n == 2)
    return 1;
    return fibonacci(n - 1) + fibonacci(n - 2);
}
void main() {
    int m = 1, n, s = 0;
    scanf("%d", &n);
    while ((s = fibonacci(m)) < n) {
          m++;
    }
    printf("到第%d 个月,才有%d 对兔子,已有%d 对兔子\n", m, n, s);
}

6.数字组合:有 1、2、3、4,四个数字,能组成多少个互不相同且无重复数字的 三位数?

#include <stdio.h> 
void main() {
    int a, b, c;
    for (a = 1; a < 5; a++)
       for (b = 1; b < 5; b++)
           for (c = 1; c < 5; c++)
               if ((a != b) && (a != c) && (b != c))
                   printf("%d,%d,%d\n", a, b, c);
}

7.字母组合:有 A、B、C、D,四个字母,能组成多少个互不相同且无重复三位组 合?

#include <stdio.h> 
void main() {
    int a, b, c;
    for (a = 65; a < 69; a++)
        for (b = 65; b < 69; b++)
            for (c = 65; c < 69; c++)
               if ((a != b) && (a != c) && (b != c))
                   printf("%c,%c,%c\n", a, b, c);
}

8.比赛抽签:两个乒乓球队进行比赛,各出三人。甲队为 a,b,c 三人,乙队为 x,y,z 三人。已抽签决定比赛名单,有人向队员打听比赛的名单,a 说他不和 x 比,c 说他不和 x,z 比,请编程序找出三队赛手的名单。

#include <stdio.h> 
 
void main() {
    char a, b, c;
    for (a = 'x'; a <= 'z'; a++)
        for (b = 'x'; b <= 'z'; b++)
            for (c = 'x'; c <= 'z'; c++)
                if (a != b && a != c && b != c)
                   if (a != 'x' && c != 'x' && c != 'z')
                      printf("order as a--%c\tb--%c\tc--%c\n",a, b, c);
}

9.韩信点兵:韩信有一队兵,他想知道有多少人,便让士兵排队报数。按从 1 至 5 报数,最末一个士兵报的数为 1;按从 1 至 6 报数,最末一个士兵报的数为 5; 按从 1 至 7 报数,最末一个士兵报的数为 4;最后再按从 1 至 11 报数,最末一 个士兵报的数为 10。计算韩信至少有多少兵。

#include <stdio.h> 
void main() {
    int x;
    for (x = 1;; x++) {
        if (x % 5 == 1 && x % 6 == 5 && x % 7 == 4 && x % 11 == 10) {
            printf("%d", x);
            break;
        }
     }
}

10.爱因斯坦:有一条长阶梯,若每步跨 2 阶,最后剩下 1 阶;若每步跨 3 阶, 最后剩下 2 阶;若每步跨 5 阶,最后剩下 4 阶;若每步跨 6 阶,最后剩下 5 阶; 只有每步跨 7 阶,最后才正好 1 阶不剩。计算这条阶梯共有多少阶。

#include <stdio.h> 
void main() {
    int x;
    for (x = 1;; x++) {
         if (x%2==1 && x%3==2 && x%5==4 && x%6==5 && x%7==0) {
            printf("%d", x);
         break;
 }
 }
}

11.哥德巴赫猜想:任何一个大于 2 的偶数总能表示为两个素数之和。比如: 24=5+19,其中 5 和 19 都是素数,请编写程序验证。

#include <stdio.h> 
int isPrime(long p) {
   long i;
    for (i = 2; i < p; i++)
       if (p % i == 0)
           return 0;
    return 1;
}
void main() {
    long n, p1, p2;
    scanf("%ld", &n);
    for (p1 = 3; p1 <= n / 2; p1 += 2) {
         p2 = n - p1;
         if (isPrime(p1) && isPrime(p2)) {
             printf("%ld=%ld+%ld\n", n, p1, p2);
             break;
         }
     }
}

12.分解质因数:每个合数(非质数)都可以写成几个质数相乘的形式,这几个 质数就叫做这个合数的质因数。比如,24 可以被分解为 2 2 2 3。请输入一个合 数,输出它的质因数。

#include <stdio.h> 
void main() {
    int n, i;
    scanf("%d", &n);
    while (n != 1) {
      for (i = 2; i <= n; i++) {
          if (n % i == 0) {
             printf("%d ", i);
             n = n / i;
             break;
          }
       }
     }
}

13.一元二次方程:求一元二次方程𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎的解(a、b、c 为任意实数)。

#include <stdio.h> 
#include <math.h> 
void main() {
    double a, b, c, disc, x1, x2, realpart, imagpart;
    scanf("%lf%lf%lf", &a, &b, &c);
    printf("The equation ");
    if (fabs(a) <= 1e-6)  
         printf("is not a quadratic");
    else {
         disc = b * b - 4 * a * c;
        if (fabs(disc) <= 1e-6)
           printf("has two equal roots:%8.4f", -b / (2 * a));
        else if (disc > 1e-6) {
           x1 = (-b + sqrt(disc)) / (2 * a);
           x2 = (-b - sqrt(disc)) / (2 * a);
           printf("has two differ roots:%8.4f and %8.4f", x1, x2);
    } else {
         realpart = -b / (2 * a);
         imagpart = sqrt(-disc) / (2 * a);
         printf("has complex roots:\n");
         printf("%8.4f+%8.4fi\n", realpart, imagpart);
         printf("%8.4f-%8.4fi\n", realpart, imagpart);
      }
    }
}

14.三角形的周长和面积:给定平面上任意三个点的坐标(x1,y1)、(x2,y2)、 (x3,y3),编写程序检验它们能否构成三角形。若这三个点不能构成三角形,则 输出“Impossible”;若可以,则输出该三角形的周长和面积。

#include <stdio.h> 
#include <math.h> 
void main() {
    float x1, x2, x3, y1, y2, y3;
    float a, b, c, s, l, area;
    scanf("%f%f%f%f%f%f", &x1, &y1, &x2, &y2, &x3, &y3);
    a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    b = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
    c = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
    if (a + b > c && a + c > b && b + c > a) {
       s = (a + b + c) / 2;
       area = sqrt(s * (s - a) * (s - b) * (s - c));
       l = a + b + c;
       printf("L = %.2f, A = %.2f\n", l, area);
    } else {
       printf("Impossible\n");
    }
}

15.圆的周长和面积:输入圆的半径,输出圆的周长和面积。Π=3.14

#include <stdio.h> 
void main() {
    float r, l, area;
    scanf("%f", &r);
    l = 2 * 3.14 * r;
    area = 3.14 * r * r;
    printf("L = %.2f, A = %.2f\n", l, area);
}

第六类、排序算法

1.冒泡排序法。

#include <stdio.h> 
void bubble_sort(int arr[], int len) {
    int i, j, t;
    for (i = 0; i < len - 1; i++) {
        for (j = 0; j < len - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                t = arr[j];
                arr[j] = arr[j + 1];
                 arr[j + 1] = t;
             }
         }
     }
}
void main() {
    int arr[] = {11, 99, 22, 88, 33, 77, 44, 66, 55, 55}, i;
    bubble_sort(arr, 10);
    for (i = 0; i < 10; i++) {
        printf("%-4d", arr[i]);
    }
}

2.选择排序法。

第一种:高效率。
#include <stdio.h>  
void selction_sort(int arr[], int len) {
  int i, j, t, min;
  for (i = 0; i < len - 1; i++) {
      min = i;
      for (j = i + 1; j < len; j++) {
          if (arr[min] > arr[j])
              min = j;
          }
          if (min != i) {
             t = arr[min];
             arr[min] = arr[i];
             arr[i] = t;
          }
       }
   }
void main() {
   int arr[] = {11, 99, 22, 88, 33, 77, 44, 66, 55, 55}, i;
   selction_sort(arr, 10);
   for (i = 0; i < 10; i++) {
      printf("%-4d", arr[i]);
   }
}

第二种:低效率。
#include <stdio.h> 
void selction_sort(int arr[], int len) {
   int i, j, t;
   for (i = 0; i < len - 1; i++) {
       for (j = i + 1; j < len; j++) {
           if (arr[i] > arr[j]) {
              t = arr[i];
              arr[i] = arr[j];
              arr[j] = t;
           }
       }
    }
}
void main() {
    int arr[] = {11, 99, 22, 88, 33, 77, 44, 66, 55, 55}, i;
    selction_sort(arr, 10);
    for (i = 0; i < 10; i++) {
        printf("%-4d", arr[i]);
    }
}

3.插入排序法。

#include <stdio.h> 
void insertion_sort(int arr[], int len) {
    int i, j, t;
    for (i = 0; i < len - 1; i++) {
        for (j = i + 1; j > 0; j--) {
             if (arr[j - 1] > arr[j]) {
                 t = arr[j - 1];
                 arr[j - 1] = arr[j];
                 arr[j] = t;
             } else {
                 break;
             }
         }
     }
}
void main() {
    int arr[] = {11, 99, 22, 88, 33, 77, 44, 66, 55, 55}, i;
    insertion_sort(arr, 10);
    for (i = 0; i < 10; i++) {
        printf("%-4d", arr[i]);
    }
}

4.字符串排序。

#include <stdio.h> 
#include <string.h> 
#define N 4
void main() {
    int i, j;
    char s[N][100], t[100];
    for (i = 0; i < N; i++)
        gets(s[i]);
    for (i = 0; i < N - 1; i++)
        for (j = 0; j < N - 1 - i; j++)
            if (strcmp(s[j], s[j + 1]) > 0) {
                strcpy(t, s[j]);
                strcpy(s[j], s[j + 1]);
                strcpy(s[j + 1], t);
            }
     for (i = 0; i < N; i++) puts(s[i]);
}

第七类、循环问题

1.求总和:1+2+3+4+5+…+100。

#include <stdio.h> 
void main() {
    int i, sum = 0;
    for (i = 1; i <= 100; i++)  sum += i;
    printf("%d", sum);
}

2.求总和:-1+2-3+4-5+…+100。

#include <stdio.h> 
void main() {
   int i, sum = 0;
   for (i = 1; i <= 100; i++) {
       if (i % 2 == 1)
          sum -= i;
       else 
          sum += i;
   }
   printf("%d", sum);
}

3.求总和:1-2+3-4+5-…-100。

#include <stdio.h> 
void main() {
    int i, sum = 0;
    for (i = 1; i <= 100; i++) {
       if (i % 2 == 0)
           sum -= i;
       else 
           sum += i;
     }
     printf("%d", sum);
}

4.求总和:11+22+33+44+55+66+77+88+99。

#include <stdio.h> 
void main() {
    int i, sum = 0;
    for (i = 1; i <= 9; i++)
       sum += (i * 10 + i);
    printf("%d", sum);
}

5.求总和:s=a+aa+aaa+…+aaa(n 个 a)。输入 a 表示基数,输入 n 表示项数。

#include <stdio.h>
void main() {
    int a, n, i, t = 0, sum = 0;
    scanf("%d%d", &a, &n);
    for (i = 1; i <= n; i++) {
        t = t * 10 + a;
        sum += t;
    }
    printf("%d", sum);
}

6.求总和:1-100 以内所有奇数的和。

#include <stdio.h> 
void main() {
    int i, sum = 0;
    for (i = 1; i <= 100; i += 2)
       sum += i;
    printf("%d", sum);
}

7.求总和:1-100 以内所有偶数的和。

#include <stdio.h> 
void main() {
    int i, sum = 0;
    for (i = 2; i <= 100; i += 2)
       sum += i;
    printf("%d", sum);
}

8.递归求和:1!+2!+3!+4!+5!+6!+7!+8!+9!+10!。

#include <stdio.h> 
void main() {
    int i, n = 1, sum = 0;
    for (i = 1; i <= 10; i++) {
        n *= i;
        sum += n;
    }
    printf("%d", sum);
}

9.斐波那契数列求和:1+1+2+3+5+8+…+6765。

#include <stdio.h>
void main() {
    int i, a = 1, b = 1, sum = 0;
    for (i = 1;; i++) {
       sum += (a + b);
       if (b == 6765) break;
       a = a + b;
       b = a + b;
     }
    printf("%d", sum);
}

10.分数求和:1-1/3+1/5-1/7+…-1/99+1/101。

#include <stdio.h> 
void main() {
    int i, t = 1;
    float n, sum = 0;
    for (i = 1; i <= 101; i += 2) {
        n = (1.0 / i) * t;
        sum += n;
        t = -t;
     }
     printf("%.2f", sum);
}

11.求Π的值:根据以下公式求Π的近似值,要求累加到某项的绝对值小于 1e-6 时为止。

#include <stdio.h>
#include <math.h>
void main() {
    double fz = 1, fm = 3, s = 1, i = 1;
    while (fabs(fz / fm) >= 1e-6) {
          /*累加当前项*/
          s += (fz / fm);
          /*准备下一项*/
          fz *= (++i);
          fm *= 2 * i + 1;
    }
    printf("%.2lf", s * 2);
}

 12.求Π的值:根据以下公式求Π的近似值,要求累加到某项的绝对值小于 1e-6 时为止。

#include <stdio.h>
#include <math.h>
void main() {
    double fz = 1, fm = 1, s = 0, t = 1;
    while (fabs(fz / fm * t) >= 1e-6) {
      /*累加当前项*/
      s += (fz / fm * t);
      /*准备下一项*/
      fm += 2;
      /*改变符号值*/
      t = -t;
     }
     printf("%.2lf", s * 4);
}

 13.求 e 的值:根据以下公式求 e 的近似值,要求累加到某项的绝对值小于 1e6 时为止

#include <stdio.h>
#include <math.h>
void main() {
    double fz = 1, fm = 1, s = 1, i = 1;
    while (fabs(fz / fm) >= 1e-6) {
        /*累加当前项*/
        s += (fz / fm);
        /*准备下一项*/
        fm *= (++i);
    }
    printf("%.2lf", s);
}

14.求 e的x幂的值:根据以下公式求 e x的近似值,要求累加到某项的绝对值小于 1e6 时为止。

​​#include <stdio.h>
#include <math.h>
void main() {
    double fz, fm, s = 1, i = 1, x;
    scanf("%lf", &x);
    fz = x;
    fm = 1;
    while (fabs(fz / fm) >= 1e-6) {
      /*累加当前项*/
      s += (fz / fm);
      /*准备下一项*/
      fz = pow(x, ++i);
      fm *= i;
    }
    printf("%.2lf", s);
}

15.求 sinx 的值:根据以下公式求 sinx 的近似值,要求累加到某项的绝对值小 于 1e-6 时为止。

#include <stdio.h>
#include <math.h>
void main() {
   double x, t, s = 0;
   int i = 0;
   scanf("%lf", &x);
   t = x;
   while (fabs(t) >= 1e-6) {
     s += t;
     i++;
     t *= -1 * (x * x) / (2 * i) / (2 * i + 1);
   }
   printf("%.2lf", s);
}

 16.求 cosx 的值:根据以下公式求 cosx 的近似值,要求累加到某项的绝对值小 于 1e-6 时为止。

#include <stdio.h>
#include <math.h>
void main() {
    double x, t, s = 0;
    int i = 0;
    scanf("%lf", &x);
    t = 1;
    while (fabs(t) >= 1e-6) {
       s += t;
       i++;
       t *= -1 * (x * x )/ (2 * i) / (2 * i - 1);
    }
    printf("%.2lf", s);
}

17.求平方根:编写函数 double getsqrt(double a),计算𝒙 = √𝒂(只计算 a=1, 2,3,4,5 的值)。已知计算𝒙 = √𝒂的迭代公式如下所示,要求累加到某项的绝 对值小于 1e-6 时为止。

#include <stdio.h> 
#include <math.h> 
double getsqrt(double a) {
    double x0, x1;
    x0 = a / 2;
    x1 = (x0 + a / x0) / 2;
    while (fabs(x0 - x1) >= 1e-6) {
        x0 = x1;
        x1 = (x0 + a / x0) / 2;
    }
    return x0;
}
void main() {
    int i;
    for (i = 1; i <= 5; i++) {
       printf("%.2lf\n", getsqrt(i));
    }
}

 第八类、进制转换

1.十进制(0~2147483647)转任意进制(2 进制~16 进制)。‘

#include <stdio.h>
void main() {
    int num, radix, i = 0;
    char res[32] = {0}, table[] = "0123456789ABCDEF";
    printf("请输入一个十进制整数:");
    scanf("%d", &num);
    printf("请输入要转换为几进制:");
    scanf("%d", &radix);
    if (num == 0) {
      res[0] = '0';
      i = 1;
    }
    while (num > 0) {
      res[i++] = table[num % radix];
      num /= radix;
    }
    printf("转换结果:");
    while (--i >= 0) {
       printf("%c", res[i]);
    }
}

2.任意进制(2 进制~16 进制)转十进制(0~2147483647)。

#include <stdio.h>
#include <string.h>
void main() {
    int res = 0, radix, i, k = 1;
    char num[32], table[] = "0123456789ABCDEF";
    printf("请输入待转换的字符串:");
    scanf("%s", &num);
    printf("待转换字符串为几进制:");
    scanf("%d", &radix);
    for (i = strlen(num) - 1; i >= 0; i--) {
       res += (strchr(table, num[i]) - table) * k;
       k *= radix;
    }
    printf("转换结果:%d", res);
}

 3.输入一个二进制字符串,将其转换为对应的十进制。

#include <stdio.h> 
void main() {
    char s[10];
    int i, n;
    gets(s);
    for (n = i = 0; s[i] != '\0'; i++) {
         n = n * 2 + s[i] - '0';
    }
    printf("%d", n);
}

4.输入一个八进制字符串,将其转换为对应的十进制。

#include <stdio.h> 
void main() {
    char s[10];
    int i, n;
    gets(s);
    for (n = i = 0; s[i] != '\0'; i++) {
        n = n * 8 + s[i] - '0';
    }
    printf("%d", n);
}

5.输入一个十六进制字符串,将其转换为对应的十进制。

#include <stdio.h> 
void main() {
    char s[10];
    int i, n;
    gets(s);
    for (n = i = 0; s[i] != '\0'; i++) {
      if (s[i] >= '0' && s[i] <= '9')
          n = n * 16 + s[i] - '0';
      if (s[i] >= 'a' && s[i] <= 'z')
          n = n * 16 + s[i] - 'a' + 10;
      if (s[i] >= 'A' && s[i] <= 'Z')
         n = n * 16 + s[i] - 'A' + 10;
    }
      printf("%d", n);
}

 第九类、实际应用

1.自由落体:一球从 100 米高度自由落下,每次落地后反跳回原高度的一半;再 落下,求它在第 10 次落地时,共经过多少米?第 10 次反弹多高?

#include <stdio.h>
void main() {
    double s = 100, h = s / 2;
    int i;
    for (i = 2; i <= 10; i++) {
       s = s + 2 * h;
       h = h / 2;
     }
     printf("s=%.6lf\n", s);
     printf("h=%.6lf\n", h);
}

2.计算天数:输入年月日,求当天是那年的第几天?

#include <stdio.h>
void main() {
   int y, m, d, s, i;
   scanf("%d%d%d", &y, &m, &d);
   s = d;
   for (i = 1; i < m; i++)
      if (i==1||i==3||i==5||i==7||i==8||i==10||i==12)
          s += 31;
      else if (i==4||i==6||i==9||i==11)
          s += 30;
      else if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
          s += 29;
      else
          s += 28;
    printf("%d", s);
}

 3.奖金提成:企业发放的奖金根据利润提成: 00 万元 < 利润 <= 10 万元,高出 00 万元的部分,奖金可提成 10%; 10 万元 < 利润 <= 20 万元,高出 10 万元的部分,奖金可提成 7.5%; 20 万元 < 利润 <= 40 万元,高出 20 万元的部分,奖金可提成 5%; 40 万元 < 利润 <= 60 万元,高出 40 万元的部分,奖金可提成 3%; 60 万元 < 利润 <= 100 万元,高出 60 万元的部分,奖金可提成 1%; 100 万元< 利润 ,高出 100 万的部分,奖金可提成 1%; 从键盘输入当月利润 n,求应发放奖金总数?

#include <stdio.h>
void main() {
    double n,bonus,bonus10,bonus20,bonus40,bonus60,bonus100;
    printf("Please enter the total profit of the month:");
    scanf("%lf", &n);
    bonus10 = 100000 * 0.1;
    bonus20 = bonus10 + 100000 * 0.075;
    bonus40 = bonus20 + 200000 * 0.05;
    bonus60 = bonus40 + 200000 * 0.03;
    bonus100 = bonus60 + 400000 * 0.015;
    if (n <= 100000)
       bonus = n * 0.1;
    else if (n <= 200000)
       bonus = bonus10 + (n - 100000) * 0.075;
    else if (n <= 400000)
       bonus = bonus20 + (n - 200000) * 0.05;
    else if (n <= 600000)
       bonus = bonus40 + (n - 400000) * 0.03;
    else if (n <= 1000000)
       bonus = bonus60 + (n - 600000) * 0.015;
    else if (n > 1000000)
       bonus = bonus100 + (n - 1000000) * 0.01;
    printf("bonus=%lf", bonus);
}

 4.出租车计价:根据某城市普通出租车收费标准编写程序计算车费,标准如下: 起步里程为 3 公里,起步费 10 元;超起步里程后 10 公里内,每公里 2 元;超过 10 公里以上的部分加收 50%的回空补贴费,即每公里 3 元;营运过程中,因路阻 及乘客要求临时停车的,按每 5 分钟 2 元计收(不足 5 分钟则不收费)。输入在 一行中,给出输入行驶里程(单位为公里,精确到小数点后 1 位)与等待时间 (单位为分钟),其间以空格分隔。输出乘客应支付的车费(单位为元),结果四 舍五入,保留到元。

#include <stdio.h> 
void main() {
    double mile;
    int time;
    double price, price1, price2;
    scanf("%lf%d", &mile, &time);
    if (mile <= 3) {
       price1 = 10;
    } else if (mile <= 10) {
       price1 = 10 + (mile - 3) * 2.0;
    } else {
      price1 = 10 + (10 - 3) * 2.0 + (mile - 10) * 3.0;
    }
    price2 = time / 5 * 2;
    price = price1 + price2;
    printf("%.0f", price);
}

5.阶梯电价:为了提倡居民节约用电,某省电力公司执行“阶梯电价”,安装一 户一表的居民用户电价分为两个“阶梯”:月用电量 50 千瓦时(含 50 千瓦时) 以内的,电价为 0.53 元/千瓦时;超过 50 千瓦时的,超出部分的用电量,电价 上调 0.05 元/千瓦时。输入某用户的月用电量(单位:千瓦时),计算该用户应 支付的电费(元),结果保留两位小数。 

#include <stdio.h>
void main() {
   float ydl, df;
   scanf("%f", &ydl);
   if (ydl <= 50) 
     df = 0.53 * ydl;
   else
     df = 0.53 * 50 + (ydl - 50) * 0.58;
   printf("%.2f", df);
}

 6.学生成绩分布:读入 N 个学生的百分制成绩,统计五分制成绩的分布。百分制 成绩到五分制成绩的转换规则:大于等于 90 分为 A;小于 90 且大于等于 80 为 B;小于 80 且大于等于 70 为 C;小于 70 且大于等于 60 为 D;小于 60 为 E。输 入在第一行中给出一个正整数 N(≤1000),即学生人数;第二行中给出 N 个学 生的百分制成绩,其间以空格分隔。在一行中输出 A、B、C、D、E 对应的五分制 成绩的人数分布,数字间以空格分隔,行末不得有多余空格。

#include <stdio.h> 
void main() {
    int a, b, c, d, e, n, i, arr[1000] = {0};
    scanf("%d", &n);
    a = b = c = d = e = 0;
    for (i = 0; i < n; i++) {
       scanf("%d", &arr[i]);
       if (arr[i] >= 90)
          a++;
       else if (arr[i] >= 80)
          b++;
       else if (arr[i] >= 70)
          c++;
       else if (arr[i] >= 60)
          d++;
       else 
          e++;
      }
     printf("%d %d %d %d %d", a, b, c, d, e);
}

 7.问星期:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一 样,则继续判断第二个字母。

#include <stdio.h>
void main() {
    char w;
    printf("请输入第一个字母:");
    scanf("%c", &w);
    getchar();
    switch (w) {
      case 'M' :printf("周一");break;
      case 'W' :printf("周三");break;
      case 'F' :printf("周五");break;
      case 'S' : printf("请输入第二个字母:");
                 scanf("%c", &w);
                 switch (w) {
                    case 'u' :printf("周日");break;
                    case 'a' :printf("周六");break;
                 }
                 break;
      case 'T' :
                printf("请输入第二个字母:");
                scanf("%c", &w);
                switch (w) {
                    case 'u' :printf("周二");break;
                    case 'h' :printf("周四");break;
                }
                break;
    }
}

 8.字符统计:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符 的个数

#include <stdio.h> 
void main() {
    int letters = 0, spaces = 0, digits = 0, others = 0;
    char c;
    while ((c = getchar()) != '\n') {
       if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
           letters++;
       else if (c == ' ')
           spaces++;
       else if (c >= '0' && c <= '9')
           digits++;
       else 
          others++;
     }
     printf("l=%d,d=%d,s=%d,o=%d", letters, digits, spaces, others);
} 

 9.报数问题:有 n 个人围成一圈,顺序排号,从第一个开始报数(从 1 到 3 报数), 凡报到 3 的人退出圈子,问最后留下的是原来第几号的那位。

#include <stdio.h> 
void main() {
    int a[1000], n, t, c, i;
    scanf("%d", &n);
    t = n;
    for (i = 0; i < t; i++) {
        a[i] = i + 1;
    }
    for (i = c = 0; n > 1; i++) {
       /* 从头开始吗 */
       if (i == t) {
          i = 0;
       }
       /* 退圈的不管 */
       if (a[i] != 0) {
         c++;
       }
       /* 数到三退圈 */
       if (c == 3) {
          a[i] = 0;
          c = 0;
          n--;
        }
    }
    for (i = 0; i < t; i++) {
       if (a[i] != 0) {
         printf("%d", a[i]);
       }
     }
}

 10.数据加密:某个公司采用公用电话传递数据,数据是四位的整数,在传递过 程中是加密的,加密规则如下:每位数字都加上 5,然后用和除以 10 的余数代 替该数字,再将第一位和第四位交换,第二位和第三位交换。然后按照相反的顺 序依次输出。

#include <stdio.h>
#define N 4
void main() {
    int a, i, t, data[N];
    scanf("%d", &a);
    data[0] = a % 10;
    data[1] = a / 10 % 10;
    data[2] = a / 100 % 10;
    data[3] = a / 1000;
    for (i = 0; i < N; i++) {
        data[i] += 5;
        data[i] %= 10;
    }
    for (i = 0; i < N / 2; i++) {
        t = data[i];
        data[i] = data[N - 1 - i];
        data[N - 1 - i] = t;
    }
    for (i = 0; i < N; i++) {
        printf("%d", data[N - 1 - i]);
    }
}

11.一维数组:输入 n 个(1<n<=10)正整数并保存到数组中,求出最大值、最小 值、平均值以及最大值、最小值在数组中的下标分别是多少。

#include <stdio.h> 
void main() {
    int i, n, max = 0, min = 0, sum = 0, a[10];
    float avg;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
         scanf("%d", &a[i]);
         sum += a[i];
    }
    avg = (float) sum / n;
    for (i = 0; i < n; i++) {
      if (a[i] > a[max])
      max = i;
      if (a[i] < a[min])
          min = i;
     }
     printf("max=%d,index=%d\n", a[max], max);
     printf("min=%d,index=%d\n", a[min], min);
     printf("avg=%.2f\n", avg);
}

 12.字符加密:编写加密函数:int encode(int *c)、解密函数:int decode(int *c),主函数输入一些纯字符,调用加密算法对这些字符进行加密,加密方式: 每个字符的码值+13,然后再调用解密函数将加密后的字符依次输出。

#include <stdio.h>
/* 加密算法 */
int encode(int *c) {
   *c = *c + 13;
   return *c;
}
/* 解密算法 */
int decode(int *c) {
    *c = *c - 13;
    return *c;
}
void main() {
    int c[100], i;
    i = 0;
    while ((c[i] = getchar()) != '\n') {
       putchar(encode(&c[i]));
       i++;
    }
    printf("\n");
    i = 0;
   while (c[i] != '\n') {
     putchar(decode(&c[i]));
     i++;
   }
   printf("\n");
}

13.数字提取:输入一个以回车为结束标志的字符串(少于 10 个字符),提取其 中的所有数字字符,将其转换成一个十进制整数输出。

#include <stdio.h>
void main() {
  char str[10];
  int i = 0, n = 0;
  gets(str);
  while (str[i] != '\0') {
     if (str[i] >= '0' && str[i] <= '9') {
     n = n * 10 + str[i] - '0';
     }
     i++;
  }
  printf("num=%d\n", n);
}

 14.渔民打鱼:中国有句俗语叫“三天打鱼两天晒网”。假设某人从某天起,开始 “三天打鱼两天晒网”,问这个人在以后得第 N 天中是“打鱼”还是“晒网”?

#include <stdio.h>
void main() {
   int day;
   scanf("%d", &day);
   if (day % 5 == 1 || day % 5 == 2 || day % 5 == 3) {
      printf("Fishing in day %d\n", day);
   } else {
     printf("Drying in day %d\n", day);
 }
}

 15.小球重量:三个球 A、B、C,大小形状相同且其中一个球与其它球重量不同, 要求找出这个不一样的球。

#include <stdio.h> 
void main() {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (a == b)
      printf("C\n");
    else if (a == c)
      printf("B\n");
    else 
      printf("A\n");
}

 16.学生捐款:在全校 1000 名学生中,征集慈善募捐,当募捐总数达到 10 万元 时就停止募捐,统计此时的捐款人数以及平均每人捐款的数目。

#include <stdio.h>
void main() {
    float amount, avg, sum = 0;
    int i;
    for (i = 1; i <= 1000; i++) {
      printf("请输入捐款金额:");
      scanf("%f", &amount);
      sum = sum + amount;
      if (sum >= 100000) {
         break;
      }
    }
    if (i <= 1000) {
      printf("募捐已经完成,信息如下:\n");
      avg = sum / i;
    } else {
      printf("募捐没有完成,信息如下:\n");
      avg = sum / (--i);
    }
    printf("捐款人数=%d,平均金额=%.2f\n", i, avg);
}

17.递归转换:用递归的方法将一个整数转换为字符串。例如:输入 123456,输 出“123456”。

#include <stdio.h>
void tranvers(int n) {
   if (n / 10 != 0)
      tranvers(n / 10);
   printf("%c", n % 10 + '0');
}
void main() {
    int n;
    printf("Please enter a number:");
    scanf("%d", &n);
    printf("The string is ");
    if (n < 0) {
      printf("-");
      n = -n;
    }
    tranvers(n);
}

18.火车时间:假设出发和到达在同一天内,根据火车的出发时间和到达时间, 编写程序计算整个旅途所用的时间。输入格式:在一行中输入两个 4 位正整数, 分别表示火车的出发时间和到达时间,每个时间的格式为 2 位小时数(00-23) 和 2 位分钟数(00-59)。输出格式:“hh:mm”,其中 hh 为 2 位小时数、mm 为 2 位分钟数。 

#include <stdio.h> 
void main() {
    int ks, js, xs, fz;
    scanf("%d%d", &ks, &js);
    xs = js / 100 - ks / 100;
    fz = js % 100 - ks % 100;
    if (fz < 0) {
       xs = xs - 1;
       fz = 60 + fz;
    }
    printf("%02d:%02d", xs, fz);
}

 19.简单计算器:编写一个简单计算器的程序,可根据输入的运算符,对 2 个整 数进行加、减、乘、除或求余运算,且保证除法和求余的分母非零。当运算符为 +、-、*、/、%时,输出相应的运算结果。若输入是非法符号则输出 ERROR。

#include <stdio.h> 
void main() {
    int a, b;
    char c;
    scanf("%d %c%d", &a, &c, &b);
    switch (c) {
      case '+': printf("%d\n", a + b); break;
      case '-': printf("%d\n", a - b); break;
      case '*': printf("%d\n", a * b); break;
      case '/':
         if (b != 0) {
            printf("%d\n", a / b);
            break;
         }  
      case '%':
         if (b != 0) {
            printf("%d\n", a % b);
            break;
         }
      default : printf("ERROR\n"); break;
    }
}

 20.机动车处理:按照规定,在高速公路上行驶的机动车,达到或超出本车道限 速的 10%,则初 200 元罚款;达到或超出本车道限速的 50%,就要吊销驾驶证。 编写程序根据车速和限速自定判别对该机动车的处理。输入两个整数,分别对应 车速和限速。输出格式:若属于正常驾驶,则输出“OK”;若属于罚款,则输出 “Exceed x%. Ticket 200”;若应该吊销驾驶证,则输出“Exceed x%. License Revoked”。其中,x 是超速的百分比,精确到整数。

#include <stdio.h> 
void main() {
    int cs, xs, n, m;
    scanf("%d%d", &cs, &xs);
    n = xs * (1 + 0.1);
    m = xs * (1 + 0.5);
    if (cs < n) {
       printf("OK\n");
    } else if (cs < m) {
       printf("Exceed %.0f%%. Ticket 200\n",
       1.0 * (cs - xs) / xs * 100);
    } else {
       printf("Exceed %.0f%%. License Revoked\n",
       1.0 * (cs - xs) / xs * 100);
    }
}

 21.阶梯水费:为鼓励居民节约用水,自来水公司按用水量阶梯式计价的办法, 居民应交水费 y(元)与月用水量 x(吨)相关:当 x 不超过 15 吨时,y=4x/3; 超过后,y=2.5x-17。请编写程序实现水费的计算。

#include <stdio.h> 
void main() {
    float x, y;
    scanf("%f", &x);
    if (x >= 0 && x <= 15)
       y = 4 * x / 3;
    else 
       y = 2.5 * x - 17;
    printf("%.2f\n", y);
}

 22.蠕虫爬井:一条蠕虫长 1 寸,在一口深为 N 寸的井的底部。已知蠕虫每 1 分 钟可以向上爬 U 寸,但必须休息 1 分钟才能接着往上爬。在休息的过程中,蠕虫 又下滑了 D 寸。就这样,上爬和下滑重复进行。问蠕虫需要多长时间才能爬出 井?要求不足 1 分钟按 1 分钟计算,假定只要在某次上爬过程中蠕虫的头部到 达了井的顶部,那么蠕虫就完成任务了。初始时,蠕虫是趴在井底的(即高度为 0)。输入格式:输入在一行中顺序给出 3 个正整数 N、U、D,其中 D

#include <stdio.h> 
void main() {
    int n, u, d, time = 0, curh = 0;
    /* 输入井的深度,蠕虫上爬和下滑的距离 */
    scanf("%d%d%d", &n, &u, &d);
    while (1) {
      time++;
      curh += u; /* 每爬一次,上升 u 距离 */
      if (curh >= n) break;
      time++;
      curh -= d; /* 休息一次,下滑 d 距离 */
    }
    printf("%d\n", time);
}

 23.汽油自助服务:现在 90 号汽油 6.95 元/升、93 号汽油 7.44 元/升、97 号汽 油 7.93 元/升。为吸引顾客,某自动加油站推出了“自助服务”和“协助服务” 两个服务等级,分别可得到 5%和 3%的折扣。编写程序,根据输入顾客的加油量 a,汽油品种 b(90、93 或 97)和服务类型 c(m – 自助,e – 协助),计算并 输出相应付款。

#include <stdio.h>
void main() {
    float je, jg;
    int a, b;
    char c;
    scanf("%d %d %c", &a, &b, &c);
    switch (b) {
       case 90:jg = 6.95;break;
       case 93: jg = 7.44;break;
       case 97:jg = 7.93;break;
    }
    if (c == 'm') {
       je = jg * a * (1 - 0.05);
    } else {
       je = jg * a * (1 - 0.03);
    }
    printf("%.2f", je);
}

 24.四舍五入:编程实现将浮点数“123.456789”分别四舍五入保留 1 位小数、 2 位小数和 3 位小数。

#include <stdio.h> 
void main() {
    float a = 123.456789;
    float f1 = (int) ((a * 10) + 0.5) / 10.0; //保留 1 位小数
    float f2 = (int) ((a * 100) + 0.5) / 100.0; //保留 2 位小数
    float f3 = (int) ((a * 1000) + 0.5) / 1000.0;//保留 3 位小数
    printf("f1 = %0.1f\n", f1);
    printf("f2 = %0.2f\n", f2);
    printf("f3 = %0.3f\n", f3);
}

25.节目调查:调查电视节目受欢迎程度。某电视台要调查观众对该台 8 个栏目 (设相应栏目编号为 1-8)的受欢迎程度,共调查了 1000 位观众。现要求编写 程序,输入每一位观众的投票情况(每位观众只能选择一个最喜欢的栏目投票), 统计输出各栏目的得票情况。

#include <stdio.h> 
void main() {
   int i, n, a[9] = {0};
   for (i = 1; i <= 1000; i++) {
     scanf("%d", &n);
     a[n]++
   } 
   for (i = 1; i <= 8; i++) {
     printf("栏目%d 得票=%d\n", i, a[i]);
   }
}

 26.由用户输入一个正整数 n,分析出该正整数的每一位,然后将各位数字从大 到小重新排序后得到正整数 m,输出 m 和 n 的平均值。 (1)编写输出正整数 n 和 m 的程序。 (2)给出调试过的输出 m 和 n 的平均值的源程序。

#include <stdio.h>
#include <stdlib.h>
/**
* 选择排序法
* @param a 排序数组
* @param len 元素个数
*/
void selction_sort(int a[], int len) {
    int i, j, t;
    for (i = 0; i < len - 1; i++) {
       for (j = i + 1; j < len; j++) {
          if (a[i] < a[j]) {
             t = a[i];
             a[i] = a[j];
             a[j] = t;
          }
       }
     }
}
void main() {
    int m, n, t, i, c, *p;
    /*输入数字 n*/
    scanf("%d", &n);
    /*分析位数 c*/
    t = n;
    c = 0;
    while (t > 0) {
       c++;
       t /= 10;
    }
    /*分解数字 n*/
    p = (int *) malloc(sizeof(int) * c);
    for (t = n, i = 0; i < c; i++) {
      p[i] = t % 10;
      t /= 10;
    }
    /*排序数组 p*/
    selction_sort(p, c);
    /*数组还原 m*/
    for (m = 0, i = 0; i < c; i++) {
       m = m * 10 + p[i];
    }
    /*输出结果*/
    printf("n = %d\n", n);
    printf("m = %d\n", m);
    printf("(m + n) / 2 = %.2lf", ((m + n) / 2.0));
}

27.现有扑克牌 52 张,其花色记录为:char suit[4][5] = {"红心", "方块", "梅花", "黑桃"};其牌面记录为:char face[] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K'};请用 C 语言编写程序实现以下功 能。 (1)自定义结构体数组,按照同一花色牌面从小到大顺序记录全部 52 张扑克牌。 (2)以时间为参数设置随机序列种子,实现洗牌:即遍历扑克牌数组,依次交换 当前牌与数组中随机位置的牌。 (3)输出打乱顺序后的全部扑克牌,牌与牌间使用 Tab 分隔。

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
typedef struct card {
   int type;/*代表花色下标*/
   int point;/*代表点数下标*/
} Card;
void main() {
   int i, j, k, index;
   Card c[52], t;
   char suit[4][5] = {"红心", "方块", "梅花", "黑桃"};
   char face[] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K'};
   /*初始化 52 张牌,由 k 控制这是第几张牌*/
   k = 0;
   for (i = 0; i < 4; i++) {
       for (j = 0; j < 13; j++) {
           c[k].type = i;
           c[k].point = j;
           k++;
        }
    }
    /*循环这 52 张牌,使用随机数进行洗牌*/
    srand(time(NULL));
    for (i = 0; i < 52; i++) {
        /*产生 0~51 的随机数*/
        index = rand() % 52;
        /*交换两张牌的位置*/
        t = c[i];
        c[i] = c[index];
        c[index] = t;
     }
    /*输出这 52 张牌,由 k 控制这是第几张牌*/
    k = 0;
    for (i = 0; i < 4; i++) {
       for (j = 0; j < 13; j++) {
          printf("%s%c\t", suit[c[k].type], face[c[k].point]);
          k++;
          if (k % 13 == 0) {
             printf("\n");
          }
       }
    }
}

第十类、图形输出

1.左上三角

**********
*********
********
*******
******
*****
****
***
**
*
#include <stdio.h> 
void main() {
   int i, j;
   for (i = 10; i >= 1; i--) {
     for (j = 1; j <= i; j++)
        printf("*");
     if (i != 1) printf("\n");
   }
}

2.左下三角

*
**
***
****
*****
******
*******
********
*********
**********
#include <stdio.h> 
void main() {
    int i, j;
    for (i = 1; i <= 10; i++) {
      for (j = 1; j <= i; j++)
         printf("*");
      if (i != 10) printf("\n");
    }
}

3.右上三角

**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *
#include <stdio.h> 
void main() {
   int i, j;
   for (i = 10; i >= 1; i--) {
     for (j = 1; j <= 10 - i; j++)
        printf(" ");
     for (j = 1; j <= i; j++)
        printf("*");
     if (i != 1) printf("\n");
   }
}

4.右下三角

         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********
#include <stdio.h> 
 
void main() {
  int i, j;
  for (i = 1; i <= 10; i++) {
    for (j = 1; j <= 10 - i; j++)  printf(" ");
    for (j = 1; j <= i; j++) printf("*");
    if (i != 10) printf("\n");
  }
}

5.输出杨辉三角形

#include <stdio.h> 
void main() {
    int i, j, n, a[100][100] = {0};
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
       a[i][0] = 1;
    }
    for (i = 1; i < n; i++) {
      for (j = 1; j <= i; j++) {
         a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
      }
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j <= i; j++) {
        printf("%-4d", a[i][j]);
      }
      printf("\n");
    }
}

6.输出数字金字塔

#include <stdio.h> 
void main() {
   int i, j;
   for (i = 1; i <= 9; i++) {
      for (j = 9; j > i; j--) printf(" ");
      for (j = 1; j <= i; j++) printf("%c", 48 + j);
      for (j = i - 1; j >= 1; j--) printf("%c", 48 + j);
      if (i != 9) printf("\n");
   }
}

7.打印乘法口诀表。

#include <stdio.h> 
void main() {
  int i, j;
  for (i = 1; i <= 9; i++) {
     for (j = 1; j <= i; j++)
     printf("%d * %d = %-5d", i, j, i * j);
     if (i != 9) printf("\n");
  }
}

8.输出空心菱形 

#include <stdio.h>
void main() {
   /*n:代表菱形边长*/
   int i, j, n = 5;
   /*打印上三角*/
   for (i = 1; i <= n; i++) {
      for (j = 1; j <= (2 * n - 1); j++) {
         if (j == n + 1 - i || j == n - 1 + i)
            printf("*");
         else
            printf(" ");
      }
      printf("\n");
    }
   /*打印下三角*/
   for (i = 1; i < n; i++) {
      for (j = 1; j <= (2 * n - 1); j++) {
         if (j == i + 1 || j == (2 * n - 1) - i)
             printf("*");
         else
             printf(" ");
         }
       printf("\n");
    }
}

9.输出实心菱形 

#include <stdio.h>
void main() {
   /*n:代表菱形边长*/
   int i, j, n = 5;
   /*打印上三角*/
   for (i = 1; i <= n; i++) {
       for (j = 1; j <= (2 * n - 1); j++) {
          if (j >= n + 1 - i && j <= n - 1 + i)
             printf("*");
          else
             printf(" ");
        }
        printf("\n");
    }
   /*打印下三角*/
   for (i = 1; i < n; i++) {
      for (j = 1; j <= (2 * n - 1); j++) {
         if (j >= i + 1 && j <= (2 * n - 1) - i)
            printf("*");
         else
            printf(" ");
         }
       printf("\n");
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/558181.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

海外媒体如何发布软文通稿

大舍传媒-带您了解海外发布新潮流 随着全球化的不断深入&#xff0c;越来越多的中国企业开始关注海外市场。为了在国际舞台上树立品牌形象&#xff0c;企业纷纷寻求与海外媒体合作&#xff0c;通过发布软文通稿的方式&#xff0c;传递正面信息&#xff0c;提升品牌知名度。作为…

【4071】基于小程序实现的活动报名管理系统

作者主页&#xff1a;Java码库 主营内容&#xff1a;SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app等设计与开发。 收藏点赞不迷路 关注作者有好处 文末获取源码 技术选型 【后端】&#xff1a;Java 【框架】&#xff1a;ssm 【…

抹机王的使用教程以及常见问题

首先请确保你已经正常安装了XPosed/EDXP/LSP框架并已激活抹机王模块&#xff0c;其中XP和EDXP模块均只需要框架内激活抹机王并重启即可&#xff0c;LSPosed注意作用域需要勾选上自己想要修改的APP&#xff08;如果你还是一意孤行只勾选系统框架那改机完全没用就是你自己的想法了…

设计模式之模板方法模式详解(下)

3&#xff09;钩子方法的使用 1.概述 钩子方法的引入使得子类可以控制父类的行为。 2.结构图 3.代码实现 将公共方法和框架代码放在抽象父类中 abstract class DataViewer {//抽象方法&#xff1a;获取数据public abstract void GetData();//具体方法&#xff1a;转换数据…

每日一题 — 最小覆盖子串

76. 最小覆盖子串 - 力扣&#xff08;LeetCode&#xff09; 解法一&#xff1a;暴力遍历哈希表 解法二&#xff1a;滑动窗口哈希表 定义left和right初始化为零&#xff0c;固定left&#xff0c;先向右遍历right&#xff0c;放到哈希表中这个时候我们需要统计有效字符的个数&…

深入挖掘C语言 ---- 文件操作

目录 1. 文件的打开和关闭1.1 流和标准流1.1.1流1.1.2标准流 1.2 文件指针1.3 文件的打开和关闭 2. 顺序读写3. 随机读写3.1 fseek3.2 ftell3.3 rewind 4. 读取结束判定 正文开始 1. 文件的打开和关闭 1.1 流和标准流 1.1.1流 我们程序的数据需要输出到各种外部设备, 也需要…

Leetcode算法训练日记 | day30

一、重新安排行程 1.题目 Leetcode&#xff1a;第 332 题 给你一份航线列表 tickets &#xff0c;其中 tickets[i] [fromi, toi] 表示飞机出发和降落的机场地点。请你对该行程进行重新规划排序。 所有这些机票都属于一个从 JFK&#xff08;肯尼迪国际机场&#xff09;出发…

java算法day2

螺旋矩阵搜索插入位置查找元素第一个位置和最后一个位置 螺旋矩阵 解法&#xff1a;模拟&#xff0c;核心在于你怎么转&#xff0c;还有就是处理边界&#xff0c;边界如何收缩&#xff0c;什么时候停止旋转。最内圈的时候怎么处理。 通过上图的模拟来解决这个问题&#xff1a;…

数据库锁等待排查方法、命令行安装数据库及授权文件更新

欢迎关注“数据库运维之道”公众号&#xff0c;一起学习数据库技术! 本期将为大家分享“数据库锁等待排查方法、命令行安装数据库及授权文件更新”的运维技能。 关键词&#xff1a;锁等待、V$LOCK、V$TRXWAIT、死锁、锁超时、命令行部署达梦、授权文件更新 当用户反馈执行SQL语…

1985-2022年各地级市专利申请数据

1985-2022年各地级市专利申请数据 1、时间&#xff1a;1985-2022年 2、指标&#xff1a;行政区划代码、地区、省份、城市、年份、发明公布&#xff08;申请数&#xff09;、其中&#xff1a;获得授权、外观设计申请量、实用新型申请量 3、来源&#xff1a;国家知识产权局 4…

【Java】简单实现图书管理系统

前言 在本篇博客当中&#xff0c;我们会使用Java基础语法来简单实现一个图书管理系统&#xff0c;主要用到的知识为&#xff1a;封装、多态、继承、接口等等&#xff0c;并不会使用数据库来存储数据&#xff0c;请注意 需求 1. 要求设置管理员和普通用户两种身份&#xff0c…

【深度学习实战(9)】三种保存和加载模型的方式

一、state_dict方式&#xff08;推荐&#xff09; torch.save(model.state_dict(), PATH)model YourModel() model.load_state_dict(torch.load(PATH)) model.eval()记住一定要使用model.eval()来固定dropout和归一化层&#xff0c;否则每次推理会生成不同的结果。 二、整个…

实验室三大常用仪器3---交流毫伏表的使用方法(笔记)

目录 函数信号发生器、示波器、交流毫伏表如果连接 交流毫伏表的使用方法 测量值的读数问题 实验室三大常用仪器1---示波器的基本使用方法&#xff08;笔记&#xff09;-CSDN博客 实验室三大常用仪器2---函数信号发生器的基本使用方法&#xff08;笔记&#xff09;-CSDN博客…

C#自定义窗体更换皮肤的方法:创建特殊窗体

目录 1.窗体更换皮肤 2.实例 &#xff08;1&#xff09;图片资源管理器Resources.Designer.cs设计 &#xff08;2&#xff09;Form1.Designer.cs设计 &#xff08;3&#xff09;Form1.cs设计 &#xff08;4&#xff09; 生成效果 &#xff08;5&#xff09;一个遗憾 1.窗…

Git常见命令行操作和IDEA图形化界面操作

设置Git用户名和标签 在安装完Git以后需要设置用户和签名&#xff0c;至于为什么要设置用户签名可以看一下这篇文章【学了就忘】Git基础 — 11.配置Git用户签名说明 - 简书 (jianshu.com) 基本语法&#xff1a; git config --global user.name 用户名 git config --global u…

SpringBoot项目创建及简单使用

目录 一.SpringBoot项目 1.1SpringBoot的介绍 1.2SpringBoot优点 二.SpringBoot项目的创建 三.注意点 一.SpringBoot项目 1.1SpringBoot的介绍 Spring是为了简化Java程序而开发的&#xff0c;那么SpringBoot则是为了简化Spring程序的。 Spring 框架&#xff1a; Spring…

ARM之栈与方法

ARM之栈与方法 计算机中的栈是一种线性表&#xff0c;它被限定只能在一端进行插入和删除操作&#xff08;先进后出&#xff09;。通常将可以插入和删除操作的一端称为栈顶&#xff0c;相对的一端为栈底。 通常栈有递增堆栈&#xff08;向高地址方向生长&#xff09;、递减堆栈…

鸿蒙OpenHarmony【搭建Ubuntu环境】

搭建Ubuntu环境 在嵌入式开发中&#xff0c;很多开发者习惯于使用Windows进行代码的编辑&#xff0c;比如使用Windows的Visual Studio Code进行OpenHarmony代码的开发。但当前阶段&#xff0c;大部分的开发板源码还不支持在Windows环境下进行编译&#xff0c;如Hi3861、Hi3516…

Day37 IO流的操作

Day37 IO流的操作 文章目录 Day37 IO流的操作Java的文件拷贝利用 文件字节输出流 向文件写入数据利用 文件字节输入流 读取文件里的数据利用 带缓冲区的字节输出流 向文件写入数据利用 带有缓冲区的字节输入流 读取文件里的数据利用 字符输出转换流 向文件写入数据利用 字符输入…

Java全套智慧校园系统源码springboot+elmentui +Quartz可视化校园管理平台系统源码 建设智慧校园的5大关键技术

Java全套智慧校园系统源码springbootelmentui Quartz可视化校园管理平台系统源码 建设智慧校园的5大关键技术 智慧校园指的是以物联网为基础的智慧化的校园工作、学习和生活一体化环境&#xff0c;这个一体化环境以各种应用服务系统为载体&#xff0c;将教学、科研、管理和校园…
最新文章