输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807

/*  输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
      输出:7 -> 0 -> 8
      原因:342 + 465 = 807
*/
public static void main(String[] args) {
    String test = test("2 -> 4 -> 3");
    String test2 = test("5 -> 6 -> 4");
    int a = Integer.parseInt(test);
    int a2 = Integer.parseInt(test2);
    int c=a+a2;
    System.out.println(c);
}
public static String test(String str){
    str=str.trim();
    String str2="";
    if(str != null && !"".equals(str)){
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)>=48 && str.charAt(i)<=57){
                str2+=str.charAt(i);
            }
        }
    }
    StringBuilder sb = new StringBuilder(str2);
    String s = sb.reverse().toString();
    return s;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C++实现Bellman-Ford算法求解带权重的有向图上单源最短路径问题的代码: ``` #include <iostream> #include <vector> #include <limits.h> using namespace std; struct Edge { int from, to, weight; }; vector<Edge> edges; vector<int> dist; vector<int> pre; int n, m; void BellmanFord(int start) { dist[start] = 0; pre[start] = -1; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m; j++) { int u = edges[j].from; int v = edges[j].to; int w = edges[j].weight; if (dist[u] != INT_MAX && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; pre[v] = u; } } } for (int i = 0; i < m; i++) { int u = edges[i].from; int v = edges[i].to; int w = edges[i].weight; if (dist[u] != INT_MAX && dist[u] + w < dist[v]) { cout << "Negative-weight cycle found in the graph" << endl; return; } } } void printPath(int cur) { if (pre[cur] == -1) { cout << cur << " "; return; } printPath(pre[cur]); cout << "-> " << cur << " "; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; edges.push_back({u, v, w}); } dist.resize(n, INT_MAX); pre.resize(n, -1); BellmanFord(0); for (int i = 1; i < n; i++) { if (dist[i] == INT_MAX) { continue; } cout << dist[i] << ": "; printPath(i); cout << endl; } return 0; } ``` 输入样例1: ``` 7 9 0 1 -1 1 2 3 0 2 4 1 3 2 3 1 1 1 4 2 4 2 2 3 4 5 4 3 -3 ``` 输出样例1: ``` -1: 0 -> 1 2: 0 -> 1 -> 4 -> 2 -2: 0 -> 1 -> 4 -> 3 1: 0 -> 1 -> 4 ``` 输入样例2: ``` 5 8 0 1 -1 1 2 3 0 2 4 1 3 2 3 1 1 1 4 1 3 4 5 4 3 -3 ``` 输出样例2: ``` Negative-weight cycle found in the graph ``` 输入样例3: ``` 8 11 0 1 1 1 2 3 0 2 4 1 3 2 2 3 5 3 4 2 4 5 8 5 6 1 5 7 6 6 7 2 7 0 1 ``` 输出样例3: ``` 1: 0 -> 1 4: 0 -> 2 3: 0 -> 1 -> 3 5: 0 -> 2 -> 3 -> 4 13: 0 -> 2 -> 3 -> 4 -> 5 -> 6 14: 0 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 16: 0 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 0 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值