奇数序列替换为计数

我正在研究 Java 程序


这是程序问题:考虑 Java 程序。它从标准输入中读取整数(直到它得到一个负数)并将它们放入一个数组中。之后它在数组上调用 processArray,然后在标准输出上打印数组的内容。在程序中,数组中任何两个或多个连续奇数的序列都会从数组中移除,并替换为代表该序列长度的单个数字。processArray 函数/方法应该就地修改数组(最好不要创建新数组),并且它应该返回修改后数组的新长度。


例如,如果这些数字是在标准输入中提供的:


222

3

35

62

124

61

29

375

66

7

-1

然后程序应该打印:


222

2

62

124

3

66

7

请注意,序列 3、35 已被 2 替换,而序列 61、29、375 已被 3 替换。


这是我的代码:


import java.util.*;

import java.io.*;


public class Main {

    public static int processArray(ArrayList<Integer> array) {

        ListIterator<Integer>iterator=array.listIterator();

        while (iterator.hasNext()) {

            Integer integer = (Integer) iterator.next();

            int count=0;

            if (integer%2!=0) {

                count=count++;

                iterator.remove();

                continue;

            }

            if(integer==-1)

                break;

            else

                iterator.previous();

            iterator.add(count);

            iterator.next();



        }


        return array.size();

    }


    public static void main (String[] args) {

        ArrayList<Integer> arrayList = new ArrayList<Integer>();

        Scanner in = new Scanner(System.in);

        while(in.hasNextInt()) {

            int num = in.nextInt();

            if (num < 0) 

                break;

            arrayList.add(new Integer(num));

        }

        int new_length = processArray(arrayList);

        for(int i=0; i<new_length; i++)

            System.out.println(arrayList.get(i));

    }

}

我的逻辑无法正常工作有助于提高逻辑


撒科打诨
浏览 149回答 3
3回答

繁星点点滴滴

试试下面的逻辑。public static int processArray(ArrayList<Integer> array) {&nbsp; &nbsp; int count=0;&nbsp; &nbsp; for(int i=0;i<array.size();i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if((array.get(i)%2)!=0) //odd&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(count>1)&nbsp; &nbsp; &nbsp;//I had to replace length of&nbsp; odd seq greater than or equal to 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array.set(i,count);&nbsp; &nbsp; &nbsp;//set curren count to current odd no and remove previous odd number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array.remove(i-1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i>0)&nbsp; &nbsp; &nbsp;//For handling change in indices&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i=i-1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i=0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count=0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return array.size();}

白衣染霜花

试试下面的 Python 代码:首先用计数替换最后一个连续的奇数,然后用 -1 替换前面的奇数。最后,从列表中删除所有 -1。原因:用-1代替连续的奇数来遍历整个列表会很方便,否则数字的结构和索引会改变并且难以迭代。l=[222,3,35,62,124,61,29,375,66,7,-1]count = 0for i in range(0,len(l)):&nbsp; &nbsp; if l[i]%2!=0 and i!=len(l)-1:&nbsp; &nbsp; &nbsp; &nbsp; count +=1&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; if count > 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l[i-1]=count&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while count != 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l[i-count]= -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count -= 1&nbsp; &nbsp; &nbsp; &nbsp; count = 0l = [i for i in l if i>=0 ]print(l)输出:[222, 2, 62, 124, 3, 66, 7]

子衿沉夜

您应该在 processArray() 中添加另一个循环,该循环在找到奇数时执行并增加计数在循环之前将号码存储为备份然后在添加之前检查计数是否大于 1 然后用计数替换数字,否则使用备份整数您还应该保存第一个奇数的迭代器位置。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java