『壹』 c語言因式分解

// 下面是用我在toj 10004上面通過的代碼,稍加修改寫成的。
#include <stdio.h>
#include <math.h>
int Prime(int x)
{
int n, i;
n = (int)sqrt(x);
for (i = 2; i <= n; i++)
if (x % i == 0) break;
if (i > n)
return 1;
else
return 0;
}
int main()
{
// freopen("2.txt","w",stdout);
int x;
int t;
int i , n;
int y;
int first;
int max;
while (scanf("%d",&max) == 1)
{
for (y = 2; y <= max; y++)
{
x = y;
if (Prime(x))
{
printf("%d=%d\n",x,x);
}
else
{
printf("%d=",x);
first = 1;
do
{
for (i = 2; i <= x; i++)
{
t = 0; n = 0;
while (x % i == 0)
{
t = 1;
n++;
x = x/i;
}
if (t)
{
if (first) first = 0;
else printf("*");
while (n>1)
{
printf("%d*",i);
n--;
}
printf("%d",i);
}
}
} while(x != 1);
printf("\n");
}
}
}
return 0;
}

『貳』 c語言 因式分解

/*tc2.0通過,其他編譯器(如gcc)可能要做相應修改*/
main()
{
int num,i;
clrscr();
printf("please enter num:");
scanf("%d",&num);
printf("%d=1",num);
i=2;
while(i<=num)
{
while(num%i==0)
{
printf("*%d",i);
num=num/i;
}
i++;
}
printf("\n");
getch();
}

『叄』 【求助啊】分解因式 c語言

http://..com/question/346500603.html?push=rece&group=1

【求助啊】分解因式 c語言 檢舉 | 離問題結束還有 14 天 3 小時 提問者:浮雲的守護者 | 懸賞分:30 | 瀏覽次數:40次

給出一個正整數a,要求分解成若干個正整數的乘積,即a = a1 * a2 * a3 * ... * an,並且1 < a1 <= a2 <= a3 <= ... <= an,問這樣的分解的種數有多少。注意到a = a也是一種分解。
輸出應是一個正整數,指明滿足要求的分解的種數

例子,20有4種表示方法問題補充:

說思路把我說明白也可以~~

謝謝了

需要指出,你所說的是「因數分解」(小學數學學習的內容)而非中學才學的「因式分解」(後者在計算機上實現的難度要大很多)。當然,「因數分解」不包括只有一個因數的情況。

對於本題,想必是給中學生的競賽題目,不需大動干戈建立質數庫。

至少有兩種思路。一按照字典序逐個生成各種分解,如20的分解:(2, 2, 5)、(2, 10)、(4, 5)、(20)在字典序下依次遞增,按照這種順序依次生成各種分解方式。另外一種是按照分解後的因數個數逐類生成,每一類按照字典序逐個生成,例如,對於20,一個因數的情形一種,(20);兩個因數的情形兩種:(2, 10)、(4, 5);三個因數的情形一種(2, 2, 5);四個及以上的因數的情形0種。

