A. New Building for SIS
题解
签到题,无非就是在同一座塔内和不在同一座塔内分情况考虑
代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int h = cin.nextInt();
int a = cin.nextInt();
int b = cin.nextInt();
int k = cin.nextInt();
for(int i = 0;i < k;i++) {
int ta = cin.nextInt();
int fa = cin.nextInt();
int tb = cin.nextInt();
int fb = cin.nextInt();
if(ta == tb)//在一座塔内
System.out.println(Math.abs(fa - fb));
else {//不在同一座塔内
long tmp = Math.abs(ta - tb);
if(fa <= a && fb <= a) {
long tmp1 = Math.abs(fa - a);
long tmp2 = Math.abs(fb - a);
System.out.println(tmp + tmp1 + tmp2);
}
else if(fa >= b && fb >= b) {
long tmp1 = Math.abs(fa - b);
long tmp2 = Math.abs(fb - b);
System.out.println(tmp + tmp1 + tmp2);
}
else {
long tmp1 = Math.abs(fa - fb);
System.out.println(tmp + tmp1);
}
}
}
}
}
B. Badge
题解
题意是有n个学生干了一些不可描述的事,然后老师要抓人,当抓到一个学生的时候就给这个学生标记1,然后这个学生会说是另一个学生让他干的,然后老师就会找另一个学生,直到老师找到已经被标记过了两次的学生为止,输出这个学生的编号
也是水题,模拟一下就行了,从1号开始遍历,找到标记两次的,然后清空map,再从2号开始遍历,找到标记两次的,然后清空map,直到到n为止
代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int[] stu = new int[n + 1];
for(int i = 1;i <= n;i++)
stu[i] = cin.nextInt();
HashMap<Integer,Integer> map = new HashMap<>();
for(int i = 1;i <= n;i++) {
map.clear();
int curStu = i;
while(true) {
if(!map.containsKey(curStu))
map.put(curStu,1);
else {
int times = map.get(curStu);
map.put(curStu,++times);
if(times == 2) {
System.out.print(curStu + " ");
break;
}
}
curStu = stu[curStu];
}
}
}
}