跳到正文

个人记录

2025年5月错题总结

WilliamYuanBao约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年5月24日
收录
2026年2月4日
更新
2026年11月13日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

T604735 拼三角形

第一次提交:

代码:

C++
#include <bits/stdc++.h>
using namespace std;
int n;
int temp;
vector <int> a;
int cnt = 0;
int main(){
	cin >> n;
	for (int i=0;i<n;i++){
		cin >> temp;
		a.push_back(temp);
	}
	for (int i=0;i<n;i++){
		for (int j=i+1;j<n;j++){
			for (int k=j+1;k<n;k++){
				if ((a[i]+a[j] > a[k]) && (a[j]+a[k] > a[i]) && (a[i]+a[k] > a[j])){
					cnt++;
					//cout << i << ' ' << j << ' ' << k << endl;
				}
			}
		}
	}
	cout << cnt;
}

评测分数 30

提交时间 2025-05-01 13:55:27

错因: 7个点TLE,数据范围n = 3000,用的3重for循环遍历每个元素,时间复杂度为 O(n3)O(n^3),过不了。

第二次提交

代码:

C++
#include <bits/stdc++.h>
using namespace std;
int n;
vector <int> a;
int ans = 0;
int temp;
int l,r,m;
int main(){
	cin >> n;
	for (int i=0;i<n;i++){
		cin >> temp;
		a.push_back(temp);
	}
	sort(a.begin(),a.end());
	for (int i=0;i<n;i++){
		for (int j=i+1;j<n;j++){
			l = j+1;
			r = n-1;
			while (l<r){
				m = (l+r+1)/2;
				if (a[i] + a[j] > a[m]){
					l = m;
				} else {
					r = m-1;
				}
			}
			if (l<n && a[i]+a[j]>a[l]){
				ans += l-(j+1)+1;
			}
		}
	}
	cout << ans;
}

评测分数 80

提交时间 2025-05-01 16:17:42

错因: 2个点WA,用的二分+双重for循环,但是不开long long见祖宗!

第三次提交

代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
vector <int> a;
int ans = 0;
int temp;
int l,r,m;
signed main(){
	cin >> n;
	for (int i=0;i<n;i++){
		cin >> temp;
		a.push_back(temp);
	}
	sort(a.begin(),a.end());
	for (int i=0;i<n;i++){
		for (int j=i+1;j<n;j++){
			l = j+1;
			r = n-1;
			while (l<r){
				m = (l+r+1)/2;
				if (a[i] + a[j] > a[m]){
					l = m;
				} else {
					r = m-1;
				}
			}
			if (l<n && a[i]+a[j]>a[l]){
				ans += l-(j+1)+1;
			}
		}
	}
	cout << ans;
}

评测分数 100

提交时间 2025-05-01 16:35:48

正确总结: 数据范围为3000,使用O(n2log⁡n)O(n^2 \log n)级别的时间复杂度(双重for循环+二分查找)过了,另外这题要开long long。

T604737 视力检测

第一次提交

代码:

C++
#include <bits/stdc++.h>
using namespace std;
int t;
string temp1,temp2,new_temp1,new_temp2;
int t1l,t2l,num_temp1,num_temp2;
int main(){
	cin >> t;
	while (t--){
		cin >> temp1;
		cin >> temp2;
		t1l = temp1.length();
		t2l = temp2.length();
		for (int i=0;i<t1l;i++){
			if (!(temp1[i] >= 'a' && temp1[i] <= 'z')){
				new_temp1 += temp1[i];
			}
		}
		for (int i=0;i<t2l;i++){
			if (!(temp2[i] >= 'a' && temp2[i] <= 'z')){
				new_temp2 += temp2[i];
			}
		}
		//cout << new_temp1 << ' ' << new_temp2 << endl;
		num_temp1 = stoi(new_temp1);
		num_temp2 = stoi(new_temp2);
		if (num_temp1 > num_temp2){
			cout << "left" << endl;
		} else if (num_temp2 > num_temp1){
			cout << "right" << endl;
		} else {
			cout << "both" << endl;
		}
		new_temp1 = "";
		new_temp2 = "";
	}
}

评测分数 0

提交时间 2025-05-01 14:27:00

错因: 把这题后面的比较部分想成了简单的数字比较,但是没有注意到数据范围,所以5个点全爆RE了。

第二次提交

damn码:

C++
#include <bits/stdc++.h>
using namespace std;
int t;
string temp1,temp2,new_temp1,new_temp2;
int t1l,t2l,num_temp1,num_temp2;
string doc(string x,string y){
    //cout<<x<<" "<<y;?
	if (x == y){
		return "both";
	} else if (x[0] != '-' && y[0] != '-'){
		if (x.length() > y.length()){
			return "left";
		} else if (x.length() < y.length()){
			return "right";
		} else {
			if (x > y){
				return "left";
			} else {
				return "right";
			}
		}
	}
    else if (((x[0]=='-')+(y[0]=='-')) == 1){
		if (x[0] == '-'){
			return "right";
		} else {
			return "left";
		}
	} else {
		if (x.length() > y.length()){
			return "right";
		} else if (x.length() < y.length()){
			return "left";
		} else {
			if (x > y){
				return "right";
			} else {
				return "left";
			}
		}
	}
}
int main(){
	cin >> t;
	while (t--){
		cin >> temp1;
		cin >> temp2;
		t1l = temp1.length();
		t2l = temp2.length();
		for (int i=0;i<t1l;i++){
			if (!(temp1[i] >= 'a' && temp1[i] <= 'z')){
				new_temp1 += temp1[i];
			}
		}
		for (int i=0;i<t2l;i++){
			if (!(temp2[i] >= 'a' && temp2[i] <= 'z')){
				new_temp2 += temp2[i];
			}
		}
		cout << doc(new_temp1,new_temp2) << endl;
		//cout << new_temp1 << ' ' << new_temp2 << endl;
		new_temp1 = "";
		new_temp2 = "";
	}
}

评测分数 100

提交时间 2025-05-01 16:53:55

正确总结:

这题需要先把小写字母去掉之后拼凑起来,然后按照字符串去比较,才不会爆。