C程序设计第六章习题

 1.素数问题

代码:

#include "stdio.h"
 
void main() {

    int a[100];
    int k = 0;
    int i = 0;
 
    while(i < 100) {
        int j = 2;
        while (j < i) {
            if(i%j == 0) {
                break;
            }
            j++;
}
        if(j == i) {
            a[k++] = i;//等于于a[k] = i; k++;
            //printf("%d,", i);
        }
        i++;
    }

    for(int i = 0; i < k; i++) {
        printf("%d,", a[i]);
    }

}

2.排序问题 

 代码:

#include "stdio.h"

void main() {
    int a[10] = {21,1,32,43,6,321,5,23,16,10};
    int temp = 0;
    printf("The num sort:");
    for(int i = 0; i < 10; i++) {
        for(int j = i; j < 10; j++) {
            if(a[i] > a[j]) {
                temp = a[j];
                a[j] = a[i];
                a[i] = temp;
            }
        }
        printf("%d,", a[i]);
    }
}

输出结果:

3. 求矩阵对角线之和 

代码:

#include "stdio.h"
void main() {

    int a[3][3] = { {1,2,3},
                    {4,5,6},
                    {7,8,9}};
    
    int sum = 0;
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++){
            if(i == j) {
                sum = sum + a[i][j];
                printf("a[%d][%d] = %d\n", i, j, a[i][j]);
            }
        }
    }
    printf("The sum is:%d", sum);
}

 输出结果:

4.有序数组插入数据 

代码:

#include "stdio.h"

void main() {
    int a[20] = {1,3,5,7,9,14,20,34,50,100};
    int temp = 0;
    int num = 39;

    printf("The numbers befor insert a number:");
    for(int j = 0; j < 10; j++) {
        printf(",%d", a[j]);
    }

    int i = 0;
    while(a[i]) {
        if(num <= a[i]) {
            break;
        }
        i++;
    }

    for(int j = 10; j > i; j--) {
        a[j] = a[j-1];
    }
    a[i] = num;

    printf("\nThe numbers after insert a number:");
    for(int j = 0; j < 11; j++) {
        printf(",%d", a[j]);
    }

}

输出结果:

6.输出杨辉三角形 

代码:

#include "stdio.h"

void main() {

    int n = 10;
    int a[10][10];

    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < 10; j++) {
            if(j == 0 && i != j) {
                a[i][j] = 1;
                printf("%d ", a[i][j]);
            } else if(i == j) {
                a[i][j] = 1;
                printf("%d\n", a[i][j]);
            } else if(i > j && j > 0) {
                a[i][j] = a[i-1][j] + a[i-1][j-1];
                printf("%d ", a[i][j]);
            }
        }
    }
}

输出结果:

8. 鞍点问题 

代码:

#include "stdio.h"

void main() {

    int n = 10;
    int a[3][3] = { {1,2,3},
                    {4,5,6},
                    {7,8,9}};
    int max = 0;
    int isMin = 1;
    int col = 0;
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
            if(max < a[i][j]) {
                max = a[i][j];
                col = j;
            }
        }
        for(int k = 0; k < 3; k++) {
                if(k!= i && max > a[k][col]) {
                    isMin = 0;
                    break;}
            }
            if(isMin) printf("a[%d][%d] = %d\n", i, col, a[i][col]);
    }
}

输出结果:

9. 二分查找

代码:

#include "stdio.h"

void main() {
    int a[15] = {1,3,5,7,9,14,20,34,50,100,123,134,234,5643,34893};
    int n = 15;
    int num = 0;
    int center = n/2;
    int flag = 0;

    printf("Please input a number to find:");
    scanf("%d", &num);
    if(num > a[14] || num < a[0]) {
        printf("No number!");
        return;
    }
    for(int j = 0; j <= n; j++) {

        if (num == a[center]) {
            printf("the num has been found:a[%d] = %d", j, a[center]);
            flag = 1;
            break;
        } else if(num < a[center]) {
            n = center;
            center = (j + center)/2;
        } else {
            j = center;
            center = (center + n)/2;
        }
        //printf(",%d", center);
    }

    if(!flag) printf("No number!"); 

    

}

 输出结果:

10. 解析字符数组中的字符类型

代码:

#include "stdio.h"
void main() {

    char str[3][80] = { "feo390000000afdfff    fffffadhfhqweh9hidhafhdaf8haifhadhf9h9hfhhfahf9w8hhdj[fs]",
                        "feo390000000afdffffffffffffadhfhqweh9hidhafhdaf8haifhadhf9h9hfhhfahf9w8hhdj[fs]",
                        "feo390000000afdfffffAAAAAffadhfhqweh9hidhafhdaf8haifhadhf9h9hfhhfahf9w8hhdj[fs]"};
    
    int a = 0;//count number
    int k = 0;//count big charactor
    int l = 0;//count small charactor
    int m = 0;//count space
    int n = 0;//other chars
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 80; j++){
            if(str[i][j] >= '0' && str[i][j] <= '9') {
            a++;
        } else if(str[i][j] >= 'a' && str[i][j] <= 'z') {
            l++;
        } else if(str[i][j] >= 'A' && str[i][j] <= 'Z'){
            k++;
        }else if(str[i][j] == ' ') {
            m++;
        } else {
            n++;
        }
        }
    }
    
    printf("The count of number is :%d\n", a);
    printf("The count of big char is :%d\n", k);
    printf("The length of small char is :%d\n", l);
    printf("The count of space is :%d\n", m);
    printf("The count of other chars is :%d", n);
}

