#include #include void printTransfer ( int i, int qty, char src, char dst, double pcnt1, double pcnt2 ) { printf("Transfer %3d: %d ml from %c to %c, ", i, qty, src, dst); printf("Alcohol percentages: "); printf("%7.3lf and %7.3lf\n", pcnt1, pcnt2); } /*** For students with ODD PC numbers ***/ int oddmain () { double x, /* quantity of alcohol in Glass 1 */ y, /* quantity of alcohol in Glass 2 */ u, /* quantity of water in Glass 1 */ v; /* quantity of water in Glass 2 */ int i; /* Initialize */ x = 0; u = 100; /* Glass 1 contains only water */ y = 100; v = 0; /* Glass 2 contains only alcohol */ /* First move 10 ml from Glass 2 to Glass 1 */ i = 1; x += y / 10; u += v / 10; y -= y / 10; v -= v / 10; printTransfer(i, 10, 'B', 'A', 100 * x / (x + u), 100 * y / (y + v)); /* Iterate */ while ((y/(y+v)-x/(x+u)) > 0.01) { ++i; if (i % 2 == 0) { /* Move 20 ml from Glass 1 to Glass 2 */ y += 2 * x / 11; v += 2 * u / 11; x -= 2 * x / 11; u -= 2 * u / 11; printTransfer(i, 20, 'A', 'B', 100 * x / (x + u), 100 * y / (y + v)); } else { /* Move 20 ml from Glass 2 to Glass 1 */ x += 2 * y / 11; u += 2 * v / 11; y -= 2 * y / 11; v -= 2 * v / 11; printTransfer(i, 20, 'B', 'A', 100 * x / (x + u), 100 * y / (y + v)); } } /* Final adjustment */ ++i; if (i % 2 == 0) { /* Move 10 ml from Glass 1 to Glass 2 */ y += x / 11; v += u / 11; x -= x / 11; u -= u / 11; printTransfer(i, 10, 'A', 'B', 100 * x / (x + u), 100 * y / (y + v)); } else { /* Move 10 ml from Glass 2 to Glass 1 */ x += y / 11; u += v / 11; y -= y / 11; v -= v / 11; printTransfer(i, 10, 'B', 'A', 100 * x / (x + u), 100 * y / (y + v)); } exit(0); } /*** For students with EVEN PC numbers ***/ int evenmain () { double x, /* quantity of alcohol in Glass 1 */ y, /* quantity of alcohol in Glass 2 */ u, /* quantity of water in Glass 1 */ v; /* quantity of water in Glass 2 */ int i; /* Initialize */ x = 100; u = 0; /* Glass 1 contains only alcohol */ y = 0; v = 150; /* Glass 2 contains only water */ i = 0; while ((x/(x+u) - y/(y+v)) > 0.01) { /* Move 10 ml from Glass 1 to Glass 2 */ ++i; y += x / 10; v += u / 10; x -= x / 10; u -= u / 10; printTransfer(i, 10, 'A', 'B', 100 * x / (x + u), 100 * y / (y + v)); /* Move 10 ml from Glass 2 to Glass 1 */ ++i; x += y / 16; u += v / 16; y -= y / 16; v -= v / 16; printTransfer(i, 10, 'B', 'A', 100 * x / (x + u), 100 * y / (y + v)); } exit(0); } int main () { char parity; while (1) { printf("Odd or Even? "); scanf("%c", &parity); if ((parity == 'o') || (parity == 'O')) oddmain(); if ((parity == 'e') || (parity == 'E')) evenmain(); } exit(1); }