东方博宜OJ答案1241-1260

文章分享了C++编程中的几个实例,涉及基础算法如循环、递归、质数判断、数字分解、排序、组合等,强调了编程实践和逻辑思维的重要性。
摘要由CSDN通过智能技术生成

写在前面

        好久没有来更新了,非常抱歉,平时学业较为繁忙,也不是纯粹的计算机专业学生,请谅解一下,放假的时候可能会继续更新一点,之前在私信我收到了一些对我答案的纠正,非常感谢,我会尽快看并且改正我的错误。

1241

#include <iostream>
using namespace std;
int main(){
	int a;
	int num = 0;
	cin>>a;
	while(a-1){
		if(a%2==0){
			a/=2;
			num++;
		}
		else{
			a = a*3+1;
			num++;
		}
	}
	cout <<num;
	return 0;
}

1242

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
bool isPrime(int num) {
	if (num < 2) {
		return false;
	}
	for (int i = 2; i <= sqrt(num); i++) {
		if (num % i == 0) {
			return false;
		}
	}
	return true;
}
int main() {
	int n, k;
	cin >> n >> k;
	int* p = new int[n];
	for (int i = 0; i < n; i++) {
		cin >> p[i];
	}
	sort(p, p + n);
	int num = 0;
	num = p[n - k] - p[k - 1];
	if (isPrime(num)) {
		cout << "YES" << endl;
	}
	else {
		cout << "NO" << endl;
	}
	cout << num;
	delete[] p;
	return 0;
}

1243

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int CountZero(int n) {
	int count = 0;
	while (n) {
		if (n % 10 == 0) {
			count++;
		}
		n /= 10;
	}
	return count;
}
int main() {
	int n;
	cin >> n;
	int a = n / 100;
	int b = n % 100 / 10;
	int c = n % 10;
	int arr[3] = { a,b,c };
	sort(arr, arr + 3);
	if (CountZero(n) == 0) {
		cout << arr[0] << arr[1] << arr[2];
	}
	else if (CountZero(n) == 1) {
		cout << arr[1] << arr[0] << arr[2];
	}
	else {
		cout << arr[2] << arr[1] << arr[0];
	}
	return 0;
}

1244

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int Count(int n) {
	int count = 0;
	while (n != 1) {
		if (n % 2 == 0) {
			count++;
			n /= 2;
		}
		else {
			break;
		}
	}
	return count;
}
int main() {
	int n;
	cin >> n;
	cout << Count(n);
	return 0;
}

1245

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int sum = 0;
int Calculate(int n, int k) {
	int num = 0;
	//cout << "n = " << n << " k = " << k << endl;
	if (k == 1) {
		return sum + n;
	}
	else {
		for (int i = 0; i < k; i++) {
			//cout << "num = " << num << endl;
			//num = num * 10 + n;这句一开始原本被我写成num += num * 10 + n;后改正
			num = num * 10 + n;
			//cout << "num = " << num << endl;
		}
		sum += num;
		return Calculate(n, k - 1);
	}

}
int main() {
	int n;
	cin >> n;
	cout << Calculate(n, n);
	return 0;
}

标签里说要用递归于是我就使用了递归的写法,如果不用递归的话可以像东方博宜里面自带的题解去做,那个看起来更简洁一些。

1246

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= i; j++) {
			cout << i << "*" << j << "=" << i * j << " ";
		}
		cout << endl;
	}
	return 0;
}

1247

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		if (i <= (n + 1) / 2) {
			for (int j = 1; j <= i; j++) {
				cout << "*";
			}
			for (int j = 1; j <= -2 * i + n + 1; j++) {
				cout << " ";
			}
			for (int j = 1; j <= i; j++) {
				cout << "*";
			}
		}
		else {
			for (int j = 1; j <= -i + n + 1; j++) {
				cout << "*";
			}
			for (int j = 1; j <= 2 * i - n - 1; j++) {
				cout << " ";
			}
			for (int j = 1; j <= -i + n + 1; j++) {
				cout << "*";
			}
		}
		cout << endl;
	}
	return 0;
}

1248

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int Calculate(int n) {
	return (n * n + n) / 2;
}
int main() {
	int n;
	cin >> n;
	int i = 1;
	while (1) {
		if (Calculate(i - 1) < n && n <= Calculate(i)) {
			cout << i;
			break;
		}
		i++;
	}
	return 0;
}

1249

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
	for (int i = 0; i <= 9; i++) {
		for (int j = 0; j <= 12; j++) {
			if ((3.5 * i + 2.5 * j == 18) && (36 - i - j) % 2 == 0) {
				cout << i << " " << j << " " << 36 - i - j << endl;
			}
		}
	}
	return 0;
}

1250

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
	for (int i = 0; i <= 17; i++) {
		for (int j = 0; j <= 25; j++) {
			if (2 * i + j == 20) {
				cout << i << " " << j << " " << 30 - i - j << endl;
			}
		}
	}
	return 0;
}

1251

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
	for (int n = 1; n <= 13; n++) {
		if (n * (13 + n) * (26 - n) * (13 - n) == 7920) {
			int d = (13 - 2 * n) / 3;
			if (d > 0) {
				cout << n << " " << n + d << " " << n + 2 * d << " " << n + 3 * d << endl;
			}
		}
	}
	return 0;
}

需要进行一点数学的运算,比较简单,注意公差要大于零

1252

#include <iostream>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
int main() {
	set<int>p;
	int m, n;
	cin >> m >> n;
	for (int i = 0; i <= m; i++) {
		for (int j = 0; j <= n; j++) {
			p.insert(3 * i + 5 * j);
		}
	}
	for (int i : p) {
		if (i != 0) {
			cout << i << " ";
		}
	}
	cout << endl;
	cout << p.size() - 1;
	return 0;
}

1253

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
	for (int i = 32; i <= 99; i++) {
		int a = i * i / 1000;
		int b = (i * i) % 100 / 10;
		if (1100 * a + 11 * b == i * i) {
			cout << a << a << b << b;
		}
	}
	return 0;
}

1254

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
bool JudgeIfPalindrome(int n) {
	int x = 0;
	int s = n;
	while (s > 0)
	{
		x = x * 10 + s % 10;
		s = s / 10;
	}
	if (x == n)
	{
		return true;
	}
	else
	{
		return false;
	}
}
int main() {
	int len;
	for (int i = 95860;; i++) {
		if (JudgeIfPalindrome(i) == true) {
			len = i;
			break;
		}
	}
	double speed = (len - 95859) / 2;
	cout << speed << endl << len;
	return 0;
}

1255

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
	for (int p = 2; p <= 4; p++) {
		for (int q = 3; q <= 6; q++) {
			for (int r = 4; r <= 12; r++) {
				for (int s = 4; s <= 42; s++) {
					if (p <= q && q <= r && r <= s) {
						if (q * r * s + p * r * s + p * q * s + p * q * r == p * q * r * s) {
							cout << p << " " << q << " " << r << " " << s << endl;
						}
					}
				}
			}
		}
	}
	return 0;
}

一道有点意思的题目,有意思的点不在于这道题本身,如果想做出来这道题把各个数的判定条件放宽就可以暴力的求出结果(比如说p<=100这种条件),问题在于怎么把每一个数的条件都缩到最小,不难得知当四个数都为4时也是一种成立的情况,那么p有没有可能比4更大呢,答案是没有可能,当p比4大的时候,受制于p<=q<=r<=s,那么这个式子永远无法成立,所以p的范围是2到4,所以此时锁定p的值为最小值即2,后续也是如此,因为分母越小,在分子确定的情况下这个数就越大,留给后面的数自由发挥的空间也就越大。同理,当p锁定2时不难得知q的最大值为6,然后锁定q的使这个式子还有机会成立的最小值3(因为如果取2的话前两个直接就是1了就没有后面的事了),同理判断r的最大值为12,s的最大值为42.

1256

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
	cout << 60;
	return 0;
}

简单的排列问题,直接手算秒了,这绝对是时间复杂度最小的233333,想看老老实实的写法可以去看题解,本人就是喜欢偷鸡。

1257

#include <iostream>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
bool FindIfEqual(int arr[], int size) {
	set<int>FIE;
	for (int i = 0; i < size; i++) {
		if (FIE.count(arr[i]) > 0) {
			return true;
		}
		else {
			FIE.insert(arr[i]);
		}
	}
	return false;
}
int main() {
	int count = 0;
	for (int a = 1; a <= 9; a++) {
		for (int b = 1; b <= 9; b++) {
			for (int c = 1; c <= 9; c++) {
				for (int d = 1; d <= 9; d++) {
					for (int e = 1; e <= 9; e++) {	
						int arr[5] = { a,b,c,d,e };
						if (FindIfEqual(arr, 5) == false) {
							int num = 10 * (a * d - c * d) + 9 * (b * c - a * e) + (b * d - d * e);
							if (num == 0) {
								count++;
							}
						}
					}
				}
			}
		}
	}
	cout << count;
	return 0;
}

10 * (a * d - c * d) + 9 * (b * c - a * e) + (b * d - d * e)=0其实就是题目要求的这种算法给化简出来的结果。

1258

#include <iostream>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
int factorial(int n) {
	if (n == 0) {
		return 1;
	}
	int num = 1;
	for (int i = n; i >= 1; i--) {
		num *= i;
	}
	return num;
}
int main() {
	for (int i = 100; i <= 999; i++) {
		int a = i / 100;
		int b = i % 100 / 10;
		int c = i % 10;
		if (factorial(a) + factorial(b) + factorial(c) == i) {
			cout << i << endl;
		}
	}
	return 0;
}

1259

笑死了东方博宜上没有1259

1260

#include <iostream>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
int main() {
	int num = 0;
	//i代表黑球,j代表红球,k代表白球
	for (int i = 2; i <= 6; i++) {
		for (int j = 0; j <= 3; j++) {
			for (int k = 0; k <= 3; k++) {
				if (i + j + k == 8) {
					num++;
				}
			}
		}
	}
	cout << num;
	return 0;
}

这样的话1241-1260就全部做完了,总体而言我觉得没有什么非常大的难度,只要认真做都是能做出来的,希望各位继续努力。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

always-like-a-star

感谢支持,一定继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值