C# 在我的算法中,查找一个和除以k的数组中的所有对的缺陷在哪里?
我搞不清楚我做错了什么 代码:C# 在我的算法中,查找一个和除以k的数组中的所有对的缺陷在哪里?,c#,algorithm,linq,C#,Algorithm,Linq,我搞不清楚我做错了什么 代码: using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { string[] lineParts = Console.ReadLine().Split(' '); int k = Convert.ToInt3
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
string[] lineParts = Console.ReadLine().Split(' ');
int k = Convert.ToInt32(lineParts[1]);
int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
var indices = Enumerable.Range(0, arr.Length - 1);
int pairsDivideK = (from i in indices
from j in indices
where i < j && ((arr[i] + arr[j]) % k == 0)
select 1).ToList().Count / 2;
Console.WriteLine(pairsDivideK);
}
}
预期产出:
5
我的输出:
1
首先,你不必除以2。由于i
...
var indices = Enumerable.Range(0, arr.Length);
int pairsDivideK = (from i in indices
from j in indices
where i < j && ((arr[i] + arr[j]) % k == 0)
select 1).ToList().Count;
Console.WriteLine(pairsDivideK);
选择1.ToList.Count/2为什么要这样做?@user3185569我要除以2来计算重复次数,为什么我应该小于j?为什么不使用linesParts中的第一个值,即6?你能至少解释一下你为什么要做每一步吗?你已经用i
...
var indices = Enumerable.Range(0, arr.Length);
int pairsDivideK = (from i in indices
from j in indices
where i < j && ((arr[i] + arr[j]) % k == 0)
select 1).ToList().Count;
Console.WriteLine(pairsDivideK);