VIP test questions basic exercises to decompose prime factors (C language)

Resource limit

Time limit: 1.0s Memory limit: 512.0MB

Problem Description

  Find the prime factorization of all integers in the interval [a, b].

Input format

  Enter two integers a, b.

Output format

  Each line outputs the decomposition of a number, like k=a1*a2*a3...(a1<=a2<=a3..., k is also from small to large) (see the example for details)

Sample input

3 10

Sample output

3=3
4=2*2
5=5
6=2*3
7=7
8=2*2*2
9=3*3
10=2*5

prompt

  First sift out all prime numbers, and then decompose.

Data size and convention

  2<=a<=b<=10000

 

#include<bits/stdc++.h>
using namespace std;
int pan(int n)
{
	int i,f=0;
	int k=(int)sqrt(n);
	for(i=2;i<=k;i++)
	{
		if(n%i==0)
		{
			f=1;
			break;
			
		 } 
		
	}
	if(f==1||n==1)
	return 0;
	else
	return 1;//是素数 
	
}
int main()
{
	int n,m,i,j;
	scanf("%d%d",&n,&m);
	
	for(i=n;i<=m;i++)
	{
		if(pan(i)==1)
		{
			printf("%d=%d\n",i,i);
		}
		else
		{
			int k=i;
			j=2;
			
			printf("%d=",k);
			while(k!=j)//结束的条件 
			{
				for(j=2;j<k;j++)
				{
					if(k%j==0&&k!=j)
					{
						printf("%d*",j);
						k=k/j;
						break;
					}
				}
			}
			printf("%d\n",k);
		}
	}
	return 0;
 } 

 

Guess you like

Origin blog.csdn.net/with_wine/article/details/115035274