小编Lrr*_*rrr的帖子

在linux中使用c ++检测连接接口

我想检测现在连接的接口; 我有三个3g-modem连接和两个lan连接到服务器(我的设备不支持wlan).我的意思是连接类型并不重要,但接口名称和细节对我来说非常重要.我想检测我用c ++编写的程序中的连接接口.请告诉我如何在c ++中检测连接的接口?(root权限不是问题.)

c++ linux networking routing tcp

3
推荐指数
2
解决办法
3474
查看次数

clang能否在编译时警告未定义的行为?

#include <iostream>

int main()
{
    int n = -1000;
    for (int i(0); i != n; ++i)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

在gcc中,在编译时捕获以下错误:

main.cpp:6:5: warning: iteration 2147483647u invokes undefined behavior [-Waggressive-loop-optimizations]

     for (int i(0); i != n; ++i)
Run Code Online (Sandbox Code Playgroud)

Clang -fsanitize=undefined是一个运行时机制.clang的编译时间相当于什么?

c++ clang undefined-behavior

3
推荐指数
1
解决办法
1412
查看次数

如何修复java.lang.arrayindexoutofboundsexception:0?

我是新手java.任何人都可以帮我解决错误arrayindexoutofboundsexception.

public class Minesweeper { 
   public static void main(String[] args) { 

      int M = Integer.parseInt(args[0]);
      int N = Integer.parseInt(args[1]);
      double p = Double.parseDouble(args[2]);

      // game grid is [1..M][1..N], border is used to handle boundary cases
      boolean[][] bombs = new boolean[M+2][N+2];
      for (int i = 1; i <= M; i++)
         for (int j = 1; j <= N; j++)
            bombs[i][j] = (Math.random() < p);

      // print game
      for (int i = 1; i <= M; i++) { …
Run Code Online (Sandbox Code Playgroud)

java

3
推荐指数
2
解决办法
3万
查看次数

为什么 powerset 给出了 2^N 的时间复杂度?

下面是生成powerset的递归函数

void powerset(int[] items, int s, Stack<Integer> res) {
     System.out.println(res);

     for(int i = s; i < items.length; i++) {
          res.push(items[i]);
          powerset(items, s+1, res);
          res.pop();
     }
}
Run Code Online (Sandbox Code Playgroud)

我真的不明白为什么这需要O(2^N). 这是2从哪里来的?为什么T(N) = T(N-1) + T(N-2) + T(N-3) + .... + T(1) + T(0)解决为O(2^n). 有人可以解释为什么吗?

math recursion recurrence discrete-mathematics

2
推荐指数
2
解决办法
7324
查看次数

不明白<T>和<?>的内容

例如,我有一堂课

class Element {}
Run Code Online (Sandbox Code Playgroud)

我有一些课程正在扩展这门课程

class ExtraElement extends Element {}
Run Code Online (Sandbox Code Playgroud)

那么这个陈述是一样的吗?

List<Element> list & List<? extends Element>
Run Code Online (Sandbox Code Playgroud)

因为据我所知wildcards定义了一类的类.在这两个语句中,我可以传递任何扩展类的元素,Element所以基本上是Element类的一部分.

java generics

2
推荐指数
1
解决办法
69
查看次数

将学生分为两组的图算法

我正在努力创建一种有效的算法,可以确定一组学生是否可以分为两组.

请注意,(X,Y)学生X必须与学生一起提供Y一些类型的约束,以及(A,B)学生A不能与学生一起使用的类型约束B.并非每个学生都对他有约束,有些学生有多种限制.

我认为是一个图形,其中每个学生都是一个节点,然后两个节点通过边连接,如果学生可以在同一个组(例如,它们要么有,他们必须要装配在一起和/或不具有约束约束他们不能在一起).但是,一旦我构造了这个图形表示,我不确定我可以应用什么算法来解决(或证明,给定一组约束这是不可能的).

任何建议表示赞赏?谢谢!

algorithm graph breadth-first-search depth

2
推荐指数
1
解决办法
1476
查看次数

Number of Subarray whose sum greater than given value

Given an array of N integers (positive and negative), find the number of contiguous sub array whose sum is greater or equal to K (also, positive or negative)

I have managed to work out a naive O(N2) solution, is it possible to get better?

algorithm data-structures

2
推荐指数
1
解决办法
2293
查看次数

找到给定元素数组的GCD

我遇到了一个面试问题,以优化的方式找到整数元素数组的gdc(最大公约数):

Sample case :
   a[] = { 10,20,19,15,13}

Result = 1

sample case : 
  a[]={9,21,12,15,27}
Result : 3
Run Code Online (Sandbox Code Playgroud)

我在面试时提交了以下结果.但他要求优化相同的.我建议的解决方案:

package threeDpalm;

public class GCF
{
    public static void main(String ag [])
    {
        int[] input = {10, 20, 3, 30, 90, 40};
        boolean flag = true;
        int min_araay = Integer.MAX_VALUE;
        int count = 0;
        while (flag)
        {
            for (int j : input)
            {

                if (j < min_araay && j != 0)
                {
                    min_araay = j;
                }
            }
            for (int k = …
Run Code Online (Sandbox Code Playgroud)

java algorithm

2
推荐指数
1
解决办法
1万
查看次数

用Java计算gcd

我正在自学Java,所以我正在从加州大学伯克利分校CS 61B做实验.我正在尝试编写一个gcd方法来使我的toString方法工作.

toString方法以非简化形式打印Fraction.检查toString方法中的代码.它正在调用另一个名为gcd的方法,它计算两个正整数的最大公约数(GCD).如果此方法正常工作,toString将以缩小的形式打印分数.我必须重写gcd的主体,以便它是一个正确计算GCD的递归函数.

这是我的toString方法:

public String toString() {
    int thisGcd = gcd(numerator, denominator);

    return (numerator / thisGcd + "/" + denominator / thisGcd);
  }
Run Code Online (Sandbox Code Playgroud)

我的目标是编写正确的gcd函数,以便toString以不可缩减的形式返回一个fracyion.这是我写的:

private static int gcd(int x, int y) {
    int div;
    if(x<0 || y<0){
        return -1;
    }
    else {

        if(x>y){
            div = y ;
        }
        else{
            div = x;
        }
        while( div !=0){
            if( (x % div==0 )&&(y % div == 0) ) {
                return div;

            }
            div --;

        }
    }

}
Run Code Online (Sandbox Code Playgroud)

说明是关于使用以下伪代码编写递归gcd函数,但我不确定如何完全实现它:

function …
Run Code Online (Sandbox Code Playgroud)

java algorithm recursion greatest-common-divisor

2
推荐指数
1
解决办法
1298
查看次数

O(nlogn)+ O(n),O(nlogn)和O(nlogn + n)之间有什么关系?

凭直觉,我认为这三个表达式是等效的。

例如,如果一种算法在O(nlogn) + O(n)或中运行O(nlogn + n)(我很困惑),我可以说这是一种O(nlogn)算法吗?

真相是什么

algorithm big-o

2
推荐指数
1
解决办法
739
查看次数