当前位置:网站首页>C语言之数据插入
C语言之数据插入
2022-07-02 06:34:00 【FF小迷糊吖~】
编写程序,从键盘输入8个非递减有序排好的数列并放入一个一维数组中,再输入一个数elem,并将这个数elem插入到数组中,而且仍然保持非递减有序,若插入的数比原有所有的数都大时放在最后,比原有的数都小时放在最前面。
编程要求:
1、数列中数据都是整数;
2、非递减有序:就是从小到大或者允许中间有相等的情形,比如1 3 4 5 5 6就是非递减有序排列;
3、请利用数组的方法实现;
4、请严格按照输出效果输出;(影响成绩评定)
5、任务提交代码时需保证其符合行业代码规范,需进行必要的缩进及换行。(影响成绩评定)
运行效果1:
请按照非递减有序输入8个整数:
2 3 3 10 15 16 28 30
请输入elem:3
插入elem后的结果为:
2 3 3 3 10 15 16 28 30
请按任意键继续. . .
运行效果2:
请按照非递减有序输入8个整数:
3 4 10 15 16 66 78 80
请输入elem:81
插入elem后的结果为:
3 4 10 15 16 66 78 80 81
请按任意键继续. . .
运行效果3:
请按照非递减有序输入8个整数:
11 22 33 45 56 57 77 89
请输入elem:3
插入elem后的结果为:
3 11 22 33 45 56 57 77 89
请按任意键继续. . .
#include <stdio.h>
int main(void)
{
int n ,arr[9] ,i ,elem ,j;
printf("请按照非递减有序输入8个整数:\n");
for (i = 0; i < 8; i++)
scanf("%d" ,&arr[i]);
printf("请输入elem:");
scanf("%d" ,&elem);
for (i = 0; i < 8; i++)
if(arr[i] > elem)
break;
for (j = 7; j >= i; j--)
arr[j+1] = arr[j];
arr[i] = elem;
printf("插入elem后的结果为:\n");
for (i = 0; i <= 8; i++)
printf("%d " ,arr[i]);
printf("\n");
return 0;
}
#include <stdio.h>
int main(void)
{
int arr[9],elem;
int a,b,j;
printf("请按照非递减有序输入8个整数:\n");
for(a=0;a<8;++a)
{
scanf("%d",&arr[a]);
}
printf("请输入elem:");
scanf("%d",&elem);
printf("插入elem后的结果为:\n");
arr[8]=elem;
for(a=0;a<8;++a){
for(j=0;j<8-a;++j){
if(arr[j]>arr[j+1]){
b=arr[j];
arr[j]=arr[j+1];
arr[j+1]=b;
}
}
}
for(a=0;a<9;++a){
printf("%d ",arr[a]);
}
printf("\n");
return 0;
}
边栏推荐
- 微服务实战|原生态实现服务的发现与调用
- How to choose between efficiency and correctness of these three implementation methods of distributed locks?
- 【Go实战基础】gin 如何设置路由
- Micro service practice | introduction and practice of zuul, a micro service gateway
- Chrome浏览器插件-Fatkun安装和介绍
- Matplotlib剑客行——没有工具用代码也能画图的造型师
- Oracle modifies tablespace names and data files
- Thinkphp5 how to determine whether a table exists
- Idea view bytecode configuration
- Learn combinelatest through a practical example
猜你喜欢

微服务实战|手把手教你开发负载均衡组件

Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack

The channel cannot be viewed when the queue manager is running

I've taken it. MySQL table 500W rows, but someone doesn't partition it?

一篇详解带你再次重现《统计学习方法》——第二章、感知机模型

每天睡觉前30分钟阅读_day4_Files

Chrome browser tag management plug-in – onetab

Troubleshooting and handling of an online problem caused by redis zadd

定时线程池实现请求合并

概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
随机推荐
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
Microservice practice | load balancing component and source code analysis
VIM操作命令大全
Flink - use the streaming batch API to count the number of words
盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘
Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
微服务实战|负载均衡组件及源码分析
微服务实战|Eureka注册中心及集群搭建
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
西瓜书--第六章.支持向量机(SVM)
分布式锁的这三种实现方式,如何在效率和正确性之间选择?
Microservice practice | teach you to develop load balancing components hand in hand
MySQL事务
Redis zadd导致的一次线上问题排查和处理
Knife4j 2. Solution to the problem of file control without selection when uploading x version files
数构(C语言)——第四章、矩阵的压缩存储(下)
Safety production early warning system software - Download safety production app software
「Redis源码系列」关于源码阅读的学习与思考
Cartoon rendering - average normal stroke
Matplotlib剑客行——初相识Matplotlib