C 练习实例39

C 语言教程 · 2019-02-21 21:41:35

题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。

实例

// Created by www.codercto.com on 15/11/9. // Copyright © 2015年 码农教程. All rights reserved. // #include<stdio.h> int main() { int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number,end,i,j; printf("原始数组是:\n"); for(i=0;i<10;i++) printf("%4d",a[i]); printf("\n插入一个新的数字: "); scanf("%d",&number); end=a[9]; if(number>end) a[10]=number; else { for(i=0;i<10;i++) { if(a[i]>number) { temp1=a[i]; a[i]=number; for(j=i+1;j<11;j++) { temp2=a[j]; a[j]=temp1; temp1=temp2; } break; } } } for(i=0;i<11;i++) printf("%4d",a[i]); printf("\n"); return 0; }

以上实例输出结果为:

原始数组是:
   1   4   6   9  13  16  19  28  40 100
插入一个新的数字: 10
   1   4   6   9  10  13  16  19  28  40 100

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

Machine Learning

Machine Learning

Kevin Murphy / The MIT Press / 2012-9-18 / USD 90.00

Today's Web-enabled deluge of electronic data calls for automated methods of data analysis. Machine learning provides these, developing methods that can automatically detect patterns in data and then ......一起来看看 《Machine Learning》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具