输出结果:

 11. 输出平行四边形图案

代码:

#include "stdio.h"

void main() {
    int m = 0;
    int n = 9;

    for(int i = 0; i < 5; i++) {
        for(int j = 0; j < 9; j++) {
            if(j >= m && j <= m + 4) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        m++;
        printf("\n");
    }
}

输出结果:

12. 字符编码解码 

代码:

#include "stdio.h"
 
void main() {
    char a[] = "Welcome to China!";
    int i = 0;
    while (a[i])
    {
        if(a[i] >= 'A' && a[i] <= 'Z') {
            //int A = 'A';//97 'Z'=123
            int j = a[i] - 'A';
            a[i] = 'A' + 25 - j;
        } else if(a[i] >= 'a' && a[i] <= 'z') {
            int x = a[i] - 'a';
            a[i] = 'a' + 25 - x;
        }
        i++;
    }
    printf("the encoded string is %s\n", a);

    i = 0;
    while (a[i])
    {
        if(a[i] >= 'A' && a[i] <= 'Z') {
            //int A = 'A';//97 'Z'=123
            int j = a[i] - 'A';
            a[i] = 'A' + 25 - j;
        } else if(a[i] >= 'a' && a[i] <= 'z') {
            int x = a[i] - 'a';
            a[i] = 'a' + 25 - x;
        }
        i++;
    }
    printf("the decoded string is %s\n", a);
    
}

 输出结果:

13. 字符串连接实现 

 代码:

#include "stdio.h"
 
void main() {
    char a[] = "Welcome to China!";
    char b[] = "My dear friend.";
    char c[200];
    int i = 0;
    while (a[i])
    {
        c[i] = a[i];
        i++;
    }

    int length = i;
    i = 0;
    while (b[i])
    {
        c[i + length] = b[i];
        i++;
    }
    c[i + length] = '\0';//字符串结尾加0,以免多余的字符显示
    
    printf("the connected string is %s\n", c);
    
}

输出结果:

 14. 字符串比较实现

#include "stdio.h"
 

int strCompare(char *a, char *b);

int strCompare(char *a, char *b){
    
    int i = 0;
    while (a[i] && b[i])
    {
        if(a[i] != b[i]) {
            return a[i] - b[i];
        }
        i++;
    }
    if(a[i] == b[i]) {
        return 0;
    } else if(a[i] == 0) {
        return -26;
    } else if(b[i] == 0) {
        return 26;
    }
}
void main() {
    char a[100];
    char b[100];
    printf("Please input string a:");
    gets(a);
    printf("Please input string b:");
    gets(b);

    int result = strCompare(a, b);
    if(result == 0) {
        printf("the compared strings equal\n");
    } else if(result > 0) {
        printf("the compared strings a long\n");
    } else {
        printf("the compared strings b long");
    }
}

输出结果:

15. 字符串复制实现

 代码:

#include "stdio.h"
 
void main() {
    char a[] = "Welcome to China!";
    char c[200];
    int i = 0;
    while (a[i])
    {
        c[i] = a[i];
        i++;
    }
    c[i] = a[i];
    
    printf("the copy string is %s\n", c);
    
}

 输出结果:

相关推荐

  1. C++程序设计3版)谭浩强 8 习题

    2024-05-14 11:20:05       40 阅读
  2. C++程序设计3版)谭浩强 9 习题

    2024-05-14 11:20:05       39 阅读
  3. C++程序设计3版)谭浩强 10 习题

    2024-05-14 11:20:05       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-14 11:20:05       5 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-14 11:20:05       5 阅读
  3. 在Django里面运行非项目文件

    2024-05-14 11:20:05       4 阅读
  4. Python语言-面向对象

    2024-05-14 11:20:05       6 阅读

热门阅读

  1. chatGPT 凌晨发布会内容总结

    2024-05-14 11:20:05       15 阅读
  2. LINQ(四) ——使用LINQ进行对象类型初始化

    2024-05-14 11:20:05       20 阅读
  3. 分享四种CAD图纸加密方法,严防盗图

    2024-05-14 11:20:05       15 阅读
  4. 矩阵的特征分解

    2024-05-14 11:20:05       16 阅读
  5. 蓝桥杯-移动距离(最简单的写法)

    2024-05-14 11:20:05       17 阅读
  6. 你眼中的IT行业现状与未来趋势

    2024-05-14 11:20:05       14 阅读
  7. 谈谈std::map的lower_bound

    2024-05-14 11:20:05       16 阅读
  8. Linux-vi/vim

    2024-05-14 11:20:05       16 阅读
  9. CentOS常见的命令

    2024-05-14 11:20:05       15 阅读
  10. 水利行业工程设计资质如何去申请

    2024-05-14 11:20:05       18 阅读