在我的Java程序中获得意外的输出

时间:2015-07-13 14:20:26

标签: java

我不明白为什么我会得到错误的答案。找不到我的代码有什么问题。如果有人能找到问题,我将非常感激。

以下是问题的链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=37

这是我的代码:

import java.util.Scanner;

class Main
{


public static void main(String[] args)
{


    Scanner input = new Scanner(System.in);

    String REGEX_WHITESPACE = "\\s+";

    while(input.hasNext())            //The input
    {
    String numberOfBlocksInput =  input.nextLine();

    int numberOfBlocks = Integer.parseInt(((numberOfBlocksInput.trim().replaceAll(REGEX_WHITESPACE, " ")).split(REGEX_WHITESPACE))[0]);

    if (numberOfBlocks>25)
    {
    return;
    }

    int[][] blocks;
    blocks = new int[numberOfBlocks][1];

    for (int z = 0; z<numberOfBlocks;z++)
    {
    blocks[z][0] = z;                         //Fill up the Blocks
    }


     while(input.hasNext())            //The input
    { 
         String line = input.nextLine();
         String cleanLine = line.trim().replaceAll(REGEX_WHITESPACE, " ");//Remove All Spaces

         if(cleanLine.equals("quit"))
            break;

         else
         {
          String[] temp = cleanLine.split(REGEX_WHITESPACE); //Now temp's first element is a, third is b, second is move/pile, fourth is over/onto

          int a=0;
          int b=0;
          a = Integer.parseInt(temp[1]);
          b = Integer.parseInt(temp[3]);
          String command = "";
          command = temp[0]+temp[2];


          if (command.equals("moveonto"))
           moveOnto(a,b,blocks);
          else if (command.equals("moveover"))
           moveOver(a,b,blocks);    
          else if (command.equals("pileonto"))
           pileOnto(a,b,blocks);    
          else if (command.equals("pileover"))
           pileOver(a,b,blocks); 

         }


    }


     for (int i = 0;i<blocks.length;i++) //The output
    {
        System.out.printf("%d: ", i);

        for (int j = 0;j<blocks[i].length;j++)
        {
         System.out.printf("%d", blocks[i][j]);

         if (i+1<blocks.length && j+1<blocks[i].length)
          System.out.printf(" ");
        }
        if (i+1<blocks.length)
          System.out.printf("\n");
    }
    }


}

public static boolean InTheSameBox(int x, int y, int blocks[][])  // Checks if a and b are in the same box (or, a==b)
{
    boolean foundA = false;
    boolean foundB = false;

    for (int[] block : blocks) 
    {
        for (int j = 0; j < block.length; j++) 
        {
            if (block[j] == x) 
            {
                foundA = true;// a is in 'j' block 
            }
            if (block[j] == y) 
            {
                foundB = true; // b is in 'j' block 
            }

            if (foundA && foundB) //Both a & b is in the same block
            {
                return true;
            }
        }

        foundA = false;
        foundB = false;
    }

     return false;
}


public static void moveOnto(int x, int y, int blocks[][])
{

    if (InTheSameBox(x,y,blocks))
    {
     return;
    }

    int[] blockA = null;          //The block a is in
    int[] blockB = null;          //The block b is in
    boolean foundA = false;       //Found input a
    boolean foundB = false;       //Found input b
    int m = 0;                    //The number of block a is in
    int n = 0;                    //The number of block b is in

    for (int i = 0;i<blocks.length;i++)
    {
        for (int j = 0;j<blocks[i].length;j++)
     {

       if (foundA) // If input a is found, return any blocks that are stacked on top of block a to their initial positions
       {
             blocks[blocks[i][j]] = new int[1];
             blocks[blocks[i][j]][0] = blocks[i][j];
       }

       if (foundB) // If input b is found, return any blocks that are stacked on top of block b to their initial positions
       {
             blocks[blocks[i][j]] = new int[1];
             blocks[blocks[i][j]][0] = blocks[i][j];
       }

        if (blocks[i][j]==x)  //Find a
        {
            foundA = true;
            blockA = new int[j+1];
            System.arraycopy(blocks[i], 0, blockA, 0, j+1);   
            m = i;
        }


        if (blocks[i][j]==y)  //Find b
        {
            foundB = true;
            blockB = new int[j+1];
            System.arraycopy(blocks[i], 0, blockB, 0, j+1); 
            n = i;
        }




     }
        foundA = false;
        foundB = false;

    }


    int[] tmpBlock = new int[blockB.length+1];          //Make a copy of block b is in with one more element...as the last element will be a
    System.arraycopy(blockB, 0, tmpBlock, 0, blockB.length);
    tmpBlock[blockB.length] = blockA[blockA.length-1];  //puts a onto b

    blocks[n] = new int[tmpBlock.length];               //Return the block b
    System.arraycopy(tmpBlock, 0, blocks[n], 0, tmpBlock.length);

    blocks[m] = new int[blockA.length-1];               //Return the block a, with one less element, as a have been removed
    System.arraycopy(blockA, 0, blocks[m], 0, blockA.length-1);

}


public static void moveOver(int x, int y, int blocks[][])
{
    if (InTheSameBox(x,y,blocks))
    {
    return;
    }

    int[] blockA = null;          //The block a is in
    int[] blockB = null;          //The block b is in
    boolean foundA = false;       //Found input a
    int m = 0;                    //The number of block a is in
    int n = 0;                    //The number of block b is in

    for (int i = 0;i<blocks.length;i++)
    {
        for (int j = 0;j<blocks[i].length;j++)
     {

        if (foundA)             // If input a is found, return any blocks that are stacked on top of block a to their initial positions
        {
            blocks[blocks[i][j]] = new int[1];
            blocks[blocks[i][j]][0] = blocks[i][j];
        }


        if (blocks[i][j]==x)  //Find a
        {
            foundA = true;
            blockA = new int[j+1];
            System.arraycopy(blocks[i], 0, blockA, 0, j+1);   
            m = i;
        }


        if (blocks[i][j]==y)  //Find b
        {
            blockB = new int[blocks[i].length];
            System.arraycopy(blocks[i], 0, blockB, 0, blocks[i].length); 
            n = i;
        }




     }
        foundA = false;

    }


    int[] tmpBlock = new int[blockB.length+1];           //Make a copy of block b is in with one more element...as the last element will be a
    System.arraycopy(blockB, 0, tmpBlock, 0, blockB.length);
    tmpBlock[blockB.length] = blockA[blockA.length-1];   //puts a over b(on top of stack containing b)

    blocks[n] = new int[tmpBlock.length];                //Return the block b
    System.arraycopy(tmpBlock, 0, blocks[n], 0, tmpBlock.length);

    blocks[m] = new int[blockA.length-1];                //Return the block a, with one less element, as a have been removed
    System.arraycopy(blockA, 0, blocks[m], 0, blockA.length-1);

}


public static void pileOnto(int x, int y, int blocks[][])
{
    if (InTheSameBox(x,y,blocks))
    {
     return;
    }

    int[] blockA = null;          //The block a is in
    int[] blockB = null;          //The block b is in
    boolean foundB = false;       //Found input a
    int m = 0;                    //The number of block a is in
    int n = 0;                    //The number of block b is in
    int p = 0;                    //The number of blocks there will be in block a is in, after a and blocks stacked on top of a have been moved to other block

    for (int i = 0;i<blocks.length;i++)
    {
        for (int j = 0;j<blocks[i].length;j++)
     {


        if (foundB)                 // If input b is found, return any blocks that are stacked on top of block b to their initial positions
        {
            blocks[blocks[i][j]] = new int[1];
            blocks[blocks[i][j]][0] = blocks[i][j];
        }

        if (blocks[i][j]==x)      //Find a
        {
            blockA = new int[blocks[i].length];
            System.arraycopy(blocks[i], 0, blockA, 0, blocks[i].length);   
            m = i;
            p = j;
        }


        if (blocks[i][j]==y)      //Find b
        {
            foundB = true;
            blockB = new int[j+1];
            System.arraycopy(blocks[i], 0, blockB, 0, j+1); 
            n = i;
        }

     }

        foundB = false;
    }


    int[] tmpBlock = new int[blockB.length+(blockA.length-p)];             //Make a copy of block b is in with more elements as needed...the last elements will be a and blocks stacked on top of a
    System.arraycopy(blockB, 0, tmpBlock, 0, blockB.length);
    System.arraycopy(blockA, p, tmpBlock,blockB.length, blockA.length-p);  //puts a and blocks stacked on top of a onto b

    blocks[n] = new int[tmpBlock.length];                                  //Return the block b
    System.arraycopy(tmpBlock, 0, blocks[n], 0, tmpBlock.length);

    blocks[m] = new int[p];                                                //Return the block a, removing elements that have been moved to other block
    System.arraycopy(blockA, 0, blocks[m], 0, p); 


}


public static void pileOver(int x, int y, int blocks[][]) 
{

    if (InTheSameBox(x,y,blocks))
    {
     return;
    }

    int[] blockA = null;          //The block a is in
    int[] blockB = null;          //The block b is in
    int m = 0;                    //The number of block a is in
    int n = 0;                    //The number of block b is in
    int p = 0;                    //The number of blocks there will be in block a is in, after a and blocks stacked on top of a have been moved to other block

    for (int i = 0;i<blocks.length;i++)
    {
        for (int j = 0;j<blocks[i].length;j++)
     {


        if (blocks[i][j]==x)   //Find a
        {
            blockA = new int[blocks[i].length];
            System.arraycopy(blocks[i], 0, blockA, 0,  blocks[i].length);   
            m = i;
            p = j;
        }


        if (blocks[i][j]==y)   //Find b
        {
            blockB = new int[blocks[i].length];
            System.arraycopy(blocks[i], 0, blockB, 0, blocks[i].length); 
            n = i;
        }

     }

    }



    int[] tmpBlock = new int[blockB.length+(blockA.length-p)];              //Make a copy of block b is in with more elements as needed...the last elements will be a and blocks stacked on top of a
    System.arraycopy(blockB, 0, tmpBlock, 0, blockB.length);
    System.arraycopy(blockA, p, tmpBlock,blockB.length, blockA.length-p);   //puts a and blocks stacked on top of a over b(on top of stack containing b)

    blocks[n] = new int[tmpBlock.length];
    System.arraycopy(tmpBlock, 0, blocks[n], 0, tmpBlock.length);           //Return the block b

    blocks[m] = new int[p];
    System.arraycopy(blockA, 0, blocks[m], 0, p);                           //Return the block a, removing elements that have been moved to other block

}


}

1 个答案:

答案 0 :(得分:0)

我已经弄明白了(感谢&#39; Gilbert Le Blanc&#39; ),

我有2个错误......

Gilbert Le Blanc指出了一个......

一个。我在数字和冒号之间有一个空格..

湾另一个错误是我没有在线上挣扎......那就是:

for (int i = 0;i<blocks.length;i++) //The output
{
    System.out.printf("%d : ", i);  //// Here was the 1st Problem

    for (int j = 0;j<blocks[i].length;j++)
    {
     System.out.printf("%d", blocks[i][j]);

     if (i+1<blocks.length && j+1<blocks[i].length)
      System.out.printf(" ");
    }
    if (i+1<blocks.length)         ////Here was the 2nd Problem
      System.out.printf("\n");
}

我不得不改为:

for (int i = 0;i<blocks.length;i++) //The output
{
    System.out.printf("%d: ", i);

    for (int j = 0;j<blocks[i].length;j++)
    {
     System.out.printf("%d", blocks[i][j]);

     if (i+1<blocks.length && j+1<blocks[i].length)
      System.out.printf(" ");
    }

      System.out.printf("\n");
}
}

那些是UVA法官不接受我答案的原因,现在它被接受了。

感谢您的回复..