标签:pre include swap const math void swa ring ==
/* 下一个排列--看图解。 */ #include<stdio.h> #include<malloc.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<string.h> #include <iostream> void swap(int nums[],int i,int j){ int t=nums[i]; nums[i]=nums[j]; nums[j]=t; } static int comp(const void *a,const void *b){ return *(int*)a-*(int*)b; } void nextPermutation(int* nums, int numsSize){ if(NULL==nums||0==numsSize){ return; } int i,j,k; for(i=numsSize-2;i>=0&&nums[i+1]<nums[i];i--); if(-1==i){ return qsort(nums,numsSize,sizeof(int),comp); } for(j=i+1;j<numsSize&&nums[i]<nums[j];j++); swap(nums,i,j-1); qsort(nums+i+1,numsSize-1-i,sizeof(int),comp); } int main() { /* 1,2,3,8,5,7,6,4 1,2,3,8,6,7,5,4 1,2,3,8,6,4,5,7 */ int nums[]={1,2,3,8,5,7,6,4}; nextPermutation(nums,8); int i; for(i=0;i<8;i++){ printf("%d ",nums[i]); } printf("\n"); return 0; }
标签:pre include swap const math void swa ring ==
原文地址:https://www.cnblogs.com/zhaohuan1996/p/12627851.html