我有这段代码,据我所知,它在给定数组中搜索最大的连续数字数量,该数组和是偶数。
private static int f (int[]a, int low, int high)
{
int res = 0;
for (int i=low; i<=high; i++)
res += a[i];
return res;
}
public static int what (int []a)
{
int temp = 0;
for (int i=0; i<a.length; i++)
{
for (int j=i;
这里有一个函数,它计算数组中唯一整数对的数目,它的和是偶数。目前,我已经使用嵌套循环对其进行了编码,但是这是效率低下的,因为嵌套循环会导致O(N²)的时间复杂性。
在本例中,A表示数组,P和Q表示整数对。Q应该始终大于P,否则会产生非唯一的整数对(其中P和Q可以指向数组中的相同值)。
public int GetEvenSumCount(int[] A)
{
// result storage
int result = 0;
// loop through each array element to get P
for (int P = 0; P < A
如何得到所有奇数的和和正偶数的乘积,在得到值后,我的程序给出了错误和退出。如何解决这个问题?为什么会出现这种错误?如果我输入的值小于5,它会给出内存地址
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n,sum=0,mul=0,i,j;
cout<<"How many numbers you want to enter?";
cin>>n;
int arr[n],odd[]={0,0,0,0,0}
需要将概率和均数从数组中分离出来,并将它们放入自己的数组中。如果没有填充点,for循环将继续向evenA和oddA数组添加0。我想不出这个办法。
//Figure out length of odd and even array
for(int i = 0; i < size; i++) {
if ((arr[i] % 2) == 0) {
evens += 1;
}
else {
odds += 1;
}
}
//Create new ar
在询问用户是否要介于'‘之间或不介于’‘之间之后,我必须读取两个值之间的值。
例如,用户在文本中选择1
'Hi' My name is 'Kev'in and i'm happ'y' to be 'there'
他将会有
'Hi' 'Kev' 'y' 'there'
在文本文件中。如果他选择了2,他将会有
My name is in and i'm happ to be
现在我正在使用
Do While objScriptFile.AtEndO
给出了长度为N的字符串S,该字符串的索引范围为0到N-1,将其偶数索引字符和奇数索引字符打印为单行上两个空格分隔的字符串。
样本输入
2黑客等级
样本输出
Hce akr Rn ak
我在VScode中试过并且工作过,但是当我在HackerRank中提交它时,他们给了我这样的输出
2Hce
ak
akrRn
有一个数字(2)涉及,但我不知道如何处理它。这是密码
function processData(input) {
let strToArr = input.split('');
let evenArr = [];
let oddArr = [];
我找到了一个O(n^2)复杂度的解决方案,如下所示:
但是,有什么办法可以减少它的时间复杂性吗?
这是bin函数
int bin(int n)
{
int i = 0;
while (n > 0)
{
if (n % 2 == 1)
i++;
n = n / 2;
}
return i;
}
这是主代码
for (int j = 0; j < n; j++)
{
int ans = a[j];
如果我有一个从0到n-1的整数排列,并且我想按升序排序置换,那么不管使用何种基于交换的排序方法,排序所需掉期数的奇偶值在所有基于交换的排序方法中都是相同的吗?
例如,考虑我下面提供的基于交换的排序方法,它是用C++编写的:
(注意:pos[i]将元素'i‘的当前索引(0 )存储在列表中)
int cnt = 0; // stores the number of operations
for (int i = 0; i < n; i++) {
if (pos[i] != i) {
cnt++;
int temp = a[i];
int
我对Java很陌生,我面临着以下问题。我需要按升序排列整数数组,这样偶数将在另一半中排列,而在另一半中为奇数。
所以,我有一个特别的比较器:
static class EvenOddSort implements Comparator<Integer> {
@Override
public int compare(Integer x, Integer y) {
if (x == y) {
return 0;
}
if (y % 2 == 0) {
if (x < y