赞
踩
#include<stdio.h> int main() { int a[9] = {14,20,5,6,7,8,11,12}; int temp1, temp2, number=10, end; int i,j,key; for(i=1; i<8; i++)//用个插入排序 { key=a[i]; j=i-1; while(j>=0&&a[j]<key) { a[j+1]=a[j]; j--; } a[j+1]=key; } for(i = 0; i < 8; i++) printf("%3d", a[i]); printf("\n"); end = a[7];//先考虑特殊情况虽然在这不适用 if(number < end)//如果数字小于最小的数就直接插入到它后面 a[8] = number; else { for(i = 0; i < 8; i++) { if(a[i] < number) { temp1 = a[i]; a[i] = number; for(j=i+1; j<9; j++) { temp2 = a[j]; a[j] = temp1; temp1 = temp2; } break; } } } for(i=0; i<9; i++) printf("%3d", a[i]); printf("\n"); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。