- #include<iostream>
- #include<ctime>
- using namespace std;
- void main()
- {int n,*p,*p1,m,t,a(0),temp,k(0);
- cout<<"请输入整数的个数:";
- cin>>n;
- p=new int[n];
- p1=new int[40];
- srand(time(0));
- s:for(int i=0;i<n;i++)
- p[i]=rand();
- for(int j=0;j<n;j++)
- for(i=j+1;i<n;i++)
- if(p[j]==p[i])
- goto s;
- cout<<"随机生成"<<n<<"个整数,分别分解为质因子"<<endl<<endl;
- cout<<n<<"个互不相同的整数:"<<endl;
- if(n<=5)
- for(i=0;i<n;i++)
- cout<<'\\t'<<p[i];
- else
- {for( i=0;i<n;i++)
- {
- if(i%5==0)
- cout<<endl;
- cout<<'\\t'<<p[i];
- }}
- cout<<endl;
- cout<<"升序排序结果:"<<endl;
- for(i=0;i<n;i++)
- {m=i;
- for(j=i+1;j<n;j++)//选择法排序
- if(p[j]<p[m])
- m=j;
- if(m!=p[i])
- {t=p[i];
- p[i]=p[m];
- p[m]=t;}}
- for(i=0;i<n;i++)
- {if(i%5==0)
- cout<<endl;
- cout<<'\\t'<<p[i];
- }cout<<endl;
- cout<<"分解质因子结果:"<<endl;
- for(i=0;i<n;i++)
- { temp=p[i];
- for(j=2;j<=p[i];)
- {if(p[i]%j==0)
- {p1[a]=j;
- p[i]=p[i]/j;
- a++;}
- else
- j++;
- }
- if(a==1)
- {cout<<'\\t'<<temp<<"="<<temp;
- a=0;
- cout<<endl;}
- else
- {cout<<'\\t'<<temp<<"=";
- for(int u=0;u<a-1;u++)
- cout<<p1[u]<<'*';
- cout<<p1[a-1];
- a=0;
- cout<<endl;}
- }}
- //该片段来自于http://www.codesnippet.cn/detail/030120148377.html
来源: http://www.codesnippet.cn/detail/030120148377.html