C语言基础习题50例(八)36-40

CuterCorley
• 阅读 1231

习题36

求100之内的素数。

实现思路: 使用函数实现,并循环遍历依次判断。

代码如下:

#include <stdio.h>
#include <math.h>

 int main(){
    int isPrime(int n);
    int i, count = 0;
    for(i = 2; i < 101; i++){
        if(isPrime(i)){
            count++;
            printf("%5d", i);
            if(count % 5 == 0){
                printf("\n");
            }
        }
    }

    return 0;
}

int isPrime(int n){
    int i, prime = 1;
    for(i = 2; i <= sqrt(n); i++){
        if(n % i == 0){
            prime = 0;
            break;
        }
    }
    return prime;
}

打印:

    2    3    5    7   11
   13   17   19   23   29
   31   37   41   43   47
   53   59   61   67   71
   73   79   83   89   97

习题37

对10个数进行排序。

实现思路: 可使用冒泡法或其他方法对数进行排序,一般都需要经过交换过程。

代码如下:

#include <stdio.h>

 int main(){
    void sort(int ua[], int l);
    int i, unsorted_list[] = {12, 54, 81, 3, 72, 47, 99, 32, 41, 62}, *p;
    printf("Unsorted:\n");
    for(i = 0; i < 10; i++){
        printf("%d ", unsorted_list[i]);
    }
    p = unsorted_list;
    int length = sizeof(unsorted_list) / sizeof(unsorted_list[0]);
    sort(p, length);
    printf("\nAfter sorted:\n");
    for(i = 0; i < 10; i++){
        printf("%d ", unsorted_list[i]);
    }

    return 0;
}

void sort(int ua[], int l){
    int i, j, temp;
    for(i = l - 2; i >= 0; i--){
        for(j = 0; j <= i; j++){
            if(ua[j] > ua[j + 1]){
                temp = ua[j];
                ua[j] = ua[j + 1];
                ua[j + 1] = temp;
            }
        }
    }
}

打印:

Unsorted:
12 54 81 3 72 47 99 32 41 62
After sorted:
3 12 32 41 47 54 62 72 81 99

习题38

求一个3*3矩阵对角线元素之和。

实现思路: 利用双重for循环控制输入二维数组,再将i和j相同的数组元素累加后输出。

代码如下:

#include <stdio.h>

 int main(){
    int a[3][3] = {0}, i, j, sum = 0;
    printf("Please input the 9 numbers:\n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            scanf("%d", &a[i][j]);
        }
    }
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            if(i == j){
                sum += a[i][j];
            }
        }
    }
    printf("Sum = %d\n", sum);

    return 0;
}

打印:

Please input the 9 numbers:
1 2 3 4 5 6 7 8 9
Sum = 15

习题39

有一个已经排好序的数组。 现输入一个数,要求插入后该数组还是有序的。

实现思路: 先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。

代码如下:

#include <stdio.h>

 int main(){
    int a[11] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 0}, num, i, j;
    printf("Please input the number to insert:\n");
    scanf("%d", &num);
    if(num >= a[9]){
        a[10] = num;
    }else{
        i = 9;
        while(a[i] > num){
            i--;
        }
        for(j = 10; j > i + 1; j--){
            a[j] = a[j - 1];
        }
        a[i + 1] = num;
    }
    for(i = 0; i < 11; i++){
        printf("%d ", a[i]);
    }

    return 0;
}

打印:

Please input the number to insert:
50
1 4 9 16 25 36 49 50 64 81 100

习题40

将一个数组逆序输出。

实现思路: 将数组均分成两半,用前后对应位置的元素交互即可。 也可以通过两个数组,前后位置的元素交换。

代码如下:

#include <stdio.h>
#define N 10

 int main(){
    int a[N] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100}, num, i, j, temp;
    printf("Normal order:\n");
        for(i = 0; i < 10; i++){
        printf("%d ", a[i]);
    }
    for(i = 0; i < N / 2; i++){
        temp = a[i];
        a[i] = a[ N - 1 - i];
        a[ N - 1 - i] = temp;
    }
    printf("\nReversed order:\n");
        for(i = 0; i < 10; i++){
        printf("%d ", a[i]);
    }


    return 0;
}

打印:

Normal order:
1 4 9 16 25 36 49 64 81 100
Reversed order:
100 81 64 49 36 25 16 9 4 1

本文原文首发来自博客专栏C语言实战,由本人转发至https://www.helloworld.net/p/0D4HpeSlMtdg,其他平台均属侵权,可点击https://blog.csdn.net/CUFEECR/article/details/106694737查看原文,也可点击https://blog.csdn.net/CUFEECR浏览更多优质原创内容。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593
Stella981 Stella981
2年前
C# Aspose.Cells导出xlsx格式Excel,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
报错信息:最近打开下载的Excel,会报如下错误。(xls格式不受影响)!(https://oscimg.oschina.net/oscnet/2b6f0c8d7f97368d095d9f0c96bcb36d410.png)!(https://oscimg.oschina.net/oscnet/fe1a8000d00cec3c
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Stella981 Stella981
2年前
Nginx反向代理upstream模块介绍
!(https://oscimg.oschina.net/oscnet/1e67c46e359a4d6c8f36b590a372961f.gif)!(https://oscimg.oschina.net/oscnet/819eda5e7de54c23b54b04cfc00d3206.jpg)1.Nginx反
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这