学校有个算法程序要写:这个程序要求输入一组数组:
第一个数组有三个值:代表电脑数,下面输入数组的行数,还有问题数;
第二个数组是二维的,每行三个值:代表电脑1,电脑2,还有它们的通信时间;
第三个数组是问题, 每行有四个值:代表电脑1在时间a染的病毒,有没有可能传染给电脑2,在时间b;
最后要求如果可能传染,写出传染路径。
下面是我写的程序,但是输出数组的末尾总带有很多0,我的编程基础是在差,请各位高手告诉我为什么,谢谢!
import java.io.*;
public class assignment1{
  public static int[] readALine() {
   try {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      String input = br.readLine();
      String[] tmp = input.split(" +");
      int[] a = new int[tmp.length];
      for (int i = 0; i < a.length; i++)
       a[i] = Integer.parseInt(tmp[i]);
      return a;
    } catch (IOException e) {
      System.out.println("read a integer has some problem!");
      return null;
      }
      // return a;
  }  public static int[] dfs(int[] a, int[][] b, int[] c)
  {                                                         //a is require, b is L, c is firstL;
    int i=0;  
    
    int[] Layer = new int[b.length];
    Layer[i]=a[0];
    int det=a[0];
    
      for(int x=0; x<c[1]; x++){
        for(int j=0; j<2; j++){
          if((a[0]==b[x][j])&&(b[x][1-j]!=det)&&(b[x][2]>=a[1])&&(b[x][2]<=a[3])){
            
            
            if (b[x][1 - j] == a[2]) { //if  in one step;
              Layer[i] = b[x][1 - j];
              return Layer;
            }
            else {
      if((b[x][1-j]!=0)&&(b[x][j]!=0)){
              Layer[i] = b[x][1 - j];
              a[0] = b[x][1 - j];
              det = b[x][j];
              i++;
      }else{
        break;
      }
            }
            
          }else{
     break;     
  }
        }
       }
    
    return null;
  }  public static void main(String[] args)
  {
    System.out.println("please input the data : ");
    int[] firstL=readALine();
///////////////////////////////////////////////////
    System.out.println("please input the data : ");
    int[][] L = new int[firstL[1]][3];
    for(int i=0; i<firstL[1]; i++)
    { int[] mid=readALine();
      //System.out.println(mid[2]);
      for(int j=0; j<3; j++)
      {
        L[i][j] = mid[j];
      }
    }
////////////////////////////////////////////////////
    System.out.println("please input the data : ");
    int[] require = new int[4];
    for(int i=1; i<=firstL[2]; i++)
          {require=readALine();          int a =require[0];
          int[] fin=new int[firstL[1]*3];
          fin=dfs(require, L, firstL);          if(fin!=null)                    //[fin.length-3]==require[2])
          {
           System.out.print("("+a+","+fin[0]+")");
           for(int j=1;j<=fin.length-1;j++)
    {
    System.out.print("("+fin[j-1]+","+fin[j]+")");
    }
          }else{
            System.out.println("Infection not possible!");
          }
          }  }
}

解决方案 »

  1.   

    int的数组初始化就是0
    你别一股脑的打印呀,要判断呀
      

  2.   

    各位不好意思, 现把加了注释的代码贴出来,并改了点,但还是出0,
    而且会提示NumberFormatException.forInputString(s)
    import java.io.*;public class assignment1{
      public static int[] readALine() {  //这个方法的目的是返回一行数组;
       try {
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          String input = br.readLine();
          String[] tmp = input.split(" +");
          int[] a = new int[tmp.length];
          for (int i = 0; i < a.length; i++)
           a[i] = Integer.parseInt(tmp[i]);
          return a;
        } catch (IOException e) {
          System.out.println("read a integer has some problem!");
          return null;
          }
          
      }  public static int[] dfs(int[] a, int[][] b, int[] c)  //这个是主要的算法,
      {                     //a 对应main中的require, b对应L, c对应firstL;
        int i=0;  
        int[] Layer = new int[b.length];  //初始化
        Layer[i]=a[0];                    //初始化根结点;
        int det=a[0];
          for(int x=0; x<c[1]; x++){       //c[1]是输入的行数;
            for(int j=0; j<2; j++){         //每行只遍历前两个元素,第三个代表时间;
              while((a[0]==b[x][j])&&(b[x][1-j]!=det)&&(b[x][2]>=a[1])&&(b[x][2]<=a[3])){
             //四个必要条件,后两个是时间上的限制;
                  if (b[x][1 - j] != a[2]) { 
                  //如果a[2]不匹配;
                  Layer[i] = b[x][1 - j];  //将路径结点输入返回数组中;
                  a[0] = b[x][1 - j];
                  det = b[x][j];
                  i++;              
                  }
                  else {
                    Layer[i] = b[x][1 - j];  //否则,b[x][1 - j]即为a[2],返回数组;
                    return Layer;
                   }
                 }
              }
            }
        return null;
      }  public static void main(String[] args)
      {
        System.out.println("please input the data : ");
        int[] firstL=readALine();  //输入第一个数组;
        System.out.println("please input the data : ");
        int[][] L = new int[firstL[1]][3];  //输入读取第二个数组;
        for(int i=0; i<firstL[1]; i++)
        { int[] mid=readALine();
          for(int j=0; j<3; j++)
          {
            L[i][j] = mid[j];
          }
        }
        System.out.println("please input the data : ");
        int[] require = new int[4];    //输入读取第三个数组;
        for(int i=1; i<=firstL[2]; i++)
              {require=readALine();
              int a =require[0];
              int[] fin=new int[firstL[1]*3];
              fin=dfs(require, L, firstL);
              if(fin!=null)                    
              {
               System.out.print("("+a+","+fin[0]+")");   //打印结果;
               for(int j=1;j<fin.length;j++)
                System.out.print("("+fin[j-1]+","+fin[j]+")");
              }else{
                System.out.println("Infection not possible!");
              }
              }
      }
    }
    参考最开始写的需求分析,可以输入以下数组:
    5 5 2  //第一个数组;
    4 5 3  //第二个数组;
    1 2 4
    2 4 8
    3 4 8
    1 4 12
    2 5 5 12  //第三个数组;
    1 2 3 8
      

  3.   

    You can read the j2se API documentation for arraylist..