C Exercise Example 39
100 cases of classic C language
Title: There has been a good array row sequence. Now enter a number, required by the laws of the original plug it into an array.
Program analysis: First, determine if this number is a number greater than the last, and then consider the case number is inserted in the middle, insert the number of this element after the turn after a shift position.
Source Code:
// Created by www.w3big.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; }
The above example output is:
原始数组是: 1 4 6 9 13 16 19 28 40 100 插入一个新的数字: 10 1 4 6 9 10 13 16 19 28 40 100