IO流登录注册小练习
public class demo02 {
public static void main(String[] args) throws IOException {
/*
需求:写一个登录小案例
步骤:
将正确的用户名和密码手动保存在本地的userinfro.txt文件中
保存格式为:username=zhangsan&password=123
让用户键盘录入用户名和密码
比较用户录入的和正确的用户名和密码是否一致
如果一致则打印登录成功
如果不一致打印登录失败
*/
FileOutputStream fos =new FileOutputStream("C:\\Users\\Administrator\\IdeaProjects\\day01\\src\\_2024_07_06\\userinfro.txt");
String str="username=zhangsan&password=123";
byte[]arr=str.getBytes();
fos.write(arr);
//1.读取正确的用户名和密码
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\IdeaProjects\\day01\\src\\_2024_07_06\\userinfro.txt"));
String line=br.readLine();
//System.out.println(line);
br.close();
String[] userfro=line.split("&");
String[] arr1=userfro[0].split("=");
String[] arr2=userfro[1].split("=");
String rightusername=arr1[1];
String rightpassword=arr2[1];
//2.用户输入用户名和密码
Scanner sc=new Scanner(System.in);
System.out.println("请输入正确的用户名");
String username=sc.nextLine();
System.out.println("请输入正确的密码");
String password=sc.nextLine();
//3.比较
if(rightusername.equals(username)&&rightpassword.equals(password)){
System.out.println("登录成功");
}
else{
System.out.println("登录失败");
}
}
}
多线程:
进程是程序的基本执行实体
线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位
简单理解:应用软件中互相独立,可以同时运行的功能
作用:可以让程序同时做多件事情,提高效率
并发和并行:
并发:在同一时刻,有多个指令在单个CPU上交替执行
并行:在同一时刻,有多个指令在多个CPU上同时进行
多线程的实现方式:
1.继承Thread类的方式进行实现
2.实现Runnable接口的方式进行实现
3.利用Callable接口和Future接口方式实现
public class demo03 {
public static void main(String[] args) {
/*
1.定义一个类继承Thread
2.重写run方法
3.创建子类的对象,进行多线程
*/
Mythread mythread = new Mythread();
Mythread mythread2 = new Mythread();
mythread.setName("1");
mythread2.setName("2");
mythread.start();
mythread2.start();
}
}
public class demo01 {
public static void main(String[] args) {
//Mycallable对象(表示多线程要执行的任务)
MyCallable callable = new MyCallable();
//FutureTask对象(管理多线程运行的结果)
FutureTask ft1 = new FutureTask(callable);
//Thread对象并启动(表示线程)
Thread thread1 = new Thread(ft1);
thread1.start();
}
}
等待唤醒机制:
吃货机制:
public class Foodie extends Thread{
/*
1.循环
2.同步代码块
3.判断共享数据是否到了末尾(建议先写到了末尾的情况)
4.(没有到末尾,执行核心代码逻辑)
*/
@Override
public void run() {
while(true){
synchronized(Desk.lock){
if(Desk.count==0){
break;
}
else{
//判断桌子上是否有面条
if(Desk.foodflag==0){
//如果有,就开吃
try {
Desk.lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
else{
//把吃的总数-1
Desk.count--;
//如果有,就开吃
System.out.println("吃货在吃面条,还能再吃"+Desk.count+"碗");
//吃完之后,唤醒厨师继续做
Desk.lock.notifyAll();
//修改桌子的状态
Desk.foodflag--;
}
}
}
}
}
}
厨师机制:
public class Cook extends Thread{
@Override
public void run() {
/*
1.循环
2.同步代码块
3.判断共享数据是否到了末尾
4.没有到末尾,执行核心逻辑
*/
while(true){
synchronized(Desk.lock){
if(Desk.count==0){
break;
}
else{
//判断桌子上是否有食物
if(Desk.foodflag==1){
try {
Desk.lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
else{
System.out.println("厨师做了一碗面条");
//修改桌子上的食物状态
Desk.foodflag=1;
//叫醒等待的消费者开吃
Desk.lock.notify();
}
}
}
}
}
}
桌子(控制):
public class Desk {
//存储是否有面条
public static int foodflag=0;
//总个数
public static int count=10;
//锁对象
public static Object lock=new Object();
}
测试类:
public class demo02 {
public static void main(String[] args) {
Cook cook = new Cook();
Foodie foodie = new Foodie();
cook.setName("厨师");
foodie.setName("吃货");
cook.start();
foodie.start();
}
}
运行结果:
=========================================================================
P2392 kkksc03考前临时抱佛脚
#include <iostream>
#include<algorithm>
#include<math.h>
using namespace std;
const int N=30;
int s[N];//S数组记录了每个科目习题集的题目数
int t[N][N];//记录每个习题集不同题目所需要的时间
int ans=0;//记录最终所需要的时间
int minn=1e9;
int l;int r;//分别表示加在左脑和右脑的时间
void work(int x,int y){
if(x>s[y]){
minn=min(max(l,r),minn);
return;
}
l+=t[y][x];
work(x+1,y);
l-=t[y][x];
r+=t[y][x];
work(x+1,y);
r-=t[y][x];
}
int main(){
for(int i=1;i<=4;i++)cin>>s[i];
for(int i=1;i<=4;i++){
for(int j=1;j<=s[i];j++){
cin>>t[i][j];
}
l=0;
r=0;
work(1,i);
ans+=minn;
minn=1e9;
}
cout<<ans;
}