整序数组插入数据

| December 11, 2007 19:23 | timmy | Via Original
程序填空,不要改变与输入输出有关的语句。
输入一个正整数n(0例:括号内是说明
输入:
5         (n=5)
1 2 4 5 7
3  (x=3)
输出:
1 2 3 4 5 7



#include <stdio.h>
void main()
{
  int i,j,n,x,a[10];

  scanf("%d",&n);
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  scanf("%d",&x);
  for(i=0;i<n;i++)
    if(a[i]<x&&a[i+1]>=x)
      j=i+1;
  for(i=n;i>j;i--)
    a[i]=a[i-1];
  a[j]=x;
  for(i=0;i<=n;i++)
    printf("%d ",a[i]);
  putchar('\n');
}

Tags:
Program/Code » C/C++ | Comments(0) | Trackbacks(0) | Reads(168)
Add a comment
 Site URI
 Email
  Password Optional
 Nickname  *  [Register]
               

 
Emots
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember