当前位置:网站首页>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;
}
边栏推荐
- 【Go实战基础】gin 如何绑定与使用 url 参数
- Chrome browser plug-in fatkun installation and introduction
- Chrome video download Plug-in – video downloader for Chrome
- Ora-12514 problem solving method
- hystrix 实现请求合并
- Chrome browser tag management plug-in – onetab
- Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
- 记录下对游戏主机配置的个人理解与心得
- Don't look for it. All the necessary plug-ins for Chrome browser are here
- 微服务实战|负载均衡组件及源码分析
猜你喜欢
洞见云原生|微服务及微服务架构浅析
Watermelon book -- Chapter 6 Support vector machine (SVM)
web安全与防御
数构(C语言--代码有注释)——第二章、线性表(更新版)
破茧|一文说透什么是真正的云原生
微服务实战|熔断器Hystrix初体验
【Go实战基础】gin 如何自定义和使用一个中间件
【Go实战基础】gin 如何设置路由
Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
How to use pyqt5 to make a sensitive word detection tool
随机推荐
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
[go practical basis] how to install and use gin
MySQL multi column in operation
VIM operation command Encyclopedia
"Redis source code series" learning and thinking about source code reading
机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
[go practical basis] gin efficient artifact, how to bind parameters to structures
hystrix 实现请求合并
【Go实战基础】gin 如何自定义和使用一个中间件
In depth analysis of how the JVM executes Hello World
Redis sorted set data type API and application scenario analysis
Knife4j 2. Solution to the problem of file control without selection when uploading x version files
Hystrix implements request consolidation
深入剖析JVM是如何执行Hello World的
企业级SaaS CRM实现
Watermelon book -- Chapter 5 neural network
Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
Solutions to Chinese garbled code in CMD window
微服务实战|负载均衡组件及源码分析
Supplier selection and prequalification of Oracle project management system