zoukankan      html  css  js  c++  java
  • 纪念第一次笔试全AC,居然来自滴滴~

    第一题:电报破解

      其实就是字符串模拟,给一个字符串(含空格),以及反转的长度。最终将字符串破解。

    #include <iostream>
    #include <vector>
    #include <stack>
    #include <string>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #include <algorithm>
    #include <stdlib.h>
    #include <map>
    using namespace std;
    
    int main()
    {
        string str;
        int n;
        scanf("%d", &n);
        // 输入一行数据
        getchar();
        getline(cin, str);
    
        int length = str.size();
    
        // 如果n>=length,直接输出
        if(n >= length){
            for(int i = length - 1; i >= 0; i--)
                printf("%c", str[i]);
            printf("
    ");
        }
        else{
            // 之前的
            int before = length / n;
            int i = 0;
            for(int i = 1; i <= before; i++){
                for(int j = i * n - 1; j >= (i - 1) * n; j--)
                    printf("%c", str[j]);
            }
            int after = length % n;
            if(after != 0){
                for(int i = length - 1; i >= length - after; i--)
                    printf("%c", str[i]);
            }
            printf("
    ");
        }
        return 0;
    }
    

    第二题:村庄建桥

      给n个村庄,m座桥,m座桥的建桥成本w,并给出成本限制k,判断能否建桥使得村庄互通。这个题理解了其实就很简单,就是一个判断图是否连通,简单DFS就完事儿了。

    #include <iostream>
    #include <vector>
    #include <stack>
    #include <string>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #include <algorithm>
    #include <stdlib.h>
    #include <map>
    using namespace std;
    
    #define MAXVEX 100
    #define INFINITYNUM 65535
    
    // 邻接矩阵
    struct GraphAdiMatix{
        int numVertexes;
        int numEdges;
        // 顶点表
        int vexs[MAXVEX];
        // 邻接矩阵
        int arc[MAXVEX][MAXVEX];
    };
    
    
    void DFSMatix(GraphAdiMatix G, bool visit[], int i){
        visit[i] = true;
        for(int j = 0; j < G.numVertexes; j++){
            if(G.arc[i][j] != INFINITYNUM && !visit[j]){
                DFSMatix(G, visit, j);
            }
        }
    }
    
    // (Depth First Search, DFS)深度优先遍历邻接矩阵(其实就像二叉树的前序遍历)
    int DFSTraveseMatix(GraphAdiMatix G){
        int count = 0;
        bool visit[MAXVEX];
        memset(visit, false, sizeof(visit));
        for(int i = 0; i < G.numVertexes; i++){
            if(!visit[i]){
                DFSMatix(G, visit, i);
                // 如果是连通图那么只会一次
                count++;
            }
        }
        return count;
    }
    
    int main(int argc, char* argv[])
    {
    
        int T;
        scanf("%d", &T);
        while(T--){
            GraphAdiMatix G;
            // 输入顶点数和边数
            int n, m, k;
            scanf("%d %d %d", &n, &m, &k);
            G.numVertexes = n;
            G.numEdges = m;
            // 顶点初始化
            for(int i = 0; i < G.numVertexes; i++){
                G.vexs[i] = i;
            }
            // 邻接矩阵初始化
            for(int i = 0; i < G.numVertexes; i++){
                for(int j = 0; j < G.numVertexes; j++)
                    G.arc[i][j] = INFINITYNUM;
            }
            while(m--){
                int x, y, w;
                scanf("%d %d %d", &x, &y, &w);
                if(w <= k)
                    G.arc[x - 1][y - 1] = G.arc[y - 1][x - 1] = w;
            }
            int count = DFSTraveseMatix(G);
            cout<<count<<endl;
            if(count == 1)
                printf("Yes
    ");
            else
                printf("No
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    ASP.NET Web API 框架研究 Self Host模式下的消息处理管道
    ASP.NET Web API 框架研究 Web Host模式下的消息处理管道
    ASP.NET Web API 框架研究 核心的消息处理管道
    ASP.NET Web API 框架研究 Web Host模式路由及将请求转出到消息处理管道
    ASP.NET Web API 框架研究 ASP.NET Web API 路由
    ASP.NET Web API 框架研究 ASP.NET 路由
    ASP.NET Web API 入门 (API接口、寄宿方式、HttpClient调用)
    MVVM模式
    RESTful Web API 理解
    C# 函数式编程及Monads.net库
  • 原文地址:https://www.cnblogs.com/flyingrun/p/13663213.html
Copyright © 2011-2022 走看看