為節約空間,先估計一個不太大的因數個數的上限,最簡單的方法就是找到所給整數a(不妨假定為正整數)的最小素因數,設為b1,令n=[log_b1 (a)](以b1為底的a的對數取整,用C語言表示為n=(int)(log(1.0*a)/log(b1*1.0))。
如果要精確一點,以節省空間(特別是當a比較大時,這是必須的),可以先找出所有素因子的個數(包括重復),以確定分解後最多有多少個因數,也不復雜,可以這樣確定n。

D=a;
m=(int)(sqrt(D))+1, n=0, c=2;
while (m>c)
{ if(D%c==0)
{ n++;
printf("%d", c);
D=D/c;
if(D==1) break;
else printf(", ", c);
m=(int)(sqrt(D))+1;
}
else c++;
if(m<=c) { n++; printf("%d ", D); break; }
}

printf(")\n The number of prime factors of the integer is %d.\n", n);

下面討論如何用第一種思路給出a的所有分解(樓主可以自行考慮第二種思路,難度是一樣的;但是第一種方法可以減少一些重復計算又不增大空間開銷)。

由於a至多可以分解為n個因數直積,用2個數組分別表示各個因數以及各個因數的大致取值范圍。用A[n]表示各個因數,B[n]表示各個因數的范圍
除去只有一個因數的情況,a至少要分解為兩個因數A[1]*A[2],其中A[1]<=A[2],所以A[1]<=(int)(sqrt(a)),遍歷時A[1]只需從b1(a的最小素因數)遍歷到(int)(sqrt(a))即可。如果只找分解為兩個因數的情況,這就足夠了。對a/A[1]==0的A[1],設E[1]=a/A[1],如果A[1]*A[1]<=E[1],則還可以分解為3個因數的乘積,其中A[2]的遍歷范圍從A[1]到(int)(sqrt(E[1]));如此重復,對E[1]/A[2]==0的A[2],設E[2]=E[1]/A[2],如果A[2]*A[2]<=E[2],則還可以分解為4個因數的乘積,其中A[3]的遍歷范圍從A[2]到(int)(sqrt(E[2]));……
直到對某個k,使得A[k]*A[k]>E[k],令A[k+1]=E[k]。這樣,得到一個分解方式:a=A[1]*A[2]*……*A[k]*A[k+1]。
回溯時,令A[k]自加,如果E[k-1]%A[k]==0,又得到另外一種分解方式。當A[k]的所有情況都遍歷完之後,令 A[k-1]自加重新進行遍歷。
直到A[1]遍歷完所有情況為止。
遍歷程序只需要把上面的求素因數個數的程序修改一下即可。

一個可行的程序如下:

#include <stdio.h>
#include <malloc.h>
#include <math.h>

main()
{ int a, b, c, n, F, F1, D, num, i, I, m;
int *A=NULL, *B=NULL, *E=NULL;

printf("\n This program will get the number of unsorted factorizations of a given integer. Please input a positive interger greater than 1.\n The number input: ");
scanf("%d", &a);
if(a<2)
{ printf("\n Input Error. The integer you input is not valid.\n");
return 0;
}

printf("\n The Prime factors of the given integer %d are as follow: \n \t ( ", a);

D=a;
m=(int)(sqrt(D))+1, n=0, c=2;
while (m>c)
{ if(D%c==0)
{ n++;
printf("%d", c);
D=D/c;
if(D==1) break;
else printf(", ", c);
m=(int)(sqrt(D))+1;
}
else c++;
if(m<=c) { n++; printf("%d ", D); break; }
}

printf(")\n The number of prime factors of the integer is %d.\n", n);

if(n==1)
{ printf("\n The integer you input is a prime. The number of unsorted factorizations is 1.\n");
return 1;
}

A=(int*)malloc(sizeof(int)*(n+1));
if(A==NULL)
{ printf("\n Error. Can not get enough space in the memory.\n");
return 0;
}

B=(int*)malloc(sizeof(int)*(n+1));
if(B==NULL)
{ printf("\n Error. Can not get enough space in the memory.\n");
return 0;
}

E=(int*)malloc(sizeof(int)*(n+1));
if(E==NULL)
{ printf("\n Error. Can not get enough space in the memory.\n");
return 0;
}

E[0]=a, A[0]=2, A[1]=1, num=0;
B[1]=(int)(sqrt(E[0]))+1;

I=1;
while (I>0 && I<=n)
{ F=0, F1=0;
// printf("\n I=%d, B[I]=%d, E[I-1]=%d, A[I-1]=%d. ", I, B[I], E[I-1], A[I-1]);
//if(I>n) { printf("\n Error. I=%d>n=%d.\n", I, n); return -1; }
for( A[I]++; A[I]<B[I]; A[I]++)
{ //printf("\n A[I]=%d, ", A[I]);
if(E[I-1]%A[I]==0)
{ //printf(" valid.");
E[I]=E[I-1]/A[I]; F++, F1++;
if( E[I]<A[I]*A[I] && F1==0) // E[I] !=1
{ printf("\n A valid factorization : %d=", a);
if(I>0) printf("%d", A[1]);
if(I>1) for(i=2; i<I; i++) printf("*%d", A[i]);
printf("*%d", E[I]);
num++;
F=0;
}
else // F1!=0 || E[I]>=A[I]*A[I]
{ B[I+1]=(int)(sqrt(E[I]))+1; A[I+1]=A[I]-1; I++; break; }
} //end if(E[I-1]%A[I]==0)
//else printf(" not valid.");
} // end loop I

if (F==0 && F1==0) // E[I-1] is not divisible by all possible A[I].
{ printf("\n A valid factorization : %d=", a);
if(I>1)
{ printf("%d*", A[1]);
for(i=2; i<I; i++) printf("%d*", A[i]);
}
printf("%d", E[I-1]);
num++, I--;
}

} // while (I>0)

printf("\n The number of unsorted factorizations of the given integer is %d.\n", num);

free(A);
free(B);
free(E);
return 1;
}

通過gcc編譯,當輸入20時,結果如下:

This program will get the number of unsorted factorizations of a given integer. Please input a positive interger greater than 1.
The number input: 20
The Prime factors of the given integer 20 are as follow:
( 2, 2, 5 )
The number of prime factors of the integer is 3.

A valid factorization : 20=2*2*5
A valid factorization : 20=2*10
A valid factorization : 20=4*5
A valid factorization : 20=20
The number of unsorted factorizations of the given integer is 4.

當輸入36時,結果如下

This program will get the number of unsorted factorizations of a given integer. Please input a positive interger greater than 1.
The number input: 36
The Prime factors of the given integer 36 are as follow:
( 2, 2, 3, 3 )
The number of prime factors of the integer is 4.

A valid factorization : 36=2*2*3*3
A valid factorization : 36=2*2*9
A valid factorization : 36=2*3*6
A valid factorization : 36=2*18
A valid factorization : 36=3*3*4
A valid factorization : 36=3*12
A valid factorization : 36=4*9
A valid factorization : 36=6*6
A valid factorization : 36=36
The number of unsorted factorizations of the given integer is 9.

『肆』 C語言 因式分解

給你個思路吧,首先,任何數可以表示為:x=p1^N1 × p2^N2 ×……pi^Ni,其中pi 是質數,
因此,你可以如下遞歸,當然,具體怎麼做還是希望你自己實現,自己動手理解更深刻嘛。
//建立一個足夠大的質數庫
const int MAXP = 31622;
const int PCOUNT = 3401;
int prim[PCOUNT];
int max, numb;
void primes()
{
bool get[MAXP+1];
int i;
for(i = 0;i <= PCOUNT;i++) prim[i] = 0;
for(i = 2;i <= MAXP;i++) get[i] = true;
for(i = 2;i <= MAXP;i++)
if (get[i])
{
int j = i+i;
while(j <= MAXP){get[j] = false;j += i;}
}
for (int ii = 2,j = 0;ii <= MAXP;ii++)
if(get[ii]) prim[++j] = ii;
}

遞歸函數(當前遞歸起始質數index,上一級遞歸數字n)
{
for (int i=index;i<質數庫數量;i++)
{
if (x == n*prim(index))//說明找到一種分解
{
print
return;
}
else(x < n*prim(index))//找過了,退回去
{
return;
}
else
{
下一級遞歸...
}
}
}
有問題歡迎追問,滿意請點贊

『伍』 90的因式分解用c語言表達

//num的因式分解用c語言表達
#include<stdio.h>
#include<math.h>
int main()
{
int m,i,j=0,k,g[1000],n,t,num;
printf("請輸入需要因式分解的數字:\n");
scanf("%d",&num);
t=num;
for(m=2;m<=t;m++)
{
k=sqrt((double)m);
for(i=2;i<=k;i++)
{
if(m%i==0)
break;
}
if(i>k)
{
g[j]=m;
j++;
}
}
n=j;
int count=0,q[1000],p;
for(i=0;i<n;i++)
{
while(1)
{
p=t/g[i];
if(p*g[i]==t)
{
q[count]=g[i];
count++;
if(p==1)
{
goto here;
}
t=p;
}
else
{
break;
}
}
}
here:;
printf("%d的因式分解如下:\n",num);
for(i=0;i<count;i++)
{
printf("%d ",q[i]);
}
printf("\n");
}
//輸入90就行了 很貼心吧 求點贊!

『陸』 c語言因子分解

#include <stdio.h>
int main ()
{
int m,k;
k=2;
printf("輸入一個數:");
scanf("%d",&m);
// if (m==2){ 其實都不用版先判權斷2
// printf("%d",m);
// return 0;
// }
printf("1");
for (k=2;k<=m;k++)
{
while(m%k==0)
{
m /= k;
printf("*%d",k);
}
}
}

『柒』 求,用c語言編寫的一個程序,編寫程序,因式分解多項式

多項式的次數是多少?,兩次的簡單點,三次,三次以上有點困難

『捌』 c語言——一元二次函數因式分解

#include#includevoidm(floata,floatb,floatc){doublex1,x2;x1=(-b+sqrt(b*b-4*a*c))/(2*a);x2=(-b-sqrt(b*b-4*a*c))/(2*a);printf("方程的根是%.2lf和%.2lf",x1,x2);}voidn(floata,floatb,floatc){doublex;x=(-b)/(2*a);printf("方程的根為%.2lf",x);}voidf(floata,floatb,floatc){printf("方程無實數根\n");}main(){floata,b,c;printf("請輸入a,b,c的值\n");scanf("%f%f%f",&a,&b,&c);if(b*b-4*a*c>0)m(a,b,c);if(b*b-4*a*c==0)n(a,b,c);if(b*b-4*a*c<0)f(a,b,c);}

『玖』 一道c語言求因式分解的種類

#include<iostream>

#include<cstdio>

using namespace std;

int sum=0;

void recur(int N,int i)

{

if(N==1) sum++;

while(i<=N)

{

if(N%i==0) recur(N/i,i);

i++;

}

return ;

}

int main()

{

//freopen("in.txt","r",stdin);

int T=0;

cin>>T;

while(T--)

{

int N=0;

cin>>N;

int i=2,res=1;

while(i<=N/2)

{

if(N%i==0)

{

sum=0;

recur(N/i,i);

res+=sum;

}

i++;

}

cout<<res<<endl;

}
return 0;

}
別人寫的 你看下 能理解就好 主要還是遞歸的思想

『拾』 C語言 因式分解問題

#include<stdio.h>

int IsPrime(int n)
{
int i,r=1;

if(n<=1)
{
r=0;
}
else
{
for(i=2;i<=n-1;i++)
{
if(0==n%i)
{
r=0;
break;
}
}
}
return r;
}
void OutputPrimeFactor(int n)
{
int over=0,i=2;

while(!over)
{
if(IsPrime(i))
{
while(0==n%i)
{
//i是最後一個質因子
if(n==i)
{
printf("%d\n",i);
over=1;
break;
}
else
{
printf("%d*",i);
n=n/i;
}
}
if(!over)
{
i++;
}
}
else
{
i++;
}
}
}
int main()
{
int n,a,b;

scanf("%d %d",&a,&b);
for(n=a;n<=b;n++)
{
if(n<=1)
{
printf("%d不能分解質因子!\n",n);
}
else
{
printf("%d=",n);
OutputPrimeFactor(n);
}
}
printf("\n");
return 0;
}