1484 分解N階乘

來源:互聯網
上載者:User

 

描述

分解N階乘

將N!分解成質數冪的乘積
從標準輸入讀取一個整數N(2≤N≤5000),將N!的質數冪的乘積分解式列印到標準輸出上,分解式中的質數按從小到大輸出。對重複出現的質因數,用指數形式表示。

 

輸入

輸入有m+1行。第一行為測試資料的組數m,下面的m行分別為m組測試資料。

輸出

輸出有m行,分別對應輸入的m組測試資料輸出其階乘的分解式

範例輸入
1
5
範例輸出
2^3*3*5

 

類比題

#include <stdio.h> #include <stdlib.h> struct line{ int pn; int i; struct line * next; }; void decompoFactor (struct line *p,struct line *q,int n); int main() { int n; int k=2; struct line *root,*p,*q; int number,te;scanf("%d",&number);for(te=1;te<=number;te++){k=2;scanf("%d",&n); if(n <2||n>5000){ printf(" error input"); return 0; } p=(struct line*)malloc(sizeof(struct line)); root=p;q=p;p->i=0; p->pn=2; p->next = NULL;        //初始化p->nextwhile (k<=n){ decompoFactor(p,q,k);k++;p=root; } p=root; if(p->i>1)printf("%d^%d",p->pn,p->i);elseprintf("%d",p->pn);p=p->next; while (p!=NULL){if(p->i>1)printf("*%d^%d",p->pn,p->i);if(p->i==1)printf("*%d",p->pn);p=p->next; }    printf("\n");}return 0;} void decompoFactor (struct line *p,struct line *q,int n){// 分解因數; while (p!=NULL){ if((n%(p->pn))==0){ n=n/(p->pn); (p->i)++; } if(n%(p->pn)!=0){q=p;    p=p->next;}} if(p==NULL&&n!=1){p=(struct line*)malloc(sizeof(struct line));q->next=p;p->i=1; p->pn=n; p->next=NULL;p=q;} }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.