Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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# 比较两个数组并返回一个整数数组,这些值是比较的结果_C#_Arrays - Fatal编程技术网

C# 比较两个数组并返回一个整数数组,这些值是比较的结果

C# 比较两个数组并返回一个整数数组,这些值是比较的结果,c#,arrays,C#,Arrays,编写一个方法,该方法接受两个长度相同的整数数组。方法应返回整数数组,这些值是使用以下规则比较输入数组的结果: a[i] > b[i], then 1 a[i] == b[i], then 0 a[i] < b[i], then -1 样本输入: a = [1, 3, 9] b = [-2, 6, 9] 预期产出: [1, -1, 0] 使用for循环迭代输入数组并为输出数组赋值。看起来像是家庭作业,但下面应该可以让您开始学习了 static int[] CompareArra

编写一个方法,该方法接受两个长度相同的整数数组。方法应返回整数数组,这些值是使用以下规则比较输入数组的结果:

a[i] > b[i], then 1
a[i] == b[i], then 0
a[i] < b[i], then -1
样本输入:

a = [1, 3, 9]

b = [-2, 6, 9]
预期产出:

[1, -1, 0]

使用for循环迭代输入数组并为输出数组赋值。

看起来像是家庭作业,但下面应该可以让您开始学习了

static int[] CompareArrays(int[] a, int[] b) {
  //add checks to make sure a.Length == b.Length;

  //allocate space for output array
  int[] retVal = new int[a.Length];

  //iterate through all the items in the input arrays... checking length of any one of the input arrays is just fine since we assume both should be same length
  for(int i = 0; i < a.Length; ++i) {
    retVal[i] = 0;

    //add your rules here e.g. a[i] > b[i] then 1 etc
    if(a[i] > b[i]) {
      retVal[i] = 1;
    }
  }

  return retVal;
}
static int[]comparararray(int[]a,int[]b){
//添加检查以确保a.Length==b.Length;
//为输出数组分配空间
int[]retVal=新的int[a.Length];
//遍历输入数组中的所有项…检查任何一个输入数组的长度都很好,因为我们假设两者的长度应该相同
对于(int i=0;ib[i]然后是1等
如果(a[i]>b[i]){
retVal[i]=1;
}
}
返回返回;
}

听起来像是家庭作业,请添加代码并描述您遇到的问题。您只列出了需求,但遗憾的是,这些需求不足以获得帮助。在寻求帮助之前,你一定已经尽力完成了手头的任务。投票结束,因为它在当前状态下“太宽”。尝试
Zip
方法:Zip或简单的for循环应该足以解决此问题
static int[] CompareArrays(int[] a, int[] b) {
  //add checks to make sure a.Length == b.Length;

  //allocate space for output array
  int[] retVal = new int[a.Length];

  //iterate through all the items in the input arrays... checking length of any one of the input arrays is just fine since we assume both should be same length
  for(int i = 0; i < a.Length; ++i) {
    retVal[i] = 0;

    //add your rules here e.g. a[i] > b[i] then 1 etc
    if(a[i] > b[i]) {
      retVal[i] = 1;
    }
  }

  return retVal;
}