読み取り/書き込み操作を参照してループを最小化します
-
27-09-2019 - |
質問
今日私が最小限に抑えたいのは、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);
}
それが役立つことを願っています。
所属していません StackOverflow