【思路】:先打表,后循环。
【AC代码】:
#include <iostream> #include <algorithm> #include <iomanip> #include <cstdio> #include <cmath> using namespace std; #define MAX 10000 int prime_list[MAX], cnt = 0; int isPrime(int n) { int i = 0; for (i = 2; i <= sqrt(n); i++) { if (0 == n%i) return 0; } return 1; } void makeList(int a, int b) { int i = 0, j = 0; for (i = 2; i <= b; i++) { if (isPrime(i)) { prime_list[j++] = i; cnt++; } } } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int a = 0, b = 0; int i = 0, j = 0; //input cin >> a >> b; makeList(a, b); //output for (i = a; i <=b; i++) { cout << i << "="; int temp = i, flag = 1;; while (1 != temp) { for (j = 0; j < cnt; j++) { if (0 == temp%prime_list[j]) { if (flag) { cout << prime_list[j]; flag = 0; } else cout << "*" << prime_list[j]; temp /= prime_list[j]; break; } } } cout << endl; } return 0; }
原文地址:http://blog.csdn.net/weijj6608/article/details/44469497