40分求调,在线等,必回关
查看原帖
40分求调,在线等,必回关
1364428
I_am_sb2楼主2025/7/28 21:22

代码

#include <bits/stdc++.h>
using namespace std;
int t;

bool zs(int h) {
	for (int i = 2; i * i <= h; i++) {
		if (h % i == 0)
			return 0;
	}
	return 1;
}

int main() {
	cin >> t;
	while (t--) {
		int h, w, l = 0, cnt = 1, cnt1 = 1;
		cin >> h;
		w = h;
		if (zs(w)) {
			cout << 1 << endl;
			continue;
		}
		int j = 1;
		while (w != 0) {
			if (w < 0) {
				break;
			}
			cnt++;
			w -= int(pow(2, j - 1));
			j++;
		}
		if (w == 0) {
			cout << cnt << endl;
			continue;
		}
		while (!zs(h)) {
			h--;
			l++;
		}
//		cout << h;
		int i = 1;
		bool s = 1;
		while (l != 0) {
			if (l < 0) {
				cout << -1 << endl;
				s = 0;
				break;
			}
			cnt1++;
			l -= int(pow(2, i - 1));
//			cout << int(pow(i - 1, 2));
			i++;
		}
		if (s) {
			cout << cnt1 << endl;
		}
	}
	return 0;
}

测点

2025/7/28 21:22
加载中...