第一道题-幼儿园排班:
* 幼儿园小朋友排班,每个小朋友都有编号由0-999,且小朋友只知道自己跟前一个小朋友是不是一个班的
* 如: 1/N 2/Y 3/N 4/Y
* 输出 1 2
* 3 4
* 输出结果要编号排序输出,输出所有班级组合
* 1/N 2/Y 3/N 4/Y 5/N 6/N 7/Y
* 7/Y 3/N 2/Y 12/N 11/N 4/Y
* 2/Y 1/Y 3/N 234/N 45/N 11/Y
这道题考试的时候脑袋很乱,有些条件没考虑清楚,最后只有20%通过率,但是考完后复盘了一下应该没问题。 不过没办法提交了,哭死。。。 代码如下:
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String line = s.nextLine();
String[] str = line.split("\\s");
// System.out.println(Arrays.toString(str));
ArrayList<String> list = new ArrayList<>();
for (String s1 : str) {
list.add(s1);
}
System.out.println(list);
ArrayList<String> res = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
String s1 = list.get(i);
String[] split = s1.split("\\W");
if (split.length < 1 || Integer.valueOf(split[0]) < 0 || Integer.valueOf(split[0]) > 999){
System.out.println("ERROR");
return;
}
if (i+1 < list.size()){
String resStr = split[0];
// 找同组
for (int j = i+1; j < list.size(); j++) {
list.size();
String s2 = list.get(j);
if (s2.contains("Y")) {
resStr = resStr + " " + s2.split("\\W")[0];
i = j;
} else {
break;
}
}
res.add(resStr);
}
}
//对结果进行排序
List<String> collect = res.stream().sorted((s1, s2) -> Integer.compare(Integer.valueOf(s1.split(" ")[0]),Integer.valueOf(s2.split(" ")[0]))).collect(Collectors.toList());
for (String re : collect) {
String[] resList = re.split(" ");
Arrays.sort(resList);//输出时要按照顺序输出
for (String s1 : resList) {
System.out.print(s1+" ");
}
System.out.println("");
}
}
第二道题-全勤奖
缺勤问题 有:absent 、late 、leaveearly、present 四个状态 1.缺勤不能超过一次 2.不能连续的迟到或早退 3.任意连续七天,缺勤/迟到/早退次数 不超过3次
输入: 可拿全勤就输出true,不可以就输出false
2 //要计算的字符串
present
present late present absent present leaveearly present present present late present late present present leaveearly
present present present absent present leaveearly present late present present present late present present leaveearly
true
false
通过率 25%
这道题是我最疑惑的一道题,规则说的比较含糊,我的处理已经很详细了,但是我也不知道是哪里出了问题通过率就是只有25%。目前还没有找到原因,我自测的数据都是符合要求的。有大佬看明白问题的话迫切的希望可以指点我一下,代码如下
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String count = s.nextLine();
int counti = Integer.valueOf(count);
String absent = "absent"; //缺勤
String late = "late"; // 迟到
String leaveearly = "leaveearly"; // 早退
while (counti > 0){
String line = s.nextLine();
String[] split = line.split("\\s");
List<String> dayList = new ArrayList<>(Arrays.asList(split));
boolean hasAbsent = false;
int absentCount = 0;
boolean islianxu = false;
int fail = 0;
for (int i = 0; i < dayList.size(); i++) {
if (absent.equals(dayList.get(i))){
hasAbsent = true;
absentCount++;
fail++;
}
if (late.equals(dayList.get(i))||leaveearly.equals(dayList.get(i))){
fail++;
}
if (i > 0){
// 是否有连续 早退/迟到
String s1 = dayList.get(i);
String s2 = dayList.get(i-1);
if ((late.equals(s1) || leaveearly.equals(s1)) &&(late.equals(s2) || leaveearly.equals(s2))){
islianxu = true;
}
}
}
//System.out.println("hasAbsent = "+hasAbsent);
//System.out.println("islianxu = "+islianxu);
//System.out.println("fail = "+fail);
if (dayList.size() < 7){
// 如果小于7,则查看 缺勤和连续,失败是否大于3
if (!(hasAbsent || islianxu || fail <= 3)){
System.out.print(false+" ");
}else{
System.out.print(true+" ");
}
}else {
// 如果缺勤次数大于1,或者存在连续早退/迟到则失败
if (absentCount > 1 || islianxu){
System.out.print(false+" ");
}else {
//最多有几次缺勤/早退/迟到
int c = (dayList.size() / 7)*3;
if (c <= fail){
System.out.print(false+" ");
}else {
//每连续七次考勤不超过3次
int[] dp = new int[dayList.size()];
for (int i = 0; i < dayList.size(); i++) {
int fc = 0;
for (int j = 0; j < 7; j++) {
if (i+j < dayList.size()) {
String s1 = dayList.get(i+j);
if (s1.equals(absent)||s1.equals(late)||s1.equals(leaveearly)){
fc++;
}
}
}
dp[i] = fc;
}
//System.out.println("dp[]="+Arrays.toString(dp));
for (int i : dp) {
if (i > 3){
System.out.println(false+" ");
return;
}
}
System.out.println(true+" ");
}
}
}
counti--;
}
}