当前位置:网站首页>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;
}
边栏推荐
- Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
- Demand delineation executive summary
- 互联网API接口幂等设计
- Microservice practice | declarative service invocation openfeign practice
- Troubleshooting and handling of an online problem caused by redis zadd
- Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
- Who is better for Beijing software development? How to find someone to develop system software
- Redis zadd导致的一次线上问题排查和处理
- Timed thread pool implements request merging
- 企业级SaaS CRM实现
猜你喜欢
Microservice practice | teach you to develop load balancing components hand in hand
Idea view bytecode configuration
数构(C语言--代码有注释)——第二章、线性表(更新版)
View the port of the application published by was
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
Matplotlib swordsman line - first acquaintance with Matplotlib
互联网API接口幂等设计
Typora安装包分享
告别996,IDEA中必装插件有哪些?
[go practical basis] how to customize and use a middleware in gin
随机推荐
【Go实战基础】如何安装和使用 gin
Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别
分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
西瓜书--第五章.神经网络
长篇总结(代码有注释)数构(C语言)——第四章、串(上)
深入剖析JVM是如何执行Hello World的
A detailed explanation takes you to reproduce the statistical learning method again -- Chapter 2, perceptron model
聊聊消息队列高性能的秘密——零拷贝技术
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
Knife4j 2. Solution to the problem of file control without selection when uploading x version files
微服务实战|熔断器Hystrix初体验
定时线程池实现请求合并
Typeerror: X () got multiple values for argument 'y‘
Solution to amq4036 error in remote connection to IBM MQ
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
Statistical learning methods - Chapter 5, decision tree model and learning (Part 1)
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
In depth analysis of how the JVM executes Hello World
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
别找了,Chrome浏览器必装插件都在这了