• 笔试中的流程控制题


    最近做到了两个笔试中的这种题目,这里备注一下

    大概就是需要你维护一个队列,然后根据一个什么过程模拟这个流程的进行

    一个是华为笔试的题目:

     

     

    这道题实际上就是维护一个队列,然后查表

    然而题意确实很复杂,写输入输出也要很久,总的来说还是很难搞

    import java.util.*;
    
    public class Solution2 {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            String[] timeStr=sc.nextLine().split(",");
            //存第一行数据
            List<Integer> list=new ArrayList<>();
            for(int i=0;i<timeStr.length;i++)
            {
                list.add(Integer.parseInt(timeStr[i]));
            }
    
            //存第二行数据
            String[] relyStr=sc.nextLine().split(",");
            List<Integer[]> table=new ArrayList<>();
            for(String str:relyStr)
            {
                Integer[] rely=new Integer[2];
                rely[0]=Integer.parseInt(str.split("->")[0]);
                rely[1]=Integer.parseInt(str.split("->")[1]);
                table.add(rely);
            }
            //输出代码
            Solution2 solution2=new Solution2();
            List<Integer> resultList=solution2.solve2(list,table);
            for(int i=0;i<resultList.size();i++)
            {
                System.out.print(resultList.get(i));
                if(i!=resultList.size()-1)
                {
                    System.out.print(",");
                }
            }
    
        }
    
        public List<Integer> solve2(List<Integer> time,List<Integer[]> table) {
            //time 时间表
    
            List<Integer> result = new ArrayList<>(time);//结果表
            HashSet<Integer> set = new HashSet<>();//已经执行完了吗
            Queue<Integer> line = new LinkedList<>();//排队表
            for (int i = 0; i < result.size(); i++) {
                line.add(i);//先进先出,序号为1的任务先执行
            }
    
            while (line.size() != 0) {
                int flag = 0;
                int test = line.poll();
                for (Integer[] fire : table) {
                    if (fire[0] == test) {
                        if (!set.contains(fire[1])) {
                            line.add(test);
                            flag = 1;
                            break;
                        }
                    }
                }
                if (flag == 0) {
                    set.add(test);
                    //都执行过了,该任务可以执行
                    for (int i = 0; i < result.size(); i++) {
                        if (!set.contains(i)) {
                            result.set(i, result.get(i) + time.get(test));//每一个未执行过的都要加上刚刚的执行时间
                        }
                    }
                }
            }
            return result;
        }
    
    }
  • 相关阅读:
    POJ 2584 T-Shirt Gumbo (二分图多重最大匹配)
    POJ 2584 T-Shirt Gumbo (二分图多重最大匹配)
    POJ 1904 King's Quest ★(强连通分量:可行完美匹配边)
    POJ 1904 King's Quest ★(强连通分量:可行完美匹配边)
    HDU 4638 Group ★(树状数组)
    HDU 4638 Group ★(树状数组)
    HDU 4632 Palindrome subsequence (区间DP)
    HDU 4632 Palindrome subsequence (区间DP)
    hdu2604 Queuing
    poj3757 Training little cats
  • 原文地址:https://www.cnblogs.com/take-it-easy/p/14653768.html
Copyright © 2020-2023  润新知