添加数组中的数字序列并打印第 n 个数字
Add sequence of number in array and print the nth number
我正在做一个 Java 练习,它将打印出序列号中的第 n 个数字。我刚刚完成了数组中的正常序列号,如 1,2,3,4,5,6,7,8,9,10,...所以如果 n=20,它会为这个序列打印出 20数.
现在,我想打印数字序列中的第 n 个数字,如下所示:
Start with a(0) = 0
The next index is #1 (odd), so add 0 + 1 = 1
The next index is #2 (even), so multiply 1 x 2 = 2
The next index is #3 (odd), so add 2 + 3 = 5
The next index is #4 (even), so multiply 5 x 4 = 20
The next index is #5 (odd), so add 20 + 5 = 25
基本上,如果索引是奇数,您 将 添加到前一项。
如果索引是偶数,则您 乘以 前一项。
模式如下:
0, 1, 2, 5, 20, 25, 150, 157, 1256, 1265, 12650, 12661, 151932, 151945, 2127230, 2127245, 34035920, 34035937, 61264686612=等等]...[=
问题是,我不知道如何存储这些类型的序列号以便打印第 n 个数字。我一直坚持到:
if ( number1 % 2 == 0)
{
number1 = number1 * (1-number1);
}
else
{
number1 = number1 + (1-number1);
}
提前致谢。
我认为您只是缺少一些用于存储上一次迭代的状态的逻辑:
int previous = 0;
int number = 0;
for (int i=1; i < 20; ++i) {
System.out.print(number + " ");
if (i % 2 != 0) {
number = previous + i;
previous = number;
}
else {
number = previous * i;
previous = number;
}
}
输出:
0, 1, 2, 5, 20, 25, 150, 157, 1256, 1265, 12650, 12661, 151932, 151945, ...
假设 number1
是你的索引变量,试试下面的代码片段,
if ( number1 % 2 == 0){
result= number1 * (result);
}
else
{
result= number1 + (result);
}
number1++;
if(number1>=n){
break;
}
基本上你需要迭代直到达到 n
并继续将每次迭代的 结果 存储在一个名为 result
.[=14 的单独变量中=]
只需将它们存储在一个数组中,获取第 n 个索引。
long[] arr = new long[20];
for(int i = 1 ; i < arr.length ; i ++){
if ( i % 2 == 0)
{
arr[i] = i * arr[i - 1];
}
else
{
arr[i] = i + arr[i - 1];
}
}
试试这个
public static void main(String[] args) {
long res = 0;
for (int i = 0; i < 20; i++) {
if (i % 2 == 0)
res = res * i;
else
res = i + res;
System.out.println(res);
}
}
输出
0 1 2 5 20 25 150 157 1256 1265 12650 12661 151932 151945 2127230
import java.lang.Math;
public class Test
{
static long number = 0, previous = 0, limit = 100;
public static void main(String[] args)
{
for (int i=1; i < limit; ++i)
{
System.out.print(number + " ");
if (i % 2 != 0)
{
number = previous + i;
previous = number;
}
else
{
number = previous * i;
previous = number;
}
}
}
}
我正在做一个 Java 练习,它将打印出序列号中的第 n 个数字。我刚刚完成了数组中的正常序列号,如 1,2,3,4,5,6,7,8,9,10,...所以如果 n=20,它会为这个序列打印出 20数.
现在,我想打印数字序列中的第 n 个数字,如下所示:
Start with a(0) = 0
The next index is #1 (odd), so add 0 + 1 = 1
The next index is #2 (even), so multiply 1 x 2 = 2
The next index is #3 (odd), so add 2 + 3 = 5
The next index is #4 (even), so multiply 5 x 4 = 20
The next index is #5 (odd), so add 20 + 5 = 25
基本上,如果索引是奇数,您 将 添加到前一项。 如果索引是偶数,则您 乘以 前一项。
模式如下: 0, 1, 2, 5, 20, 25, 150, 157, 1256, 1265, 12650, 12661, 151932, 151945, 2127230, 2127245, 34035920, 34035937, 61264686612=等等]...[=
问题是,我不知道如何存储这些类型的序列号以便打印第 n 个数字。我一直坚持到:
if ( number1 % 2 == 0)
{
number1 = number1 * (1-number1);
}
else
{
number1 = number1 + (1-number1);
}
提前致谢。
我认为您只是缺少一些用于存储上一次迭代的状态的逻辑:
int previous = 0;
int number = 0;
for (int i=1; i < 20; ++i) {
System.out.print(number + " ");
if (i % 2 != 0) {
number = previous + i;
previous = number;
}
else {
number = previous * i;
previous = number;
}
}
输出:
0, 1, 2, 5, 20, 25, 150, 157, 1256, 1265, 12650, 12661, 151932, 151945, ...
假设 number1
是你的索引变量,试试下面的代码片段,
if ( number1 % 2 == 0){
result= number1 * (result);
}
else
{
result= number1 + (result);
}
number1++;
if(number1>=n){
break;
}
基本上你需要迭代直到达到 n
并继续将每次迭代的 结果 存储在一个名为 result
.[=14 的单独变量中=]
只需将它们存储在一个数组中,获取第 n 个索引。
long[] arr = new long[20];
for(int i = 1 ; i < arr.length ; i ++){
if ( i % 2 == 0)
{
arr[i] = i * arr[i - 1];
}
else
{
arr[i] = i + arr[i - 1];
}
}
试试这个
public static void main(String[] args) {
long res = 0;
for (int i = 0; i < 20; i++) {
if (i % 2 == 0)
res = res * i;
else
res = i + res;
System.out.println(res);
}
}
输出
0 1 2 5 20 25 150 157 1256 1265 12650 12661 151932 151945 2127230
import java.lang.Math;
public class Test
{
static long number = 0, previous = 0, limit = 100;
public static void main(String[] args)
{
for (int i=1; i < limit; ++i)
{
System.out.print(number + " ");
if (i % 2 != 0)
{
number = previous + i;
previous = number;
}
else
{
number = previous * i;
previous = number;
}
}
}
}