文章
5
粉丝
79
获赞
0
访问
3.2k
c语言暴力求解与时间复杂度为O(n)的求解 暴力求解 void Fac(int a[],int *n){ int i = *n,j=0,temp; while (i>1){ if( *n % i == 0 ){ a[j++] = i; } i--; } for (int k = 0; k < j/2; ++k) { temp = a[k]; a[k] = a[j-k-1]; a[j-k-1] = temp; } *n = j; } int main(){ int a[10000] = {0},n,count,maxlen=0,maxp=-1; scanf("%d",&n); Fac(a,&n); for (int i = 0; i < n; ++i) { count=1; for (int j = i; j < n; ++j) { if(a[j] + 1 != a[j+1]) break; count++; } if(count>maxlen){ maxlen = count; maxp = i; } } for (int i =maxp; i < maxp+maxlen; ++i) { printf("%d ",a[i]); } }
O(n)复杂度
void Fac(int a[],int *n){ int m = *n,i = 0,temp; while(m != 1){ if(*n % m == 0){ a[i++] = m; }...
登录后发布评论
暂无评论,来抢沙发