文章时效性提示
本文发布于 500 天前,部分信息可能已经改变,请注意甄别。
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k(n%k==0),然后按下述步骤完成:
- 如果这个质数恰等于(小于的时候,继续执行循环)n,则说明分解质因数的过程已经结束,另外 打印出即可。
- 但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数n.重复执行第二步。
- 如果n不能被k整除,则用k+1作为k的值,重复执行第一步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include <stdio.h> void Cal(int input) { int temp = input; int arr[100]={0}; int j; int i,a; int count=0; while (temp != 1) { for (j=0; j<=100; j++) { for (i=2; i<=temp; i++) { if (temp%i==0) { temp = temp / i; arr[j]=i; printf("%d",arr[j]); break; } } if (temp != 1) { printf("*"); } } } printf("=%d\n",input); } int main() { int input = 0; printf("请输入一个正整数用于分解质因数:->"); scanf("%d",&input); Cal(input); return 0; }
|