Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查列表中的数字是否仅为正数或负数<;列表<;int>&燃气轮机;有效地_C#_C++_.net_Linq - Fatal编程技术网

C# 检查列表中的数字是否仅为正数或负数<;列表<;int>&燃气轮机;有效地

C# 检查列表中的数字是否仅为正数或负数<;列表<;int>&燃气轮机;有效地,c#,c++,.net,linq,C#,C++,.net,Linq,我有一个列表数据集,具有如下字符串表示形式(给出想法!): 我想检查一下,在所有现有的数字中,是否存在一个或一组仅为正数形式的数字。我的意思是,如果在整个数据中,有3和-3,我应该返回false,否则我必须将3作为一个数字添加到另一个列表中,这个数字只显示为正3。(同样的事情只适用于否定的数字) 以下是我试图做到这一点的方式: 首先,生成一组唯一的数字并删除负片: private void GenerateCurrentExistingVariables() { _uid = new L

我有一个
列表
数据集,具有如下字符串表示形式(给出想法!):

我想检查一下,在所有现有的数字中,是否存在一个或一组仅为正数形式的数字。我的意思是,如果在整个数据中,有3和-3,我应该返回false,否则我必须将3作为一个数字添加到另一个列表中,这个数字只显示为正3。(同样的事情只适用于否定的数字)

以下是我试图做到这一点的方式:

首先,生成一组唯一的数字并删除负片:

