当前位置:网站首页>C language PTA -- continuity factor

C language PTA -- continuity factor

2022-06-25 19:57:00 Make the best of the time

7-7 Continuous factor (20 branch )

                                                              The title is as you can see on your screen

  I know you may have some difficulties now , No problem , Keep reading , I'm sure you can see clearly

sample input :
630
sample output :
3
5*6*7
The code is as follows :

#include<stdlib.h>
#include<math.h>
int main() {
	int n,i,j,first=0,max_length=0,fact,f=0,length=0;
	scanf("%d",&n);

	for(i=2; i<=sqrt(n); i++) { // It must be prescribed , Otherwise, the operation will time out 
		fact=1;
		length=0;
		f=i;
		for(j=i; n%j==0&&n%(fact*j)==0; j++) {
			length++;
			fact=fact*j;
		}

		// Change and ml
		if(length>max_length) {
			first=f;
			max_length=length;
		}
	}
	// Printout 
	//printf("f=%d first=%d\n",f,first);

	if(max_length==0)
		printf("%d\n%d\n",1,n);
	else {
		printf("%d\n",max_length);
		for(i=first; i<first+max_length-1; i++) {
			printf("%d*",i);
		}
		printf("%d\n",i);
	}
	return 0;
}

  Thinking analysis :

1、 With 630=3*5*6*7 For example , factor “3” Not the most critical , The focus is on output 5*6*7;

2、 As shown in the figure above , This code is created by 3 Section composition :

        (1) Ergodic factor , Satisfy n% The present factor ==0;

        (2) Find the maximum length , And the assignment , Pay attention to this if It's on the second for Outside of the loop ;

        (3) Printout

原网站

版权声明
本文为[Make the best of the time]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190510209128.html