今天我要最小化的最后一件事是包含while和do-while循环的代码。这是原始文件:

class Vereinfache3_edit {

        public static void main(String [] args) {

           int c1 = Integer.parseInt(args[0]) ;
           int c2 = Integer.parseInt(args[1]) ;
           int c3 = Integer.parseInt(args[2]) ;

/*  1 */           c1 += 7 ;
/*  2 */           System.out.println( c1 ) ;

/*  3 */       while (c1 % 8 != 0)
/*  4 */              if ( c1 % 16 == 0 ) ;
/*  5 */              else
/*  6 */         do
/*  7 */                 {
/*  8 */                    c1 += 7 ;
/*  9 */                    System.out.println( c1 ) ;
/* 10 */                    if ( c2 < c3 )
/* 11 */                       { c1 = c1+c1 ;
/* 12 */                         c3 ++ ;
/* 13 */                         c1 /= 2 ;
/* 14 */                         c3 -= 1 ;
/* 15 */                       }
/* 16 */                 }
/* 17 */                 while ( c1 % 8 != 0 ) ;

/* 18 */           c1 += 7 ;
/* 19 */           System.out.println( c1 ) ;
        }

}  // end of class Vereinfache3


这是我的最小化版本:

class Vereinfache3 {

        public static void main(String [] args) {

               int c1 = Integer.parseInt(args[0]);
               int c2 = Integer.parseInt(args[1]) ;
               int c3 = Integer.parseInt(args[2]) ;

               do{
                   c1 += 7 ;
                   System.out.println( c1 ) ;
               }while (c1 % 8 != 0);

/* 18 */       c1 += 7 ;
/* 19 */       System.out.println( c1 ) ;
        }

}  // end of class Vereinfache3


它为我生成相同的输出。您是否看到任何错误或可以改进的地方?

尤其是这段代码似乎很棘手:

/*  3 */       while (c1 % 8 != 0)
/*  4 */              if ( c1 % 16 == 0 ) ;
/*  5 */              else
/*  6 */          do{}


我该如何处理?什么包含while循环?

最佳答案

最小化版本会产生与原始版本相同的结果,仅当c1 % 8 != 0 and c1 % 16 == 0
这种情况是不可能的,因为16的所有倍数也都是8的倍数。

删除未使用的声明是我可以建议的唯一改进。
我确实有一个使用更少代码的解决方案..不是说它可以使事情变得更好...我不喜欢代码中的过多sysouts .. :)

    int finalBound = ((c1 + 7) % 8 == 0) ? c1 + 7 : 8 * c1;
    for (; c1 <= finalBound; c1 += 7) {
        System.out.println(c1 + 7);
    }


希望能有所帮助。

关于java - 最小化引用读/写操作的循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4062970/

10-12 04:16
查看更多