codevs 2804 最大最小数质因数

题目描述 Description

    先输入n,n<=20;再依次输入n个不同的数,每个数<=1000000;找出最大数和最小数,并输出最大最小数的质因数,没有质因数则输出0。

输入描述 Input Description

数n,空行,输入n个数,每个数中间空行;

输出描述 Output Description

最大数的质因数,每个数中间空格;
最小数的质因数,每个数中间空格.

样例输入 Sample Input

2
15 6

样例输出 Sample Output

3 5
2 3

代码:

/*
分解质因数
*/
#include<cstdio> #include<algorithm> using namespace std; int n,a[1001],maxx,minn=999999999; void cf(int x) { if(x<2) { printf("0"); return ; } int y=x; for(int i=2;i<=x;i++) if(x%i==0) { if(i==y) { printf("0"); return ; } printf("%d ",i); while(x%i==0) x/=i; } } int main() { int i,j; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); maxx=max(maxx,a[i]); minn=min(minn,a[i]); } cf(maxx); printf("\n"); cf(minn); return 0; }

 

posted @ 2016-11-15 19:48  一叶落尽天下秋  阅读(168)  评论(0编辑  收藏  举报