按自己的思想简单写了一下,多多交流。
#include "stdafx.h" int insertInOrder(int *a,int num,int len){//这个方法可以改进很多,插入数据,再进行排序。 int position; if (a[0]==0) { a[0] = num; position = 1; } for (int i = 0; i < len-1;i++) { if (num>a[i] && num<a[i + 1]) { position = i + 2; for (int j = len-1; j > i+1;j--) { a[j] = a[j-1]; } a[i+1] = num; break; } if (num>a[i] && 0 == a[i + 1]) { position = i + 2; a[i + 1] = num; break; } } return position; } int _tmain(int argc, _TCHAR* argv[]) { int a[100] = { 0 }; int b[] = { 1, 2, 5, 8, 10, 4, 3, 6, 9, 7}; int count = 0; int i = 0; int position;//刚开始的时候想把position从1开始,也带了很多不便。 int len = sizeof(b) / sizeof(int); for (i = 1; i <= len; i++){ position = insertInOrder(a,b[i-1],i); if (count == (position - 1) && position == a[position - 1]) { printf("%d,", a[position - 1]); count++; for (int j = position + 1; j <= len; j++) { if (a[j-1]==0) { break; } if (count == (j - 1) && j == a[j - 1]) { count++; //i++; printf("%d,", a[j-1]); } } printf("\n"); } } return 0; }
<img src="http://img.blog.csdn.net/20150402234047745?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTQyNjM0MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
时间: 2024-10-31 06:41:39