简体   繁体   English

打印数字系列 1,n,2,n-1,3,n-2,... 系列的逻辑应该是 1 到 n(给定的输入)

[英]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 = 0i < 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:将问题分解为两个较小的问题:

Generating all values for even indices为偶数索引生成所有值

for(int i = 0; i < a.length; i += 2)
    a[i] = i + 1;

Generating all values for odd incides生成奇数事件的所有值

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

Full code完整代码

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.

相关问题 给定数字n作为输入,返回长度为n的新字符串数组,其中包含字符串“ 0”,“ 1”,“ 2”,依此类推,直到n-1 - Given a number n as input, return a new string array of length n, containing the strings “0”, “1”, “2” so on till n-1 给定n作为项数的系列的总和(递归) - Summation of a series given n as number of terms (recursion) 第(N-2)和(N-1)的第N个输出和 - Nth output sum of (N-2)th and (N-1)th 我如何编写递归方法来求和 x^n + x^(n-1) + x^(n-2)? - How do i write a recursive method to sum x^n + x^(n-1) + x^(n-2)? base-n系列生成器,用于java中的给定数字, - base-n series generator for a given number in java,, 如果输入 n 并且输出是 n 位数字,并且以 (n-1)0 开头,并且从 1 到 (n-1)9? - If enter n is input and the output is in n digits and starting with (n-1)0 with 1 to (n-1)9's? 如何在给定数字n的最大n平方上打印魔术矩阵 - how to print magic matrix upto n square for a given number n 通过检查 n 是否可整除 2^n-2 对 n 进行素性测试 - Primality test for n by checking whether n divides 2^n-2 程序查找数字n1的最小排列,但应大于另一个给定的数字n2; 如果可能,打印“无效” - Program to find smallest permutation of a number n1 but should be larger than another given number n2; print “Invalid” if not possible 打印出素数小于给定数N - Print out prime Number less than a given number N
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM