下列程式碼是自動計算找零程式的一部分, 程式碼中三個主要變數分別為 Total (購買總額 ),Paid (實際支付金額 ),Change (找零金額 )。但是此程式片段有冗餘的程式碼,請找出冗餘程式碼的區塊。- int Total, Paid, Change;
- …
- Change = Paid - Total;
- printf ("500 : %d pieces\n", (Change-Change%500)/500);
- Change = Change % 500;
- printf ("100 : %d coins\n", (Change-Change%100)/100);
- Change = Change % 100;
- // A 區
- printf (“50 : %d coins\n”, (Change-Change%50)/50);
- Change = Change % 50;
- // B 區
- printf ("10 : %d coins\n", (Change-Change%10)/10);
- Change = Change % 10;
- // C 區
- printf ("5 : %d coins\n", (Change-Change%5)/5);
- Change = Change % 5;
- // D 區
- printf ("1 : %d coins\n", (Change-Change%1)/1);
- Change = Change % 1;
複製代碼 |