奇数序列替换为计数

问题描述 投票:0回答:6

我正在开发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));
    }
}

我的逻辑不能正常工作有助于提高逻辑

java arrays arraylist collections
6个回答
0
投票

添加另一个问题:

您应该删除:

if (integer == -1)
    break;

因为您已经消除了第一个循环中的负数


0
投票
  • 您应该在 processArray() 中添加另一个循环,该循环在找到奇数时执行并增加计数

  • 在循环之前将号码存储为备份

  • 然后在添加之前检查计数是否大于1,然后用计数替换数字,否则使用备份整数

    您还应该保存第一个奇数的迭代器位置。


0
投票

processArray
中,您需要将
int count=0;
移动到
while
循环之外(之前)。

while
循环中,您对奇怪情况的处理大多是正确的。但是,您需要添加一个
else
子句来处理偶数情况。它应该看起来像这样(伪代码):

if odd
    count++;
    remove current element
else
    if count > 0 
        add count to the array
        set count to zero
    add current element

while
循环内的所有其他内容都可以删除。

您还需要处理列表以奇数序列结尾的情况。您需要在 while 循环结束后通过检查 count 是否 > 0 来处理此问题。


0
投票

尝试下面的逻辑。

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

0
投票
lis=[]

while True:
    inp= int(input())

    if inp<0:
        break

    else:
        lis.append(inp)


def processArray(lis):
    count=0
    for x in lis:
        ind=lis.index(x)

        if x%2!=0:                             # if number = odd
            if ind!=len(lis)-1:                #  if not last element
                count=count+1         #count of odd sequence
                y=x                   #storing x for later use as x is removed from list
                lis.remove(x)
                lis.insert(ind,-1)    #replacing x with -1 as removing x changes entire list index structure, messing with for loop iteration
                #print('odd ',x, lis, count)  

            if ind==len(lis)-1 and count>1:     # if last element and count of odd > 1
                lis.remove(x)                   
                lis.append(count+1)            
                break

        elif x%2==0:            # if number = even
           # print('even ',x, lis, count)

            if count==1:        # if count of odd =1, keeping same number back from -1 to y 
                lis.remove(-1)
                lis.insert(ind-1,y)
                count=0
               # print('even count=1 ',x, lis, count)

            if count>1 :        # if count of odd >1, adding count element in list
                 lis.insert(ind-1,count)
                 count=0
                # print('even count>1 ',x, lis, count)

    while -1 in lis:            # removing all -1
           lis.remove(-1)

    return len(lis)

print('length of modified list ',processArray(lis))
print(lis)

0
投票

尝试下面的Python代码:首先用计数替换最后一个连续的奇数,然后用-1替换前面的奇数。 最后,从列表中删除所有 -1。

原因:将连续奇数替换为-1可以方便遍历整个列表,否则数字的结构和索引会改变,导致迭代困难。

l=[222,3,35,62,124,61,29,375,66,7,-1]
count = 0
for i in range(0,len(l)):
    if l[i]%2!=0 and i!=len(l)-1:
        count +=1
    else:
        if count > 1:
            l[i-1]=count
            while count != 1:
                l[i-count]= -1
                count -= 1
        count = 0
l = [i for i in l if i>=0 ]

print(l)

输出:[222,2,62,124,3,66,7]

© www.soinside.com 2019 - 2024. All rights reserved.