在輸入第一行中,有兩個數字的第一個是行量?和第二個是一個極限?。我必須找到元素大于K的最長連續子陣列的第一個和最后一個元素的索引。
(有許多不同數字的輸入,但它們具有相同的基數。)
示例輸入是:
7 20
18
23
44
32
9
30
26
所以N是 7,K是 20,在這種情況下,有 2 個連續的子陣列是正確的:[23, 44, 32] 和 [30, 26],但我只需要更長的索引。
因此輸出是:
1 3
我已經拆分了第一行,所以我有N和K,我在陣列 H[] 中添加了剩余的行。現在我只需要找到最長的連續子陣列并獲取第一個和最后一個元素的索引。
static void Main(string[] args)
{
string[] fRow = Console.ReadLine().Split(' ');
int N = int.Parse(fRow[0]);
int K = int.Parse(fRow[1]);
int[] H = new int[N];
for (int i = 0; i < N; i )
{
H[i] = int.Parse(Console.ReadLine());
}
}
我被困在這里,如果有人能幫助我,我將不勝感激。
uj5u.com熱心網友回復:
你可以做這樣的事情(可以用 LINQ 改進很多,但我想這是某種介紹練習,所以我會堅持這個):
static void Main(string[] args)
{
string[] fRow = Console.ReadLine().Split(' ');
int N = int.Parse(fRow[0]);
int K = int.Parse(fRow[1]);
int[] H = new int[N];
int firstIndex = 0;
int lastIndex = 0;
int subarraySize = 0;
int firstIndexTemp = 0;
int lastIndexTemp = 0;
int subarraySizeTemp = 0;
bool arrayContinues = false;
for (int i = 0; i < N; i )
{
//Read the newest index
H[i] = int.Parse(Console.ReadLine());
/*If arrrayContinues is true, and the current value is higher than the threshold K,
this means this is the continuation of a subarray. For now, the current value is the last index value
*/
if (H[i] > K && arrayContinues)
{
subarraySizeTemp ;
lastIndexTemp = i;
}
/*If arrrayContinues is false, but the current value is higher than the threshold K,
this means this is the first index of a new subarray
*/
else if (H[i] > K)
{
subarraySizeTemp = 1;
firstIndexTemp = i;
arrayContinues = true;
}
/*If we reach this statement, the current value is smaller than K,
* so the array streak stopped (or was already stopped by a previous smaller value)
*/
else
{
arrayContinues = false;
}
/* We're only interested in the largest subarray,
* so let's override the previous largest array only when the current one is larger.
*/
if(subarraySizeTemp > subarraySize)
{
subarraySize = subarraySizeTemp;
firstIndex = firstIndexTemp;
lastIndex = lastIndexTemp;
}
}
/*Let's print our result!*/
Console.WriteLine($"{firstIndex} {lastIndex}");
}
uj5u.com熱心網友回復:
聽起來像家庭作業,但仍然是一個有趣的挑戰。這是一種方法。
static void Main(string[] args)
{
string[] fRow = Console.ReadLine().Split(' ');
int N = int.Parse(fRow[0]);
int K = int.Parse(fRow[1]);
int[] H = new int[N];
for (int i = 0; i < N; i )
{
H[i] = int.Parse(Console.ReadLine());
}
int greatesRangeStartIndex = -1;
int greatestRangeEndIndex = -1;
int greatestIndexSpan = 0;
for (int i = 0; i < N; i )
{
// Find the first array item that meets the criteria.
if (H[i] > K)
{
var rangeStartIndex = i;
// Continue spinning through the array while we still meet the criteria.
do
{
i ;
} while (i < N && H[i] > K);
var rangeEndIndex = i - 1;
// Determine the width of our current range and check if its our largest one.
// If the range is the biggest so far, store that as the current largest range.
var indexSpan = rangeEndIndex - rangeStartIndex 1;
if (indexSpan > greatestIndexSpan)
{
greatesRangeStartIndex = rangeStartIndex;
greatestRangeEndIndex = rangeEndIndex;
greatestIndexSpan = indexSpan;
}
}
}
// Report out the results.
// Not part of the requirements, but will remove false reporting of the criteria being in index position 1.
if (greatesRangeStartIndex == -1 && greatestRangeEndIndex == -1)
{
Console.WriteLine($"No values in the array were greater than {K}.");
}
else
{
Console.WriteLine($"{greatesRangeStartIndex} {greatestRangeEndIndex}");
}
}
uj5u.com熱心網友回復:
其他答案可行,但您可以使用像這樣更簡單的方法;
private static void Main()
{
var input = Console.ReadLine().Split(' ');
var n = int.Parse(input[0]);
var k = int.Parse(input[1]);
var startingIndex = 0;
var endingIndex = 0;
var temporaryIndex = 0;
var items = new int[n];
for (var i = 0; i < n; i )
{
var value = int.Parse(Console.ReadLine());
items[i] = value;
if (value < k)
{
temporaryIndex = i;
continue;
}
var currentSize = i - temporaryIndex;
var currentBiggestSize = endingIndex - startingIndex;
if (currentSize > currentBiggestSize)
{
startingIndex = temporaryIndex 1;
endingIndex = i;
}
}
Console.WriteLine($"Biggest Subset's Start and Ending Indexes: {startingIndex} {endingIndex}");
Console.ReadLine();
}
uj5u.com熱心網友回復:
另外一個選項:
static void Main(string[] args)
{
Example(new string[] { "7","20"},new string[] { "18", "23", "44", "32", "9", "30", "26"});
}
static void Example(string[] arr,string[] values)
{
int N = int.Parse(arr[0]);
int K = int.Parse(arr[1]);
int counter = 0;
int most_succesfull_index_yet = 0;
int most_succesfull_length_yet = 0;
for (int i = 1; i < N; i )
{
if (int.Parse(values[i]) > K)
{
counter ;
}
else
{
if (counter > most_succesfull_length_yet)
{
most_succesfull_length_yet = counter;
most_succesfull_index_yet = i - counter;
}
counter = 0;
}
}
// For last index
if (counter > most_succesfull_length_yet)
{
most_succesfull_length_yet = counter;
most_succesfull_index_yet = N - counter;
}
var bestStart = most_succesfull_index_yet;
var bestEnd = most_succesfull_index_yet most_succesfull_length_yet -1;
Console.WriteLine(bestStart "," bestEnd);
Console.ReadLine();
}
uj5u.com熱心網友回復:
另一種解決方案,保留最長匹配的索引,并在最后顯示最大長度、索引和行值。
static void Main(string[] args)
{
var data = @"7 20
18
23
44
32
9
30
26";
var rows = data.Split("\r\n");
var frow = rows[0].Split(" ");
int N = int.Parse(frow[0]);
int K = int.Parse(frow[1]);
int max = 0, currentmax = 0;
int i = 1;
int[] indexes = null;
while(i < rows.Length)
{
if (int.Parse(rows[i]) > K)
{
currentmax ;
}
else
{
if (currentmax > max)
{
max = currentmax;
indexes = new int[max];
indexes[--currentmax] = i;
do
{
indexes[--currentmax] = indexes[currentmax 1] - 1;
} while (currentmax > 0);
currentmax = 0;
}
}
i ;
}
if (indexes != null) {
Console.WriteLine($"{max} occured on indexes {string.Join(",", indexes)} with values {string.Join(",", indexes.Select(i => rows[i]).ToList())}");
}
}
uj5u.com熱心網友回復:
string[] fRow = Console.ReadLine().Split(' ');
int N = int.Parse(fRow[0]);
int K = int.Parse(fRow[1]);
bool isChain = false;
int currentFirstIndex = -1;
int maxFirstIndex = -1;
int currentLastIndex = -1;
int maxLastIndex = -1;
int currentLength = 0;
int maxLength = 0;
int[] H = new int[N];
for (int i = 0; i < N; i )
{
H[i] = int.Parse(Console.ReadLine());
if(H[i] > K)
{
if (isChain)
{
currentLastIndex = i;
}
else
{
currentFirstIndex = i;
isChain = true;
}
currentLength ;
}
else
{
if (maxLength < currentLength)
{
maxLength = currentLength;
maxFirstIndex = currentFirstIndex;
maxLastIndex = currentLastIndex;
}
currentLength = 0;
isChain = false;
}
}
Console.WriteLine("first: " maxFirstIndex " last: " maxLastIndex);
uj5u.com熱心網友回復:
這可能是其中一種方式
static void Main(string[] args)
{
var firstLineInput = Console.ReadLine().Split(" ");
var numberOfInput = Int64.Parse(firstLineInput[0]);
var value = Int64.Parse(firstLineInput[1]);
var startIndex = -1; //No value greater than value
var endIndex = -1;
var maxLength = 0;
var maxStartIndex = -1;
var maxEndIndex = -1;
for (int i = 0; i < numberOfInput ; i )
{
var input = Int64.Parse(Console.ReadLine());
if (input > value && startIndex == -1)
{
startIndex = i;
endIndex = i;
if(maxLength == 0)
{
maxLength = 1;
maxStartIndex = startIndex;
maxEndIndex = endIndex;
}
}
else if(input > value && startIndex != -1)
{
endIndex = i;
}
else if(input < value)
{
startIndex = -1;
endIndex = -1;
}
if (maxLength < (endIndex - startIndex))
{
maxLength = endIndex - startIndex;
maxStartIndex = startIndex;
maxEndIndex = endIndex;
}
}
Console.WriteLine($"{maxStartIndex} {maxEndIndex}");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/365825.html
上一篇:在BigQuery中提取嵌套的Array/STRUCTJSON字串欄位的組件
下一篇:通過滑動視窗重復Numpy陣列
