算法提高 8-1因式分解
时间限制:10.0s 内存限制:256.0MB
问题描述
设计算法,用户输入合数,程序输出若个素数的乘积。例如,输入6,输出2*3。输入20,输出2*2*5。
样例
与上面的样例输入对应的输出。
例:
例:
![](https://i-blog.csdnimg.cn/blog_migrate/46eea56c6e9a481e2b13e26846a51601.png)
![](https://i-blog.csdnimg.cn/blog_migrate/02002ed766b61f3803dc776a06759e48.png)
数据规模和约定
输入数据中每一个数在int表示范围内。
作者注释:这题巧用while循环的话蛮容易的。
1 #include<stdio.h> 2 main(){ 3 int n,x=1; 4 scanf("%d",&n); 5 bool flag=true; 6 while(n!=1){ 7 x++; 8 while(n%x==0){ 9 n/=x; 10 if(flag){ 11 flag=false; 12 printf("%d",x); 13 }else{ 14 printf("*%d",x); 15 } 16 } 17 } 18 }