#include<stdio.h>
void main()
{
int r,i,j,end,t1,t2;
int a[6]={1,2,3,5,6};
/*int b[6];*/
printf("into a num:\n");
scanf("%d",&r);
end=a[4];
if(r>end) a[5]=r;
else
{for(i=0;i<5;i++)
{ if(a[i]>r)
{t1=a[i];
a[i]=r;
for(j=i+1;j<6;j++)
{
t2=a[j];
a[j]=t1;
t1=t2;
}
break; //break为啥在这个地方?而不是后面的两个括号里面
}
}
}
for(i=0;i<6;i++)
printf("%d ",a[i]);
}
...
for(i=len-2;i>=0;i--)
{
if(temp>a[i]) a[i+1]=temp;
else
{ j=a[i];
a[i]=temp;
a[i+1]=j;
}
}
... }
哥们你的程序编的好简单呀,不过调试了下,应该这样吧,还有我编的那个程序,break语句还是不太理解,可以再稍微解释清楚吗,
1 2 3 4 5
0 1 2 3 4 5 这是下标
开始时 i=4 也就是 a[4]
这样 如果temp > 5( 就是 a[4] ) 那就直接把 a[i+1] =temp 也就是a[5] ; 1 2 3 4 5 temp ; 这样可以直接退
如果 不是 那么 a[i+1] =a[i] , a[5] = a[4] ; 1 2 3 4 5 5 这是现在的数组情况,循环下一次 i--
如果temp > 4 (a[3] ,a[i]) ; 就把 a[i+1] =temp ; a[4] 1 2 3 4 temp 5; 然后退出break ;
如果不退 下一次 temp > a[i] 是肯定的 temp> 4 肯定也 temp>3
所以break;就是 防止数组所有下标被temp全部覆盖
你这个 break; 作用也是差不多防止被误覆盖
这个程序的原理 就是 先把后边的数挨个后移 覆盖 直到有一个符合条件就 把temp 插入数组,接下来必须退出,不然肯定会继续把temp插入数组的