/* Problem 9--Number of Numbers Although this was the easiest problem to solve, there was a deliberate vagueness in the description that led people not to test for negative numbers at all, thus slowing down the competition for everyone. */ #include #include int main (int argc, char **argv); int mod (int a, int b); FILE *in, *out; int main (int argc, char **argv) { int first, last, diff, ct, cs=0; in = fopen ("prob9.in","r"); out = fopen ("prob9.out","w"); while (fscanf (in,"%d %d %d",&first,&last,&diff),first<=last) { if (first==last) ct = 1; /* Certain easy special cases */ else if (diff==0) ct = 2; else { diff = abs(diff); ct = (last-first)/diff+1; /* How many numbers */ if (mod (last,diff)!=mod(first,diff)) ct*=2; /*Doubled if necessary*/ } if (ct!=1) fprintf (out,"Case %d: Set contains %d integers.\n\n",++cs,ct); else fprintf (out,"Case %d: Set contains %d integer.\n\n",++cs,ct); } fclose (in); fclose (out); return EXIT_SUCCESS; } /* Mod computes a normal modulo unless a is negative, in which case it is adjusted to ensure that the answer is nonnegative. */ int mod (int a, int b) { if (a>=0) return a%b; return ((b-(-a)%b)%b); }