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

习题36

求100之内的素数。

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

代码如下:

1#include <stdio.h> 2#include <math.h> 3 4 int main(){ 5 int isPrime(int n); 6 int i, count = 0; 7 for(i = 2; i < 101; i++){ 8 if(isPrime(i)){ 9 count++; 10 printf("%5d", i); 11 if(count % 5 == 0){ 12 printf("\n"); 13 } 14 } 15 } 16 17 return 0; 18} 19 20int isPrime(int n){ 21 int i, prime = 1; 22 for(i = 2; i <= sqrt(n); i++){ 23 if(n % i == 0){ 24 prime = 0; 25 break; 26 } 27 } 28 return prime; 29}

打印:

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

习题37

对10个数进行排序。

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

代码如下:

1#include <stdio.h> 2 3 int main(){ 4 void sort(int ua[], int l); 5 int i, unsorted_list[] = {12, 54, 81, 3, 72, 47, 99, 32, 41, 62}, *p; 6 printf("Unsorted:\n"); 7 for(i = 0; i < 10; i++){ 8 printf("%d ", unsorted_list[i]); 9 } 10 p = unsorted_list; 11 int length = sizeof(unsorted_list) / sizeof(unsorted_list[0]); 12 sort(p, length); 13 printf("\nAfter sorted:\n"); 14 for(i = 0; i < 10; i++){ 15 printf("%d ", unsorted_list[i]); 16 } 17 18 return 0; 19} 20 21void sort(int ua[], int l){ 22 int i, j, temp; 23 for(i = l - 2; i >= 0; i--){ 24 for(j = 0; j <= i; j++){ 25 if(ua[j] > ua[j + 1]){ 26 temp = ua[j]; 27 ua[j] = ua[j + 1]; 28 ua[j + 1] = temp; 29 } 30 } 31 } 32}

打印:

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

习题38

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

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

代码如下:

1#include <stdio.h> 2 3 int main(){ 4 int a[3][3] = {0}, i, j, sum = 0; 5 printf("Please input the 9 numbers:\n"); 6 for(i = 0; i < 3; i++){ 7 for(j = 0; j < 3; j++){ 8 scanf("%d", &a[i][j]); 9 } 10 } 11 for(i = 0; i < 3; i++){ 12 for(j = 0; j < 3; j++){ 13 if(i == j){ 14 sum += a[i][j]; 15 } 16 } 17 } 18 printf("Sum = %d\n", sum); 19 20 return 0; 21}

打印:

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

习题39

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

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

代码如下:

1#include <stdio.h> 2 3 int main(){ 4 int a[11] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 0}, num, i, j; 5 printf("Please input the number to insert:\n"); 6 scanf("%d", &num); 7 if(num >= a[9]){ 8 a[10] = num; 9 }else{ 10 i = 9; 11 while(a[i] > num){ 12 i--; 13 } 14 for(j = 10; j > i + 1; j--){ 15 a[j] = a[j - 1]; 16 } 17 a[i + 1] = num; 18 } 19 for(i = 0; i < 11; i++){ 20 printf("%d ", a[i]); 21 } 22 23 return 0; 24}

打印:

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

习题40

将一个数组逆序输出。

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

代码如下:

1#include <stdio.h> 2#define N 10 3 4 int main(){ 5 int a[N] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100}, num, i, j, temp; 6 printf("Normal order:\n"); 7 for(i = 0; i < 10; i++){ 8 printf("%d ", a[i]); 9 } 10 for(i = 0; i < N / 2; i++){ 11 temp = a[i]; 12 a[i] = a[ N - 1 - i]; 13 a[ N - 1 - i] = temp; 14 } 15 printf("\nReversed order:\n"); 16 for(i = 0; i < 10; i++){ 17 printf("%d ", a[i]); 18 } 19 20 21 return 0; 22}

打印:

1Normal order: 21 4 9 16 25 36 49 64 81 100 3Reversed order: 4100 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浏览更多优质原创内容。

点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

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