[英]logic to print a number series 1,n,2,n-1,3,n-2,... series should be 1 to n(the input given)
I'm trying my hands on basic programming and I came across this question.我正在尝试基本编程,但遇到了这个问题。 I have a function with a return type as string, that takes an integer input and has to print the series mentioned.我有一个返回类型为字符串的函数,它接受一个整数输入并且必须打印提到的系列。 Here's what I did.这就是我所做的。
String s=new String("h");
int[] a=new int[n];
int k=1;
for(int i=0;i<n;i+=2)
{
a[i]=b;//line6
a[i+1]=n-(b-1);//line7
b++;
}
s=Arrays.toString(a);
return s;
When I enter an "even" no.当我输入“偶数”时。 like 4. I get the proper result [1,4,2,3].像 4. 我得到了正确的结果 [1,4,2,3]。
But when I enter "odd" no.但是当我输入“奇数”时没有。 like 5. I get an ArrayOutOfBoundException像 5. 我得到一个 ArrayOutOfBoundException
I Know where Im going wrong at line6 and line7 but I'm not getting an idea how to modify it accordingly.我知道我在第 6 行和第 7 行哪里出错了,但我不知道如何相应地修改它。
I also wish to return the string as 1 n 2 n-1 3 n-2 ... instead of [1,n,2,n-1,3,n-2,..]我还希望将字符串返回为 1 n 2 n-1 3 n-2 ... 而不是 [1,n,2,n-1,3,n-2,..]
That's because you have a loop running from i = 0
to i < n
, and you are trying to access a[i + 1]
.那是因为您有一个从i = 0
到i < n
运行的循环,并且您正在尝试访问a[i + 1]
。 This runs fine on even numbers because you're incrementing 2
each time, and the last iteration checks for a[n - 2]
and a[n - 1]
.这在偶数上运行良好,因为您每次都增加2
,最后一次迭代检查a[n - 2]
和a[n - 1]
。
The ArrayIndexOutOfBoundException
occurs on odd numbers, however, because the last iteration attempts to access a[n - 1]
and a[n]
.然而, ArrayIndexOutOfBoundException
发生在奇数上,因为最后一次迭代尝试访问a[n - 1]
和a[n]
。
One way to modify the loop would be to increment only by 1
, and set the value of a[i]
by checking the parity of i
inside the loop:修改循环的一种方法是仅增加1
,并通过检查循环内i
的奇偶校验来设置a[i]
的值:
for(int i = 0; i < n; i++, b++) {
a[i] = (i % 2 == 0)? b: (n - (b - 1));
}
consider the next approach:考虑下一个方法:
for(int i=0;i<n/2;i++)
{
a[2*i] = i+1;
a[2*i+1] = n-i;
}
if (n&1==1) //how to check for oddity in Java?
a[n-1] = (n+1)/2
The inside of your loop should look like你的循环内部应该看起来像
a[i] = b;
if (i + 1 < n) { // check the bounds before writing at i+1
a[i + 1] = n - (b - 1);
}
b++;
The reason for that is that when having odd numbers (eg 5) i
gets to become 4 in the last iteration of the loop, 4 is smaller than 5, therefore the code enters the loop, then you access a
at index 4
, which is okay, but then you try to access it at 4+1
, which is 5
, but the array does not have an index 5 because.其原因是,具有奇数时(例如,5) i
得到成为4在循环的最后一次迭代,4是小于5,因此该代码进入循环,然后访问a
在索引4
,这是好的,但是随后您尝试在4+1
访问它,即5
,但该数组没有索引 5 因为。
Split the problem up into two smaller problems:将问题分解为两个较小的问题:
for(int i = 0; i < a.length; i += 2)
a[i] = i + 1;
for(int i = 1; i < a.length; i += 2)
a[i] = n - i / 2;
Thanks to integer-division i / 2
of an odd number can substitute (i - 1) / 2
.由于奇数的整数除法i / 2
可以替代(i - 1) / 2
。
int[] a = new int[n];
for(int i = 0; i < a.length; i += 2)
a[i] = i + 1;
for(int i = 1; i < a.length; i += 2)
a[i] = n - i / 2;
return Arrays.toString(a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.