60分,请大佬指导
查看原帖
60分,请大佬指导
1071773
yiguorui楼主2024/10/13 07:20
#include<bits/stdc++.h>
using namespace std;
bool zs (int x) {
	if(x==0 || x==1) return 0;
	for (int i = 2; i <= sqrt(x); i++) {
		if(x % i == 0) return 0; 
	}
	return 1;
}
int main() {
	string a;
	while (1) {
		cout << "Enter the number=" << endl;
		cin >> a;
		bool flag = 0;
		int num = 0;
		int l = a.length();
		for (int i = 0; i < l; i++) {
			if (a[i] >= '0' && a[i] <= '9') {
				flag = 1;
				num = num * 10 + a[i] - '0';
			}
		}
		if(!flag) return 0;
		cout << "Prime? ";
		flag = 0;
		if (num > 40000000) {
			cout << "No!" << endl;
			cout << "The number if too large!" << endl << endl;
			continue;
		}
		if (!zs(num)) {
			cout << "No!" << endl;
			if (num == 1 || num == 0) {
				cout << endl;
				continue;
			}
			cout << num << "=";
			for(int i = 2; i <= num; i++) {
				int cnt = 0;
				while(num % i == 0) {
					num /= i;
					cnt++;
				}
				if (cnt) {
					if(!flag) {
						cout << i << "^" << cnt;
						flag = 1;
					}
					else cout << "*" << i << "^" <<cnt;
				}
			}
			cout<<endl;
		} else {
			cout << "Yes!" << endl;
		}
		cout << endl;
	}
	return 0;
}
2024/10/13 07:20
加载中...