private void GenerateCurrentExistingVariables()
{
    _uid = new List<int>();
    var all = _cnf.Data.SelectMany(list => list).ToList();
    _uid = all.Distinct().ToList(); //make list unique
    _uid.Sort(); //sort numbers
    _uid.Reverse(); //reverse so highest numbers would evalaute first!
    _uid = _uid.Where(i => i >= 0).ToList(); //remove negative numbers
}
private void GenerateCurrentExistingVariables()
{
_uid=新列表();
var all=_cnf.Data.SelectMany(list=>list.ToList();
_uid=all.Distinct().ToList();//使列表唯一
_uid.Sort();//对数字进行排序
_uid.Reverse();//反转,这样最高的数字将首先求值!
_uid=\u uid.Where(i=>i>=0.ToList();//删除负数
}
然后我会这样做:

在方法中,我调用以下代码:

    for (var i = 0; i < _uid.Count; i++)
    {
        if (ExistOnlyInNegatedForm(_uid[i]))
        {
            onlyNegatedList.Add(_uid[i]);
        }

        //perhaps continue

        if (ExistOnlyInPositiveForm(_uid[i]))
        {

            onlyPositiveList.Add(_uid[i]);
        }
    }
for(变量i=0;i<\u uid.Count;i++)
{
if(ExistOnlyInNegatedForm(_uid[i]))
{
onlyNegatedList.Add(_uid[i]);
}
//也许继续
if(ExistOnlyInPositiveForm(_uid[i]))
{
onlyPositiveList.Add(_uid[i]);
}
}
依次调用以下方法:

private bool ExistOnlyInPositiveForm(int id)
{
    for (var i = 0; i < _cnf.Data.Count; i++)
    {
        for (var j = 0; j < _cnf.Data[i].Count; j++)
        {
            if (_cnf.Data[i][j] == id)
            {
                return false;
            }
        }
    }

    return true;
}

private bool ExistOnlyInNegatedForm(int id)
{
    var toCheck = -id;
    for (var i = 0; i < _cnf.Data.Count; i++)
    {
        for (var j = 0; j < _cnf.Data[i].Count; j++)
        {
            if (_cnf.Data[i][j] == -toCheck)
            {
                return false;
            }
        }
    }

    return true;
}
private bool ExistOnlyInPositiveForm(int-id)
{
对于(变量i=0;i<\u cnf.Data.Count;i++)
{
对于(var j=0;j<_cnf.Data[i].计数;j++)
{
如果(_cnf.Data[i][j]==id)
{
返回false;
}
}
}
返回true;
}
私有bool ExistOnlyInNegatedForm(int-id)
{
var-toCheck=-id;
对于(变量i=0;i<\u cnf.Data.Count;i++)
{
对于(var j=0;j<_cnf.Data[i].计数;j++)
{
如果(_cnf.Data[i][j]==-toCheck)
{
返回false;
}
}
}
返回true;
}
对于这个简单的任务来说,这是太多的代码了,我觉得当数据越来越大时,这会变得越来越慢……请让我知道如何改进这一点。另外,我希望至少为了减少代码行数,使用LINQ来完成这项工作


<>我很想看到一个C++解决方案,所以我在我的问题中标注C++(不做语言垃圾!)

为什么不写一个简单的方法来获取一个int的列表,一个布尔值,如果你想要正反两个,它只会返回真的假? 通过这种方式,您可以迭代外部列表,为其中的每个元素调用它,如果您得到了想要的,请从那里继续:

public int AllSameSign(IEnumerable<int> theInputList, bool positive = true)
{
     if (positive)
         return theInputList.All(i=>i >= 0);
     else
         return theInputList.All(i=>i < 0);
}
public int-AllSameSign(输入列表的IEnumerable,bool-positive=true)
{
如果(正)
返回输入列表.All(i=>i>=0);
其他的
返回输入列表.All(i=>i<0);
}
(这是我的想法,您可以将bool更改为一个简单的枚举,以使其更具可读性…

对于正数:

var uids = _uid.SelectMany(q => q).ToArray();

var positive = uids.Where(p => p >= 0).Distinct()
    .Except(uids.Where(p => p < 0).Select(p => -p))
    .ToList();
return positive.Any();
var negative = uids.Where(p => p < 0).Distinct()
    .Except(uids.Where(p => p >= 0).Select(p => -p))
    .ToList();
return negative.Any();
var uids=\u uid.SelectMany(q=>q.ToArray();
var positive=uids.Where(p=>p>=0).Distinct()
。除了(uids.Where(p=>p<0)。选择(p=>-p))
.ToList();
返回正数。Any();
对于负数:

var uids = _uid.SelectMany(q => q).ToArray();

var positive = uids.Where(p => p >= 0).Distinct()
    .Except(uids.Where(p => p < 0).Select(p => -p))
    .ToList();
return positive.Any();
var negative = uids.Where(p => p < 0).Distinct()
    .Except(uids.Where(p => p >= 0).Select(p => -p))
    .ToList();
return negative.Any();
var negative=uids.Where(p=>p<0).Distinct()
.Except(uids.Where(p=>p>=0)。选择(p=>-p))
.ToList();
返回负数。Any();

只需通过以下方式创建您自己的IEquality比较器:

 class OnlyPositiveInt : IEqualityComparer<int>
    {
        public bool Equals(List<int> some)
        {
            if ( some.First() > 0 && some.Last() > 0)   { return true; }
            return false;
        }
        public int GetHashCode(int x)
        {
            return x.GetHashCode();
        }
    }
 List<List<int>> onlyPositive = new List<List<int>>(generatedList.Distinct(OnlyPositiveInt));
仅类positiveint:IEqualityComparer
{
公共布尔等于(列出一些)
{
如果(some.First()>0&&some.Last()>0){return true;}
返回false;
}
公共int GetHashCode(int x)
{
返回x.GetHashCode();
}
}
List onlyPositive=新列表(generatedList.Distinct(OnlyPositiveInt));

<>代码> 因为你提到你可能想要C++答案,所以我写了一个。它使用哈希表存储整数绝对值的键值对,以及存储该特定绝对值历史记录的对象

历史跟踪对象定义如下:

int sign(int a){return ((a > 0) - (a < 0));}
int absolute(int a){return a * sign(a);}

struct SignRecord{
    SignRecord() : value_{NEITHER}{}
    enum SignGrouping{
        NEITHER, NEGATIVE, POSITIVE, BOTH
    } value_;

    SignGrouping& modify(int modifier){
        auto intToGrouping = [](int a)->SignGrouping {return a < 0 ? NEGATIVE : POSITIVE;};
        if(value_ == NEITHER){value_ = intToGrouping(modifier);}
        else if(value_ == BOTH){return value_;}
        else if(value_ == intToGrouping(modifier)){return value_;}
        else{
            return value_ = BOTH;
        }
        return value_;
    }
};

coliru演示位于。请注意,我对给定的输入列表做了一些修改,因为每个输入整数似乎都用列表列表中的两个符号表示。

我一直在使用这个想法,到目前为止,它似乎有效。当然,我不得不稍微修改一下你的数据集,因为事实上,没有一个数字通过了测试,要么是正的,要么是负的

下面是:

  //Setting up the mock data
  List<List<int>> data = new List<List<int>>();
  data.Add(new List<int>(new[] { 1, 3 }));
  data.Add(new List<int>(new[] { -1, -3 }));
  data.Add(new List<int>(new[] { 2, 5 }));
  data.Add(new List<int>(new[] { -2, -5 }));
  data.Add(new List<int>(new[] { -3, 4 }));
  data.Add(new List<int>(new[] { -5, 4 }));
  data.Add(new List<int>(new[] { 3, 5, -4 }));
  data.Add(new List<int>(new[] { 6, -8 }));
  data.Add(new List<int>(new[] { 7, -8 }));
  data.Add(new List<int>(new[] { -6, -7, 8 }));
  data.Add(new List<int>(new[] { 7, 9 }));
  data.Add(new List<int>(new[] { -7, -9 }));
  data.Add(new List<int>(new[] { 3, 8, -10 }));
  data.Add(new List<int>(new[] { -3, -8, -10 }));
  data.Add(new List<int>(new[] { -3, 8, 10 }));
  data.Add(new List<int>(new[] { 3, -8, 10 }));
  data.Add(new List<int>(new[] { 4, 9, -11 }));
  data.Add(new List<int>(new[] { -4, -9, -11 }));
  data.Add(new List<int>(new[] { -4, 9, 11 }));
  data.Add(new List<int>(new[] { 4, -9, 11 }));
  data.Add(new List<int>(new[] { 10, 11 }));
  data.Add(new List<int>(new[] { -1, 6 }));
  data.Add(new List<int>(new[] { 1, -6 }));
  data.Add(new List<int>(new[] { -2, 7 }));
  data.Add(new List<int>(new[] { 2, 39 }));

  //Actual solution code
  var all = data.SelectMany(l => l); //flatten
  var grouped = all.GroupBy(g => Math.Abs(g)); //Group

  //Look for groups where the Sum is equal Key * Count, 
  //which means only either all positives, or all negatives
  var good = grouped.Where(i => i.Key * i.Count() == Math.Abs(i.Sum())); 

  var result = good.Select(g => g.First()); //Get rid of groups
  var allPos = result.Where(i => i > 0); //Self explanatory
  var allNeg = result.Where(i => i < 0);
//设置模拟数据
列表数据=新列表();
添加(新列表(新[]{1,3}));
添加(新列表(新[]{-1,-3}));
添加(新列表(新[]{2,5}));
添加(新列表(新[]{-2,-5}));
添加(新列表(新[]{-3,4}));
添加(新列表(新[]{-5,4}));
添加(新列表(新[{3,5,-4}));
添加(新列表(新[]{6,-8}));
添加(新列表(新[]{7,-8}));
添加(新列表(新[]{-6,-7,8}));
添加(新列表(新[]{7,9}));
添加(新列表(新[]{-7,-9}));
添加(新列表(新[{3,8,-10}));
添加(新列表(新[]{-3,-8,-10}));
添加(新列表(新[]{-3,8,10}));
添加(新列表(新[{3,-8,10}));
添加(新列表(新[{4,9,-11}));
添加(新列表(新[]{-4,-9,-11}));
添加(新列表(新[]{-4,9,11}));
添加(新列表(新[{4,-9,11}));
添加(新名单(新[{10,11}));
添加(新列表(新[]{-1,6}));
添加(新列表(新[]{1,-6}));
添加(新列表(新[]{-2,7}));
添加(新列表(新[]{2,39}));
//实际解决方案代码
var all=data.SelectMany(l=>l)//压平
var group=all.GroupBy(g=>Math.Abs(g))//团体
//查找总和等于Key*Count的组,
//这意味着要么全部是正面的,要么全部是负面的
var good=grouped.Where(i=>i.Key*i。