天天写算法之Can you find it?

#include<cstdio>  
#include<algorithm>  
using namespace std;  
int a[505],b[505],c[505],f[250010];  
  
int binary(int x,int M,int k)  
{  
    for(int i=0; i<M; i++)  
    {  
        int l = 0, r = k;  
        while(l <= r)  
        {  
            int mid = (l+r)/2;  
            if(x > c[i]+f[mid]) l = mid+1;  
            else if(x < c[i]+f[mid]) r = mid-1;  
            else return 1;  
        }  
    }  
    return 0;  
}  
  
int main()  
{  
    int L,N,M,i,j,s,t=1;  
  
    while(~scanf("%d%d%d", &L,&N,&M))  
    {  
        for(i=0; i<L; i++)  
            scanf("%d", a+i);  
        for(i=0; i<N; i++)  
            scanf("%d", b+i);  
        for(i=0; i<M; i++)  
            scanf("%d", c+i);  
        int k = 0;  
        for(i=0; i<L; i++)  
            for(j=0; j<N; j++)  
                f[k++] = a[i]+b[j];  
        sort(f,f+k);  
        scanf("%d", &s);  
        printf("Case %d:\n", t++);  
        while(s--)  
        {  
            int x;  
            scanf("%d", &x);  
            printf(binary(x,M,k-1) ? "YES\n" : "NO\n");  
        }  
    }  
    return 0;  
}  
这个题的思路是:先对前两个相加,排序,然后再逐个与第三个相加,看看匹不匹配(二分法)。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dijkstra's Algorithm (DWA) is a popular algorithm used to find the shortest path between nodes in a graph. Although it is commonly implemented in programming languages like Python and C++, it is also possible to implement it in MATLAB. Here is an example of how you can implement Dijkstra's Algorithm in MATLAB: ```matlab function shortestPath = dijkstra(graph, startNode, endNode) numNodes = size(graph, 1); unvisitedNodes = 1:numNodes; shortestDistances = inf(1, numNodes); previousNodes = zeros(1, numNodes); shortestDistances(startNode) = 0; while ~isempty(unvisitedNodes) [~, currentNode] = min(shortestDistances(unvisitedNodes)); if currentNode == endNode break; end unvisitedNodes(unvisitedNodes == currentNode) = []; neighbors = find(graph(currentNode, :)); for i = 1:length(neighbors) neighbor = neighbors(i); distance = graph(currentNode, neighbor); if shortestDistances(currentNode) + distance < shortestDistances(neighbor) shortestDistances(neighbor) = shortestDistances(currentNode) + distance; previousNodes(neighbor) = currentNode; end end end % Reconstruct the shortest path currentNode = endNode; path = currentNode; while currentNode ~= startNode currentNode = previousNodes(currentNode); path = [currentNode path]; end shortestPath = path; end ``` In this implementation, the `graph` is a matrix representing the weighted directed graph. Each element of the matrix represents the weight of the edge between two nodes, or `inf` if there is no edge. The `startNode` and `endNode` are the indices of the start and end nodes in the graph, respectively. To use the algorithm, you can call the `dijkstra` function with your graph, start node, and end node as inputs. It will return the shortest path as an array of node indices. Please note that this is just a basic implementation of Dijkstra's Algorithm in MATLAB. Depending on your specific requirements and graph representation, you may need to modify it accordingly.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值