C# 给定一个整数数组,如何找到最大数的所有公共倍数?
这是我在这个网站上的第一个问题。我正在练习Hackerrank上的一个问题,该问题要求在“两组数据之间”查找数字。给定两个整数数组,我必须找到符合以下两个条件的数: 1) 第一个数组中的元素都必须是数字的因子 2) 数字必须包含在第二个数组的所有元素中 我知道我需要找到第一个数组中每个元素的所有公共倍数,但这些倍数必须小于或等于第二个数组的最小值。我首先对第一个数组进行排序,然后找到第一个数组中最大数字的所有倍数(同样,最大值为第二个数组的最小值),并将这些倍数存储在一个列表中。然后,我转到第一个数组中的第二大元素,并根据现有的倍数数组进行测试。删除现有倍数列表中的所有元素,这些元素不是第一个数组第二大元素的倍数。然后我测试第一个数组的第三大值,一直到最小值。当我以降序遍历第一个数组时,应该修剪现有倍数的列表。我已经编写了一个解决方案,它只通过了站点上9个测试用例中的5个,请参见下面的代码。我的任务是编辑getTotalX函数,我自己作为助手创建了getCommonMultiples函数。我没有创建或编辑主函数。我不知道为什么我没有通过其他4个测试用例,因为我看不到任何测试用例是什么C# 给定一个整数数组,如何找到最大数的所有公共倍数?,c#,arrays,C#,Arrays,这是我在这个网站上的第一个问题。我正在练习Hackerrank上的一个问题,该问题要求在“两组数据之间”查找数字。给定两个整数数组,我必须找到符合以下两个条件的数: 1) 第一个数组中的元素都必须是数字的因子 2) 数字必须包含在第二个数组的所有元素中 我知道我需要找到第一个数组中每个元素的所有公共倍数,但这些倍数必须小于或等于第二个数组的最小值。我首先对第一个数组进行排序,然后找到第一个数组中最大数字的所有倍数(同样,最大值为第二个数组的最小值),并将这些倍数存储在一个列表中。然后,我转到第一
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
/*
* Complete the getTotalX function below.
*/
static int getTotalX(int[] a, int[] b) {
//get minimum value of second array
int b_min = b.Min();
//create List to hold multiples
List<int> multiples = getCommonMultiples(a, b_min);
//create List to hold number of ints which are in solution
List<int> solutions = new List<int>();
foreach(int x in multiples)
{
foreach(int y in b)
{
if (y % x == 0 && !solutions.Contains(x))
{
solutions.Add(x);
}
else
{
break;
}
}
}
return solutions.Count;
}
static List<int> getCommonMultiples(int[] array, int max)
{
//make sure array is sorted
Array.Sort(array);
int x = array.Length - 1; //x will be the last # in array -- the max
int y = 1;
//find all multiples of largest number first and store in a list
int z = array[x] * y;
List<int> commonMultiples = new List<int>();
while(z <= max)
{
commonMultiples.Add(z);
y++;
z = array[x] * y;
}
//all multiples of largest number are now added to the list
//go through the smaller numbers in query array
//only keep elements in list if they are also multiples of smaller
//numbers
int xx = array.Length - 2;
for(int a = array[xx]; xx >= 0; xx--)
{
foreach(int b in commonMultiples.ToList())
{
if (b % a != 0)
{
commonMultiples.Remove(b);
}
else
{
continue;
}
}
}
return commonMultiples;
}
static void Main(string[] args) {
TextWriter tw = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] nm = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(nm[0]);
int m = Convert.ToInt32(nm[1]);
int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), aTemp => Convert.ToInt32(aTemp))
;
int[] b = Array.ConvertAll(Console.ReadLine().Split(' '), bTemp => Convert.ToInt32(bTemp))
;
int total = getTotalX(a, b);
tw.WriteLine(total);
tw.Flush();
tw.Close();
}
}
样本输出
三,
说明:2和4平均分为4、8、12和16。4、8和16等分为16、32、96。
4、8和16是仅有的三个数字,第一个数组中的每个元素都是系数,第二个数组中的每个元素都是系数。我发现您发布的代码有两个问题 首先,正如所指出的,
a=array[xx]
不会在for循环中每次更新。由于变量a
仅在一个位置使用,因此我建议将其替换为array[xx]
,并按如下方式使用:
for(int xx = array.Length - 2; xx >= 0; xx--)
{
foreach(int b in commonMultiples.ToList())
{
if (b % array[xx] != 0)
{
commonMultiples.Remove(b);
对于For
循环的理解:要在每次编写For
循环时正确地增加a
,如下所示:
for(int xx = array.Length - 2, a = array[xx]; xx >= 0; xx--, a = array[xx])
循环的的第一部分(直到;)是初始化阶段,仅在第一次进入循环之前调用。第二部分是每次通过循环(包括第一个)之前检查的while条件,如果在任何时候它的计算结果为false,则循环被中断(停止)。第三部分是仅在每次成功循环后调用的增量阶段
因此,为了使a
在for
循环头中保持最新,它必须出现两次
其次,getTotalX
中的解决方案
是加法的,这意味着数组b
中每个值的每个倍数都作为解决方案添加,即使它不适合b
中的其他值。为了让它按您想要的方式工作,我们必须使用Remove
循环,而不是Add
循环
List<int> multiples = getCommonMultiples(a, b_min);
//create List to hold number of ints which are in solution
List<int> solutions = multiples.ToList();
foreach(int x in multiples)
{
foreach(int y in b)
{
if (y % x != 0)
{
solutions.Remove(x);
break;
}
}
}
这些输入数字有多大?你能在for循环中给出一个典型的示例(编辑到你的问题中)吗?第一部分执行一次:In'for(int a=array[xx]········································value@HansKesting不是每次xx递减时都会更新吗?这很有效!感谢您提出从现有列表中减去解的想法,感谢@Hans Kesting指出我在数组初始化中的错误。
List<int> multiples = getCommonMultiples(a, b_min);
//create List to hold number of ints which are in solution
List<int> solutions = multiples.ToList();
foreach(int x in multiples)
{
foreach(int y in b)
{
if (y % x != 0)
{
solutions.Remove(x);
break;
}
}
}
//create List to hold number of ints which are in solution
List<int> solutions = multiples.Where((x) => b.All((y) => y % x == 0)).ToList();