1.今天进行了能力测试感觉自己算法方面的能力还有所欠缺,虽然前两天已经写过跟多模板题但是等到真正在题中运用的时候感觉还是有些力不从心。
下面来看一下这个唯一做出来的算法题 其实用的逻辑非常简单 就是用了一个二分的方法来判断是否能借到教室,不过这里有一个非常容易踩坑的地方就是输入要用scanf而不是cin 是残废相对于cin来说 速度更快更迅速
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define Max 1000011
int m, n;
long long diff[Max], need[Max], r[Max], l[Max], d[Max], a[Max];
bool find(int x)
{
memset(diff, 0, sizeof(diff));
for (int i = 1; i <= x; i++)
{
diff[l[i]] += d[i];
diff[r[i] + 1] -= d[i];
}
for (int i = 1; i <= n; i++)
{
need[i] = need[i - 1] + diff[i];
if (need[i] > a[i])return 0;
}
return 1;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
scanf("%d", & a[i]);
}
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &d[i], &l[i], &r[i]);
}
int begin = 1, end = m;
if (find(m))
{
cout << "0";
return 0;
}
while (begin < end)
{
int mid = (begin + end) / 2;
if (find(mid))
{
begin = mid + 1;
}
else end = mid;
}
cout << "-1" << endl << begin;
}
2.项目方面新增了一个创建班级的功能,其实创建班级的大体思路和注册差不多,也是来读取文本框中的信息然后对数据库进行操作来插入,同时运用了雪花算法来生辰个随机的id保证创建班级的唯一性
@FXML
private void CreateNameAction(ActionEvent event) throws IOException, NoSuchAlgorithmException, ClassNotFoundException {
// 获取文本框信息
String ClassName = ClassNameField.getText();
String Subject = SubjectName.getText();
String ClassId = ClassIdField.getText();
String Name = TeacherName.getText();
long snowid = SnowFlakeUtil.getID();
String snowid1 = Long.toString(snowid);
String snowids = snowid1.substring(8);
// 确保雪花 ID 足够长,避免 substring 报错
// 判断文本框内容是否为空
if (ClassName == null || ClassName.isEmpty() || Subject == null || Subject.isEmpty() || snowids.isEmpty() || ClassId == null || ClassId.isEmpty() || Name == null || Name.isEmpty()) {
// 创建一个信息提醒框
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("没有填写关键信息,请仔细检查");
// 显示提醒框并等待用户关闭
alert.showAndWait();
} else {
// 创建 user 对象
Users2 u = new Users2(snowid, ClassName, Subject, ClassId, Name);
// 根据选中的 RadioButton 设置 Operation
String Operation = MarkTool.createclass;
// 序列化,并传输提示词和 user 对象
oos.writeObject(Operation);
oos.writeObject(u);
System.out.println("创建课程请求已发送");
// 接收注册结果
String response = Connection.ois.readObject().toString();
if (response.equals(MarkTool.createclassfail)) {
// 创建一个信息提醒框
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("没有填写完整信息,请您检查");
// 显示提醒框并等待用户关闭
alert.showAndWait();
} else if (response.equals(MarkTool.createclasssuccess)) {
// 注册成功信息提示
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("注册界面");
alert.setHeaderText(null);
alert.setContentText("创建" + Subject + "课程成功");
alert.showAndWait();
}
/* oos.flush();
ois.close();*/
System.out.println("服务器响应" + response);
}
}