2019
n个有序整数 n (- 0 ~100 插入整数m
使得序列依然有序
本来还想搞个算法来着 后来想想还是算了 基础太薄弱了
值得一提的是这个条件表达式 实现了输入0 0不做处理 退出程序
while (scanf("%d%d", &n, &m), n || m)
#include <stdio.h> int main(void) { int n,m,i,mid,j[100]; while(scanf("%d%d",&n,&m)) { for(i = 0;i < n;i++) { scanf("%d",&j[i]); }//0 -> n-1 for(i = 0;j[i] < m;i++); //j[i] >= m j[i + 1] = j[i]; j[i] = m; i++; for(;i < n + 1;i++) { j[i + 1] = j[i]; } for(i = 0;i < n + 1;i++) printf("%d ",j[i]); } return 0; }
参考答案
#include <stdio.h> int main(void) { int n, i, m, x[101]; while (scanf("%d%d", &n, &m), n || m) { for (i = 0 ; i < n ; i++) scanf("%d", x + i); for (i = n ; i && x[i - 1] > m ; i--) x[i] = x[i - 1]; x[i] = m; for (i = 0 ; i < n + 1 ; i++) printf("%d%c", x[i], (i - n ? ' ' : '\n')); } return 0; }
========================if i have some wrong, please give me a message, thx.========================