递减通过数组创建的 Carousel。我不明白哪里出了问题
Decrement Carousel created through array. I don't understand what went wrong
我不得不制作一个递减轮播 [1]:https://i.stack.imgur.com/T7E4d.png。虽然它有点管用,但我需要它 return 正确的结果,这样我才能通过测试。代码:
int position = 0;
int capacity = 3;
public int next() {
if (allNegative(arr)) return -1;
if (position >= capacity) position = 0;
if (arr[position] <= 0) {
if (position == arr.length - 1) {
position = 0;
if (arr[position] == 0) {
return arr[++position]--;
}
else {return arr[position++]--;}
}
else {
return arr[++position]--;
}
}
return arr[position++]--;
}
所以它将元素放入数组中,next() 方法应该 return 当前值并递减它,之后它移动到下一个元素。它应该跳过零元素。当它没有什么可减少时,它 returns -1。我一直卡在这里,我不明白什么不起作用。
一个程序的完整代码:
public class DecrementingCarousel {
static int capacity;
int counter = 0;
//static List <Integer> elements = new ArrayList<>();
static int [] arr ;
public DecrementingCarousel(int capacity) {
DecrementingCarousel.capacity = capacity;
arr = new int[capacity];
}
public boolean addElement(int element){
if (counter < capacity && element > 0) {
arr[counter] = element;
counter++;
return true;
}
return false;
}
public CarouselRun run(){
return new CarouselRun();
}
}
public class CarouselRun {
int position = 0;
int number = 0;
/*
if (arr[position] > 0) return arr[position++]--;
if (arr[position] <= 0) {
arr = removeTheElement(arr,position);
capacity--;
if (position >= capacity) position = 0;
}
return arr[position++]--;
*/
public int next() {
if (allNegative(arr)) return -1;
if (position >= capacity) position = 0;
if (arr[position] <= 0) {
if (position == arr.length - 1) {
position = 0;
if (arr[position] == 0) {
return arr[++position]--;
}
else {return arr[position++]--;}
}
else {
return arr[++position]--;
}
}
return arr[position++]--;
}
public static int[] removeTheElement(int[] arr, int index)
{
// If the array is empty
// or the index is not in array range
// return the original array
if (arr == null || index < 0
|| index >= arr.length) {
return arr;
}
// Create another array of size one less
int[] anotherArray = new int[arr.length - 1];
// Copy the elements except the index
// from original array to the other array
for (int i = 0, k = 0; i < arr.length; i++) {
// if the index is
// the removal element index
if (i == index) {
continue;
}
// if the index is not
// the removal element index
anotherArray[k++] = arr[i];
}
// return the resultant array
return anotherArray;
}
public static boolean allNegative (int arr[]) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) return false;
}
return true;
}
public boolean isFinished() {
for (int var: arr) {
if (var > 0) return false;
}
return true;
}
}
主要方法和输出的外观:
public class Main {
public static void main (String args[]) {
DecrementingCarousel carousel = new DecrementingCarousel(7);
carousel.addElement(2);
carousel.addElement(3);
carousel.addElement(1);
CarouselRun run = carousel.run();
System.out.println(run.isFinished()); //false
System.out.println(run.next()); //2
System.out.println(run.next()); //3
System.out.println(run.next()); //1
System.out.println(run.next()); //1
System.out.println(run.next()); //2
System.out.println(run.next()); //1
System.out.println(run.isFinished()); //true
System.out.println(run.next()); //-1
}
}
您需要遍历数组,直到找到一个值大于 0 的索引。您还可以使用该循环来检查是否存在 any 个值大于 0:
public int next() {
int count = 0;
while (count < arr.length && arr[position %= arr.length] <= 0) {
position++;
count++;
}
if (count == arr.length) return -1;
return arr[position++]--;
}
我不得不制作一个递减轮播 [1]:https://i.stack.imgur.com/T7E4d.png。虽然它有点管用,但我需要它 return 正确的结果,这样我才能通过测试。代码:
int position = 0;
int capacity = 3;
public int next() {
if (allNegative(arr)) return -1;
if (position >= capacity) position = 0;
if (arr[position] <= 0) {
if (position == arr.length - 1) {
position = 0;
if (arr[position] == 0) {
return arr[++position]--;
}
else {return arr[position++]--;}
}
else {
return arr[++position]--;
}
}
return arr[position++]--;
}
所以它将元素放入数组中,next() 方法应该 return 当前值并递减它,之后它移动到下一个元素。它应该跳过零元素。当它没有什么可减少时,它 returns -1。我一直卡在这里,我不明白什么不起作用。
一个程序的完整代码:
public class DecrementingCarousel {
static int capacity;
int counter = 0;
//static List <Integer> elements = new ArrayList<>();
static int [] arr ;
public DecrementingCarousel(int capacity) {
DecrementingCarousel.capacity = capacity;
arr = new int[capacity];
}
public boolean addElement(int element){
if (counter < capacity && element > 0) {
arr[counter] = element;
counter++;
return true;
}
return false;
}
public CarouselRun run(){
return new CarouselRun();
}
}
public class CarouselRun {
int position = 0;
int number = 0;
/*
if (arr[position] > 0) return arr[position++]--;
if (arr[position] <= 0) {
arr = removeTheElement(arr,position);
capacity--;
if (position >= capacity) position = 0;
}
return arr[position++]--;
*/
public int next() {
if (allNegative(arr)) return -1;
if (position >= capacity) position = 0;
if (arr[position] <= 0) {
if (position == arr.length - 1) {
position = 0;
if (arr[position] == 0) {
return arr[++position]--;
}
else {return arr[position++]--;}
}
else {
return arr[++position]--;
}
}
return arr[position++]--;
}
public static int[] removeTheElement(int[] arr, int index)
{
// If the array is empty
// or the index is not in array range
// return the original array
if (arr == null || index < 0
|| index >= arr.length) {
return arr;
}
// Create another array of size one less
int[] anotherArray = new int[arr.length - 1];
// Copy the elements except the index
// from original array to the other array
for (int i = 0, k = 0; i < arr.length; i++) {
// if the index is
// the removal element index
if (i == index) {
continue;
}
// if the index is not
// the removal element index
anotherArray[k++] = arr[i];
}
// return the resultant array
return anotherArray;
}
public static boolean allNegative (int arr[]) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) return false;
}
return true;
}
public boolean isFinished() {
for (int var: arr) {
if (var > 0) return false;
}
return true;
}
}
主要方法和输出的外观:
public class Main {
public static void main (String args[]) {
DecrementingCarousel carousel = new DecrementingCarousel(7);
carousel.addElement(2);
carousel.addElement(3);
carousel.addElement(1);
CarouselRun run = carousel.run();
System.out.println(run.isFinished()); //false
System.out.println(run.next()); //2
System.out.println(run.next()); //3
System.out.println(run.next()); //1
System.out.println(run.next()); //1
System.out.println(run.next()); //2
System.out.println(run.next()); //1
System.out.println(run.isFinished()); //true
System.out.println(run.next()); //-1
}
}
您需要遍历数组,直到找到一个值大于 0 的索引。您还可以使用该循环来检查是否存在 any 个值大于 0:
public int next() {
int count = 0;
while (count < arr.length && arr[position %= arr.length] <= 0) {
position++;
count++;
}
if (count == arr.length) return -1;
return arr[position++]--;
}