特殊處理2,然后一次對x/3,/5。。。判斷,知道x變成0
#include<iostream>#include<cmath>using namespace std;int main(){ long int x; cin >> x; cout << x << "="; if (x == 0 || x==1) {//額外處理0,1,的情況 cout << x << endl;exit(0); } int count = 0;int flag = 0; while (x % 2 == 0) { //先把2處理掉 count++; x = x / 2; flag = 1; } if (count >1) cout << "2^" << count; else if (count == 1) cout << "2"; for (int i = 3;x!=1;i += 2)//按3.5.7...順序循環,知道x變成1 { count = 0; while (x%i == 0) { count++; x /= i; } if (flag == 1) { if (count > 1)新聞熱點
疑難解答