首页 > 学院 > 开发设计 > 正文

1059. Prime Factors (25)

2019-11-08 19:24:57
字体:
来源:转载
供稿:网友

特殊处理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) PRintf("*%d^%d", i, count); else if (count == 1)printf("*%d", i); } else { if (count > 1) printf("%d^%d", i, count); else if (count == 1)printf("%d", i); flag = 1; } } cout << endl;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表