ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C语言经典习题100例(八)36-40

2020-06-13 10:04:53  阅读:233  来源: 互联网

标签:int 36 40 ++ printf 习题 81


文章目录

<iframe allowfullscreen="true" data-mediaembed="bilibili" frameborder="0" id="BZhToZqN-1591879608758" src="https://player.bilibili.com/player.html?aid=413471436"></iframe>

不会玩阴阳师,但我照样带你一键下载所有卡牌,并识别文字信息


西瓜视频同步更新https://www.ixigua.com/i6836341666006172174/

习题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

<iframe allowfullscreen="true" data-mediaembed="edu_course" frameborder="0" id="KbiaMOAL-1591879863512" src="https://edu.csdn.net/course/blogPlay?goods_id=19446&blog_creator=CUFEECR&marketing_id=1256"></iframe>

图解Python数据结构与算法-实战篇

标签:int,36,40,++,printf,习题,81
来源: https://blog.csdn.net/CUFEECR/article/details/106694737

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有