第一次双周赛

本文通过四个编程竞赛题目,揭示了参赛者在解决实际问题时可能遇到的陷阱,包括边界条件处理不当、细节忽略、算法优化不足以及逻辑判断错误。题目涉及字符串处理、数值计算、链表操作和求解方程。这些错误提醒我们在编程时要细心检查边界、理解题意、优化算法并充分测试特殊案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#题1
能放就放的思想

#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	string ori,temp=".";

	cin >> n;
	cin >> ori;
	for (int i = 0; i < n; i++) {
		if (ori[i] == '.'&&temp!="L") {
			if (ori[i + 1] == '.'||i+1==n) {
				
				ori[i] = 'C';
			}
		}
		temp = ori[i];
	}
	cout << ori;
}

#题2
a1[i] - ‘A’) >= 0,考试的时候没打等于,而样例有没有0,我直接完美的跳进了自己挖到坑,之后越找越急,在往后就全崩了。

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000;
int a[maxn], b[maxn];
int main() {
	//freopen("3in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	string a1, b1;
	map <int, string> shu;
	shu[0] = "0";
	shu[1] = "1";
	shu[2] = "2";
	shu[3] = "3";
	shu[4] = "4";
	shu[5] = "5";
	shu[6] = "6";
	shu[7] = "7";
	shu[8] = "8";
	shu[9] = "9";
	shu[10] = "A";
	shu[11] = "B";
	shu[12] = "C";
	shu[13] = "D";
	shu[14] = "E";
	shu[15] = "F";
	cin >> a1 >> b1;
	if (a1.length() < b1.length()) {
		swap(a1, b1);
	}
	reverse(a1.begin(), a1.end());
	reverse(b1.begin(), b1.end());
	for (int i = 0; i < a1.length(); i++) {
		if ((a1[i] - 'A') >= 0) {
			a[i] = a1[i] - 'A' + 10;
		}
		else {
			a[i] = a1[i] - '0';
		}
	}
	for (int i = 0; i < b1.length(); i++) {
		if ((b1[i] - 'A') >= 0) {
			b[i] = b1[i] - 'A' + 10;
		}
		else {
			b[i] = b1[i] - '0';
		}
	}
	int temp = 0;
	int sum[2000]={0};
	for (int i = 0; i < b1.length(); i++) {
		for (int j = 0; j < a1.length(); j++) {
			temp = b[i] * a[j]  + sum[i + j];
			while (temp > 15) {
				temp -= 16;
				sum[i + j + 1] = sum[i + 1 + j] + 1;
			}
			sum[i + j] = temp;

		}
	}
	reverse(sum, sum + maxn);
	int i = 0;
	for (; i < maxn; i++) {
		if (sum[i] == 0 && sum[i + 1] != 0) {
			break;
		}
	}
	i++;
	string sum1,c;
	for (; i < maxn; i++) {
		c = shu[sum[i]];
		sum1 = sum1 + c;
	}
	cout << sum1;
}

#题3
在本地调试的时候把scan函数里的m改成了100,后面教的时候没发现,之后,之后就寄了。。

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int n, m, ori[maxn];
bool scan(int k) {
	int temp = 0;
	for (int i = 0; i < n; i++) {
		if (ori[i] - temp > m) {
			temp += ori[i] - temp - m;
		}
		if (ori[i] - temp < 0) {
			return false;
		}
		temp += k;
	}
	return true;
}

int main() {
	//freopen("prob3in.txt", "r", stdin);
	//freopen("prob3out.txt", "w", stdout);
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> ori[i];
	}
	sort(ori, ori + n);
	int r, l;
	r = m, l = 0;
	while (l < r) {
		int mid = (l + r) / 2 + 1;
		if (scan(mid) == 1) {
			l = mid;
		}
		else {
			r = mid - 1;
		}
	}
	cout << r;

}

#题4
测试样例有一种链表上的节点数小于n的情况,考试的时候没想到,。

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
map <string, int> ori;
map <int, string> next1;
map<int, string> address;
int num[maxn];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, k;
	string add0;
	cin >> add0 >> n >> k;
	for (int i = 0; i < n; i++) {
		int temp2;
		string temp1, temp3;
		cin >> temp1 >> temp2 >> temp3;
		ori[temp1] = temp2;
		address[temp2] = temp1;
		next1[temp2] = temp3;
	}
	string next2 = add0;
	for (int i = 0; i < n; i++) {
		num[i] = ori[next2];
		if (next1[num[i]] == "-1") {
			n = i + 1;
			break;
		}
		next2 = next1[num[i]];
	}
	//读入

	for (int i = 0; i < (n / k); i++) {
		static int* p = num;
		if ((i + 1) * k > n) {
			break;
		}
		reverse(p, p + k);
		p += k;
	}
	//翻转
	for (int i = 0; i < n - 1; i++) {
		cout << address[num[i]] << " " << num[i] << " " << address[num[i + 1]] << endl;
	}
	cout << address[num[n - 1]] << " " << num[n - 1] << " " << -1 << endl;
}

#题5
求导后分三段,多好的方法啊,可惜我没想到,我还想他给个一元二次的求根公式干嘛。

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
double a, b, c, d, p, q;
double a1, b1, c1;
double san(double x) {
	return x * x * x * a + b * x * x + c * x + d;
}
double er(double x) {
	return x * x * a1 + x * b1 + c1;
}
void suan(double l, double r) {

	double mid = (l + r) / 2;
	if (er(mid) > 0) {
		while (r - l > 0.00000001)
		{
			mid = (r + l) / 2;
			if (san(mid) > 0) {
				r = mid;
			}
			else {
				l = mid;
			}
		}
		cout << r << " ";
	}
	else {
		while (r - l > 0.00000001)
		{
			mid = (r + l) / 2;
			if (san(mid) < 0) {
				r = mid;
			}
			else {
				l = mid;
			}
		}
		cout << r << " ";
	}
}
int main() {
	//freopen("3in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int t;
	cin >> t;
	while (t--) {
		cin >> a >> b >> c >> d >> p >> q;
		a1 = 3 * a;
		b1 = 2 * b;
		c1 = c;
		double m2 = (-b1 - sqrt(b1 * b1 - 4 * a1 * c1)) / (2 * a1);
		double m3 = (-b1 + sqrt(b1 * b1 - 4 * a1 * c1)) / (2 * a1);
		cout << fixed << setprecision(6);
		if (m2 > m3)swap(m2, m3);
		suan(p, m2);
		suan(m2, m3);
		suan(m3, q);
		cout << endl;

	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值