标签:des style blog color io ar for 2014 div
/* * ===================================================================================== * * Filename: min_two_selector.cpp * * Description: * 找出整形数组中的两个最小值,并将其和重新放入该数组 * * Version: 1.0 * Created: 09/14/2014 10:44:19 PM * Revision: none * Compiler: gcc * * Author: RollStone (rs), jealdean@outlook.com * Organization: RollStone. Ltd. Co * Last Change: 09/14/2014 10:44:19 PM * * ===================================================================================== */ #include <stdio.h> struct SA { int nSize; int retm, retm2; int select_min_two_values(int arr[]); }; //为了方便查找而实现的插入排序 void InsertSort(int arr[], unsigned int n) { int i, j; int tmp; for (i = 1; i < n; i++) { tmp = arr[i]; for (j = i; j > 0 && arr[j - 1] > tmp; j--) { arr[j] = arr[j - 1]; } arr[j] = tmp; } } int SA::select_min_two_values(int *sa) { int i = 0; //sort InsertSort(sa, nSize); //find none-zero value index for (i = 0; i < nSize; i++) { if (0 != sa[i]) { break; } } //get two values retm = sa[i]; retm2 = sa[i + 1]; //replace the addition to array,and set the minimum one‘s value be zero if(i<nSize-2){//将置零操作控制到倒数第三个数字的位置上 sa[i + 1] += sa[i]; }else{ sa[i+1]=0; } sa[i] = 0; return 0; } int main(int argc, char* argv[]) { SA asd; int arr[] = {5, 1, 1, 4, 4, 4, 3, 7, 5, 1}; asd.nSize = sizeof (arr) / sizeof (arr[0]); for (int i = 0; arr[asd.nSize-2]!=0; i++) { asd.select_min_two_values(arr); printf("%d %d\n", asd.retm, asd.retm2); } return 0; }
标签:des style blog color io ar for 2014 div
原文地址:http://www.cnblogs.com/jealdean/p/3972026.html