1

这是我用 Java 编写的代码,itr 是密钥的迭代器,我在迭代密钥时遇到了问题,代码的问题是:我们得到一个 2n 整数数组,其中每一对在这个数组中整数分别代表恐龙的出生年份和死亡年份。我们要考虑的有效年份范围是 [-100000 到 2005]。例如,如果输入是:

-80000 -79950 20 70 22 60 58 65 1950 2004

这意味着第一只恐龙的出生年份分别为 –80000 年和死亡年份 –79950 年。同样,第二只恐龙的寿命从 20 岁到 70 岁,以此类推。

我们想知道同时存在的最大数量的恐龙。给定上面的 2n 个整数数组,编写一个方法来计算它。

编写以下方法之一:

C/C++: int find_max_dinosaur (int[] 年);

Java: public int findMaxdinosaur (int[] 年);

package usingarray;


int dinoStrength;
int deathOfDino;
int dinoBirthAge;
int currentDinoBirthAge;
int currentDinoDeathAge;
int count;

TreeMap dino=new TreeMap();
List<Integer> dinos = new ArrayList<Integer>();

Scanner sc=new Scanner(System.in);


public void getDionoAges()
{
    System.out.println("How many Dinosaur");
    dinoStrength=sc.nextInt()+1;
    for(int i=1;i<dinoStrength;i++)
    {
        System.out.println("Please enter "+i+" dino birth age and death age:");
        dino.put(sc.nextInt(),sc.nextInt());
    }
}

public void logic()
{
    Collection value = dino.values();
    Collection key = dino.values();
    Iterator it = value.iterator();
    Iterator itr = key.iterator();

    System.out.println("logic");

    for(int x=0;x<dinoStrength;x++)
    {
        System.out.println("in for");
    while(it.hasNext() && itr.hasNext())
        {
            System.out.println("in while");
            deathOfDino=(int) it.next();
            //currentDinoDeathAge=(int) it.next();
            dinoBirthAge=(int) itr.next();

            while(itr.hasNext())
            {
                System.out.println("In itr while");
                currentDinoBirthAge=(int) itr.next();
                if(dinoBirthAge<=currentDinoBirthAge && currentDinoBirthAge<=deathOfDino)
                {
                    count++;
                    System.out.println(count);
                }
            }
            dinos.add(count);
        }
    }
}

public void display()
{
    Iterator<Integer> it = dinos.iterator();
    while(it.hasNext())
    {
        System.out.println(it.next());
    }
}
public static void main(String[] args) {
    VickyDino vd=new VickyDino();
    vd.getDionoAges();
    vd.logic();
    vd.display();
}

}

4

2 回答 2

1

我会假设你的问题是这样的:

Collection value = dino.values();
Collection key = dino.values();

你用dino.values()了两次;第二个应该是dino.keys()。但是,由于您将出生年份放入键中,将死亡年份放入值中,您可能想要切换两者。注意:我没有检查循环中的逻辑是否正确。

于 2013-02-03T16:46:47.193 回答
0

/* 问题:给定一个由 2n 个整数组成的数组,其中这个整数数组中的每一对分别代表恐龙的出生年份和死亡年份。我们想要的有效年份范围......分别是恐龙的死亡。我们要考虑的有效年份范围是 [-100000 到 2005]。

*/

公共 int findMaxdinosaur (int[] 年) {

    int[] birthDino = new int[years.length];
    int[] deathDino=new int[years.length];

      //Now  birth & death list is made in which death contains even place.

       for(int i=0;i<=years.length-2;i=i+2){  
           birthDino[i]=years[i];
           deathDino[i+1]=years[i+1];
          }
     Arrays.sort(birthDino);
     Arrays.sort(deathDino);
   List<Integer> al=new ArrayList<Integer>();//List is need because i need to remove null values from here
       for(int aa: birthDino){
            al.add(aa);
       }
      al.removeAll(Collections.singleton(0));// Removing all positions which contain null values
    // System.out.println(al);
     List<Integer> all=new ArrayList<Integer>();
       for(int aa: deathDino){
         all.add(aa);                             //adding in to the List
        }
     all.removeAll(Collections.singleton(0));

 int  Alive=0,maxDinos=0,LastBornMax=0;
 int b = 0,d=0;

    while( b!=birthDino.length && d!=deathDino.length) {
            if (birthDino[b] < deathDino[d])  {
                  Alive =  Alive + 1;
                  b = b + 1;
                } 
                 else {
                         Alive =  Alive - 1;
                        d = d + 1  ;    
                       }

            if(maxDinos <  Alive){  //checking the values
                maxDinos =  Alive;
                LastBornMax=birthDino[b-1];
                }
       }//while end

    //S N Prasad Rao.
      return maxDinos;
}//end of method
于 2013-02-04T08:22:15.943 回答