gpt4 book ai didi

java - 使用迭代器降序斐波那契数

转载 作者:行者123 更新时间:2023-12-02 11:17:03 25 4
gpt4 key购买 nike

所以我的代码基本上可以工作,至少是我期望的那样。但我的问题是我的斐波那契数应该是下降而不是上升。我为此苦苦挣扎的原因是因为起始数字是用户输入,所以与升序不同,我每次都不知道第一步是什么。我考虑过将整数加载到数组中,排序并打印它们,但我觉得这违背了迭代器的目的......有什么想法吗?这是我的代码:

import java.util.Iterator;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.*;
import java.text.DateFormat;

public class Conversion1
{

public static void main(String[] args)
{

Scanner scanner = new Scanner(System.in);

int choice = 0;
int prime = 0, p = 0;
int fib = 0, f = 0;
long pTime = 0;
long fTime = 0;
long pETime = 0;
long fETime = 0;
int ff[];
long start = System.currentTimeMillis();

while (choice != 3)
{
System.out.println("Enter 1 or 2 to print out Fibonacci or Prime number iterator or enter 3 to exit.");
System.out.println("1 - Fibonacci Number Iterator");
System.out.println("2 - Prime Number Iterator");
System.out.println("3 - Exit");
choice = scanner.nextInt();
switch (choice)
{
case 1:
fTime += System.currentTimeMillis();
System.out.println("Enter the Max value");
int maxInt2 = scanner.nextInt();
ff = new int[maxInt2];
Iterator iterator = new FibonacciIterator(maxInt2);
while (iterator.hasNext())
{
ff[f] = (int) iterator.next();
f++;
}
for (int i = 0; i < f; i++)
System.out.println(ff[i]);
fETime += System.currentTimeMillis();
fib++;
break;

case 2:
pTime += System.currentTimeMillis();
System.out.println("Enter the Max value");
int maxInt = scanner.nextInt();
Iterator iterator2 = new PrimeIterator(maxInt);

while (iterator2.hasNext())
{
System.out.println(iterator2.next());
p++;
}

System.out.println("\n");
pETime += System.currentTimeMillis();
prime++;
break;

case 3:
long pFinal = pETime - pTime;
long fFinal = fETime - fTime;
double fseconds = ((fFinal / 1000) % 60);
double pseconds = ((pFinal / 1000) % 60);
System.out.println("\n");
System.out.println(fib + " Fibonacci commands yielding " + f + " individual outputs requiring "
+ fseconds + " seconds.");
System.out.println("\n");
System.out.println(prime + " Prime commands yielding " + p + " individual outputs requiring " + pseconds
+ " seconds.");
System.out.println("\n");
long end = System.currentTimeMillis();
DateFormat df = new SimpleDateFormat("HH':'mm':'ss");
System.out.println("Program started at " + df.format(new Date(start)) + " and terminated at: "
+ df.format(new Date(end)));
System.out.println("\n");
System.out.println("Program Ended");
System.out.println("\n");
break;

default:

System.out.println("Invalid Input");

}

}
}

static class PrimeIterator implements java.util.Iterator
{
private int limit = 0;
private int current;
private int a_number;

public PrimeIterator(int current)
{
this.current = current;
}

@Override
public Integer next()
{
return current;
}

static boolean isPrime(int number)
{
for (int divisor = 2; divisor < number; divisor++)
if (number % divisor == 0)
return false;

return true;

}

@Override
public boolean hasNext()
{
current--;

while (true)
{
if (isPrime(current))
break;

current--;
}

if (current <= limit)

return false;

else

return true;

}
}

static class FibonacciIterator implements java.util.Iterator
{
private int limit;
private int current = 1;// -1,1,0,1,1,2,3,5
private int prev = -1;

public FibonacciIterator(int limit)
{
this.limit = limit;
}

@Override
public Integer next()
{
return current;
}

@Override
public boolean hasNext()
{
int temp = current;
current = current + prev;// -1+1=0
prev = temp;

if (current >= limit)
return false;

else

return true;

}

@Override
public void remove()
{
throw new UnsupportedOperationException("Method not supported");
}
}
}

最佳答案

如果必须颠倒顺序,难道不能只颠倒捕获的斐波那契值的顺序吗?

                for (int i = f - 1; i >= 0; i--)
System.out.println(ff[i]);

与从索引 0 开始的位置相反。

                for (int i = 0; i < f; i++)
System.out.println(ff[i]);

关于java - 使用迭代器降序斐波那契数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50196449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com