当前位置:网站首页>插一个数到排好序的数组中(冒泡+rand函数)
插一个数到排好序的数组中(冒泡+rand函数)
2022-04-22 03:51:00 【CTGU通玄上境编程手】
随机产生20个【1,100】的数据,从小到大排好序,从键盘输入一个数,插入到已经排好序的数组中不改变原来的排序规律。用C语言写的。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>//产生随机数要的头文件
#define N 20
int main() {
int a[N + 1], i, j, t,temp, posi;//
srand(time(0));//计算机没有办法产生真正的随机数的,所以你只调用rand,每次出来的东西
//是一样的,Srand是种下随机种子数,你每回种下的种子不一样,用Rand得到的随机数就不一样
for (i = 0; i < N; i++)
a[i] = rand() % 101;//rand()%n,就是随机0~(n-1)之间的数字
for (i = 1; i < N; i++)
for (j = 1; j < N; j++)//先用冒泡排序
if (a[j - 1] > a[j]) {
temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}
for (i = 0; i < N; i++)
printf("%d ", a[i]);
printf("\n说吧,你想输入啥数:");
scanf("%d", &t);
for (i = 0; i < N; i++)
if (t < a[i])
break;
posi = i;//记载插入数的下标
for (i = N - 1; i >= posi; i--)//将后面的数都后移一位
a[i + 1] = a[i];
a[posi] = t;
for (i = 0; i < N + 1; i++)//打印
printf("%d ", a[i]);
}
好文章就应该多点赞,这样才有更多的人看到!
版权声明
本文为[CTGU通玄上境编程手]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_62600503/article/details/124333278
边栏推荐
- Autodesk Genuine Service2020删除
- How do programmers take orders alone?
- 2021-10-20 interface test
- Deep learning and image recognition: principle and practice notes day_ fifteen
- Solve themedata in fluent Primarycolor does not take effect in components such as AppBar
- Sharing: web design specification
- 【树莓派C语言开发】实验12:PCF8591模数转换器模块
- DR/AP4029 outdoor IPQ-4019/4029 Outdoor directional-antennas
- Where is the whole house intelligence that Huawei, Haier Zhijia and Xiaomi are all doing?
- SaaS version goes online, and the applet application ecology goes further
猜你喜欢

js动态生成table表格,加滚动条

export ‘createStore‘ (imported as ‘createStore‘) was not found in ‘./ store/index. js‘ (possible expor

Recruitment - long term effectiveness

Botu monitor floating-point variable display 16 7fc0_ 0000 exception

Convenience stores are crazy: convenience bee, Rosen and Yijie "fierce battle"

【树莓派C语言开发】实验12:PCF8591模数转换器模块

Machine learning theory (6): from logistic regression (logarithmic probability) method to SVM; Why is SVM the maximum interval classifier

机器学习理论之(6):由逻辑回归(对数几率)方法到 SVM;为什么 SVM 是最大间隔分类器

博图监视浮点型变量显示16#7FC0_0000异常

Common convolutional neural network structures
随机推荐
Oracle architecture
How do programmers ensure that software is free of bugs?
Go gin framework configuration log output to file
Applet about subcontracting
Class component details
解决方案| 快对讲调度系统:高效协作
Deep learning and image recognition: principle and practice notes day_ fourteen
Vscode shell
js动态生成table表格,加滚动条
Une solution pour enregistrer l'échec de la connexion à distance MySQL dans la configuration du serveur Cloud
[leetcode daily question] 824 Goat Latin
机器学习理论之(6):由逻辑回归(对数几率)方法到 SVM;为什么 SVM 是最大间隔分类器
【C语言初学必看】之冒泡排序和竞选社长
MongoDB——聚合管道之$project操作
偶然间用到的sql语句
解决Flutter中ThemeData.primaryColor在AppBar等组件中不生效
Oracle 11g enterprise installation tutorial
Five methods of not displaying line number in history command in UNIX like system
容联七陌赋能企业智能化服务,重新定义客服价值
【网络实验】/主机/路由器/交换机/网关/路由协议/RIP+OSPF/DHCP