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://www.codercto.com/courses/l/17.html

查看所有标签

Creative Selection

Creative Selection

Ken Kocienda / St. Martin's Press / 2018-9-4 / USD 28.99

Hundreds of millions of people use Apple products every day; several thousand work on Apple's campus in Cupertino, California; but only a handful sit at the drawing board. Creative Selection recounts ......一起来看看 《Creative Selection》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具