2002 LSU Computer Science High School Programming Contest Unknown - Problem Solution:
/*
Fifty-Fifty (and then some)
Andrew Horwitz
*/
#include
int main()
{
int series_count, /*number of series in the input*/
count_sentinel, /*will increment in while loop until it equals series_count*/
size_sentinel, /*tests to see if the entry is the first in the series*/
series_size, /*number of letters in a given series*/
heads, /*count for the number of times heads occurs*/
tails, /*count for the number of times tails occurs*/
alt, /*count for number of alternating circumstances*/
i; /*for loop counter*/
char curr_let, /*current letter entered*/
past_let; /*previous letter entered*/
FILE *file;
file = stdin;
fscanf(file,"%d", &series_count);
past_let = 'X';
count_sentinel = size_sentinel = 0;
while(series_count != count_sentinel)
{
heads = tails = alt = 0;
fscanf(file,"%d", &series_size);
for(i = 0; i < series_size; i++)
{
fscanf(file," %c", &curr_let);
if(size_sentinel == 0)
{
if(curr_let == 'H')
heads++;
if(curr_let == 'T')
tails++;
}
else
{
if(curr_let == 'H')
heads++;
if(curr_let == 'T')
tails++;
if(curr_let != past_let)
alt++;
}
past_let = curr_let;
size_sentinel = 1;
}
count_sentinel++;
if(alt > heads && alt > tails)
printf("Series %d: Alternating\n", count_sentinel);
else if (heads > tails && heads > alt)
printf ("Series %d: All Heads\n", count_sentinel);
else if (tails > heads && tails > alt)
printf ("Series %d: All Tails\n", count_sentinel);
else
printf ("Series %d: Indeterminate\n", count_sentinel);
size_sentinel = 0;
}
return 0;
}
Solution:
/*
Wherehouse?
Nick Muguira
*/
#include
int main ()
{
int dimsLoop, convertLoop, numDim, numToConvert, toConvert, dims[5];
int numJobs, jobLoop;
scanf(" %d", &numJobs);
for ( jobLoop = 1; jobLoop <= numJobs; jobLoop++ )
{
printf("Job %d:\n", jobLoop);
dims[0] = dims[1] = dims[2] = dims[3] = dims[4] = 0;
scanf("%d", &numDim);
for ( dimsLoop = 0; dimsLoop < numDim; dimsLoop++ )
{
scanf("%d", &dims[numDim - dimsLoop - 1]);
}
dims[4] = dims[3] * dims[2] * dims[1] * dims[0];
dims[3] = dims[2] * dims[1] * dims[0];
dims[2] = dims[1] * dims[0];
dims[1] = dims[0];
scanf("%d", &numToConvert);
for ( convertLoop = 0; convertLoop < numToConvert; convertLoop++ )
{
scanf("%d", &toConvert);
printf("%d -> [", toConvert);
toConvert--;
for ( dimsLoop = 0; dimsLoop < numDim - 1; dimsLoop++ )
{
printf("%d%c", ( toConvert / dims[ numDim - dimsLoop - 1] + 1 ), ',');
toConvert = toConvert % dims[ numDim - dimsLoop - 1];
}
printf("%d]\n", 1 + toConvert);
}
dims[0] = dims[1] = dims[2] = dims[3] = dims[4] = 0;
}
return 0;
}
Solution:
/*
Fifty-Fifty
Josh Abadie
*/
#include
#include
int main()
{
char prev;
char current;
int x,y;
int iterations, time;
int flips;
int heads, tails, alternating;
cin>>iterations;
for(time=0; time>flips;
tails=0;
alternating=0;
heads=0;
for(x=0; x>current;
if(current=='T')
{
tails++;
}
else
{
if(current=='H')
{
heads++;
}
}
if(prev!='n' && prev!=current)
{
alternating++;
}
prev=current;
}
if(heads>tails && heads >alternating)
{
cout<<"Series "<heads && tails>alternating)
{
cout<<"Series "<tails && alternating>heads)
{
cout<<"Series "<
Solution:
/*
Football Folly
Nick Muguira
*/
#include
int main ( void )
{
int sLoop, fLoop, tLoop, xLoop;
int fScore, lScore, toWin, minScores;
int sim, simLoop;
scanf(" %d", &sim);
for ( simLoop = 1; simLoop <= sim; simLoop++)
{
scanf(" %d %d", &fScore, &lScore);
toWin = fScore - lScore + 1;
minScores = 0;
for ( tLoop = 0; tLoop <= toWin; tLoop += 8 )
{
for ( xLoop = 0; ( xLoop + tLoop ) <= toWin; xLoop += 7 )
{
for ( fLoop = 0; ( fLoop + xLoop + tLoop ) <= toWin; fLoop += 3 )
{
for ( sLoop = 0; ( tLoop + xLoop + fLoop + sLoop ) <= toWin; sLoop += 2 )
{
if ( ( tLoop + xLoop + fLoop + sLoop ) == toWin && toWin > 0 )
{
minScores = tLoop / 8 + xLoop / 7 + fLoop / 3 + sLoop / 2;
}
}
}
}
}
printf("Simulation %d:\n", simLoop);
printf("The Coronets are down by %d points.\n", toWin - 1 );
printf("The minimum number of times that they need to score is %d.\n", minScores);
}
return 0;
}
Solution:
/*
Tropical Trouble
Nick Muguira
*/
#include
int main ()
{
int capacity, current = 0, evap, hours, added = 0, collected = 0;
int sims, simLoop;
int days, dayLoop;
scanf(" %d", &sims);
for ( simLoop = 1; simLoop <= sims; simLoop++)
{
current = added = collected = 0;
scanf(" %d %d", &capacity, &days);
for( dayLoop = 0; dayLoop < days; dayLoop++)
{
scanf(" %d %d %d", &added, &hours, &evap);
current += added;
if ( current > capacity )
{
current = capacity;
}
if ( hours * evap > current )
{
collected += current;
current = 0;
}
else
{
current -= hours * evap;
collected += hours * evap;
}
}
printf("Simulation %d: The village produced %d gallons of fresh water.\n", simLoop, collected);
}
return 0;
}
Solution:
/*
Bandwidth of the Future
Vy Nguyen
*/
#include
#include
const int flatfee=2500;
const int first_extra_fee= 60*5;
const int second_extra_fee=280*15;
const int third_extra_fee=600*40;
const int final_flat_fee=32000;
int main()
{
int num_of_month, totalblocks, tempblock;
int num_of_blocks=0;
int totalfee=0;
cin>>num_of_month;
for (int counter=1; counter<=num_of_month; counter++)
{ cin>>num_of_blocks;
totalblocks=0;
if (num_of_blocks>0 && num_of_blocks<=100)
{ for (int counter2=1; counter2<=num_of_blocks; counter2++)
{ cin>>tempblock;
totalblocks=totalblocks+tempblock;
}
}
if (totalblocks > 1000)
totalfee=final_flat_fee;
else
if (totalblocks > 400)
totalfee=flatfee + first_extra_fee + second_extra_fee +
(totalblocks-400)*40;
else
if (totalblocks>120)
totalfee=flatfee + first_extra_fee +
(totalblocks-120)*15;
else
if (totalblocks>60)
totalfee=flatfee + (totalblocks-60)*5;
else
totalfee=flatfee;
cout<<"Month "<
Solution:
/*
Umbrella Bashing
Isaac Traxler
*/
#include
#include
int main(){
int i;
int count;
int hit_points;
int damage;
int strikes;
scanf("%d", &count);
for(i=0;i 0) && (strikes > 0))
{ /* while */
if (damage >= hit_points)
hit_points = 0;
else
hit_points = hit_points - ((damage + 1) / 2);
strikes--;
} /* while */
if (hit_points > 0)
printf("Interview %d: The umbrella will remain intact.\n", i);
else
printf("Interview %d: The umbrella will break.\n", i);
}
return 0;
}
Solution:
/*
Accounting Blues
Phil Bordelon
*/
#include
#include
#include
#define INFILE stdin
#define OUTFILE stdout
/*
* We're going to set 'sammy' as a #define up here, just in case we want to use
* this code for some /other/ project.
*/
#define USERNAME "sammy"
/*
* We're also going to have a convenient constant length for strings,
* to make sure we have enough room. Since its an arbitrarily long value . . .
*/
#define ALV 999
int main (void)
{
int name_count; /* The number of names that we're going to test. */
int name_loop; /* Loop control for the names. */
char read_name [ALV]; /* The name read in from the file. */
char cleaned_name [ALV]; /* The name after clean-up. */
int clean_count; /* Count of characters in the cleaned string. */
int clean_loop; /* Loop control for the clean loop. */
FILE * infile; /* Input file. */
FILE * outfile; /* Output file. */
/*
* Since we're not doing file IO, we're going to simply set these to stdin and
* stdout.
*/
infile = INFILE;
outfile = OUTFILE;
/* Get the number of iterations. */
fscanf (infile, " %d", &name_count);
/* Iterate! */
for (name_loop = 1; name_loop <= name_count; name_loop ++)
{
/* Read the name in. */
fscanf (infile, " %s", &read_name);
/*
* Now we're going to clean the string up. The method of doing this is simple.
* We're going to take each character in the string and throw it away if it's
* not a number or letter. We're also going to lowercase all of the
* characters, so that case becomes irrelevant. We'll insert these changed
* characters into cleaned_name, and then we'll use that to see if the name we
* want is in there. To do this, we need to start our cleaned string's count at
* 0, the first location in the string.
*/
clean_count = 0;
for (clean_loop = 0; clean_loop < strlen (read_name);
clean_loop ++)
{
if (isalnum (read_name[clean_loop])) /* Alphabetic? */
{
cleaned_name[clean_count] = tolower
(read_name[clean_loop]);
clean_count ++;
}
}
/* Add a null character to the end of the cleaned-up string. */
cleaned_name[clean_count] = '\0';
/*
* Now, for the magic--if the name we want is a substring of the cleaned
* string, we cannot delete that account. If it isn't, we can.
*/
if (NULL == strstr (cleaned_name, USERNAME)) /* Not there. */
{
fprintf (outfile, "Name %d: Deleted: %s\n", name_loop,
read_name);
}
else /* It's in there! */
{
fprintf (outfile, "Name %d: Not Deleted: %s\n",
name_loop, read_name);
}
}
return (0);
}
Solution:
/*
Tropical Trouble
Shovon Nazmus
*/
# include
int main()
{
int watergained = 0;
int i=0,
basin,
days,
j=0,
water,
sun,
evap,
sim,
totwater=0,
gain = 0;
FILE * in;
in = stdin;
fscanf(in, "%d", &sim);
for (i=0; ibasin)
totwater = basin;
gain = sun * evap;
if (gain
Solution:
/*
Colour is the key
John Vernon
*/
#include
#define DEBUG (0 == 1)
void printpicture(char c[15])
{ /* function printpicture */
printf(" %c %c\n",c[2],c[9]);
printf(" %c %c %c\n",c[1],c[3], c[10]);
printf("%c %c %c %c\n",c[0],c[8], c[4], c[11]);
printf(" %c %c %c\n",c[7],c[5], c[12]);
printf(" %c %c\n",c[6],c[13]);
printf("\n");
} /* function printpicture */
int main()
{
int simulations, simloop, loop, rotations;
char colours[15];
char buffer[20];
char ring, direction, temp;
fscanf( stdin, "%d\n", &simulations );
for( simloop = 0; simloop < simulations; ++simloop )
{
printf("Simulation %d:\n", simloop + 1 );
for( loop = 0; loop < 14; ++loop )
{
colours[loop] = getchar();
fgets( buffer, 20, stdin );
}
fscanf( stdin, "%d\n", &rotations );
for( loop = 0; loop < rotations; ++loop )
{
if (DEBUG) printpicture(colours);
fscanf( stdin, "%c ", &ring );
fscanf( stdin, "%c ", &direction );
if( ring == 'L' )
{
if( direction == 'W' )
{
temp = colours[0];
colours[0] = colours[1];
colours[1] = colours[2];
colours[2] = colours[3];
colours[3] = colours[4];
colours[4] = colours[5];
colours[5] = colours[6];
colours[6] = colours[7];
colours[7] = temp;
}
if( direction == 'D' )
{
temp = colours[7];
colours[7] = colours[6];
colours[6] = colours[5];
colours[5] = colours[4];
colours[4] = colours[3];
colours[3] = colours[2];
colours[2] = colours[1];
colours[1] = colours[0];
colours[0] = temp;
}
}
if( ring == 'R' )
{
if( direction == 'W' )
{
temp = colours[8];
colours[8] = colours[3];
colours[3] = colours[9];
colours[9] = colours[10];
colours[10] = colours[11];
colours[11] = colours[12];
colours[12] = colours[13];
colours[13] = colours[5];
colours[5] = temp;
}
if( direction == 'D' )
{
temp = colours[13];
colours[13] = colours[12];
colours[12] = colours[11];
colours[11] = colours[10];
colours[10] = colours[9];
colours[9] = colours[3];
colours[3] = colours[8];
colours[8] = colours[5];
colours[5] = temp;
}
}
if (DEBUG) printf("Command: %c %c\n", ring, direction);
}
if (DEBUG) printpicture(colours);
for( loop = 0; loop < 14; ++loop )
{
if( colours[loop] == 'r' )
{
printf("red\n");
}
if( colours[loop] == 'g' )
{
printf("green\n");
}
if( colours[loop] == 'b' )
{
printf("blue\n");
}
if( colours[loop] == 'y' )
{
printf("yellow\n");
}
if( colours[loop] == 'p' )
{
printf("purple\n");
}
}
}
return 0;
}
Solution:
/*
Family Ties
Charles M Huang
*/
#include
#include
int main(){
int i, counter;
char temp, mom1, mom2, dad1, dad2, kid1, kid2;
FILE *inp;
inp=stdin;
fscanf(inp, "%d", &counter);
for(i=0;i
Solution:
/*
Wherehouse?
Nick Muguira
*/
#include
int main ()
{
int dimsLoop, convertLoop, numDim, numToConvert, toConvert, dims[5];
int numJobs, jobLoop;
scanf(" %d", &numJobs);
for ( jobLoop = 1; jobLoop <= numJobs; jobLoop++ )
{
printf("Job %d:\n", jobLoop);
dims[0] = dims[1] = dims[2] = dims[3] = dims[4] = 0;
scanf("%d", &numDim);
for ( dimsLoop = 0; dimsLoop < numDim; dimsLoop++ )
{
scanf("%d", &dims[numDim - dimsLoop - 1]);
}
dims[4] = dims[3] * dims[2] * dims[1] * dims[0];
dims[3] = dims[2] * dims[1] * dims[0];
dims[2] = dims[1] * dims[0];
dims[1] = dims[0];
scanf("%d", &numToConvert);
for ( convertLoop = 0; convertLoop < numToConvert; convertLoop++ )
{
scanf("%d", &toConvert);
printf("%d -> [", toConvert);
toConvert--;
for ( dimsLoop = 0; dimsLoop < numDim - 1; dimsLoop++ )
{
printf("%d%c", ( toConvert / dims[ numDim - dimsLoop - 1] + 1 ), ',');
toConvert = toConvert % dims[ numDim - dimsLoop - 1];
}
printf("%d]\n", 1 + toConvert);
}
dims[0] = dims[1] = dims[2] = dims[3] = dims[4] = 0;
}
return 0;
}
Solution:
/* C source file */
/*
Super Vinnie
Nick Muguira
*/
#include
#include
int main ()
{
int numCases, str, coins, pos, iLength;
char map[11], action[6];
int printCount;
scanf(" %d", &numCases);
printCount = 1;
for ( ; numCases > 0; numCases-- )
{
scanf(" %d %s %s", &str, map, action);
iLength = strlen( action );
coins = 0;
for ( pos = 1; pos / 2 < iLength && str; pos += 2 )
{
if ( map[pos] == 'E' && action[ pos / 2 ] == 'B' )
{
if ( str < 3 )
{
str--;
}
}
else if ( map[pos] == 'C' && action[ pos / 2 ] == 'A' )
{
coins++;
}
}
for ( ; str && pos < 10; pos += 2 )
{
if ( map[pos] == 'E' )
{
str--;
}
}
printf ("Level %d:\n", printCount);
printf ("%s\n", (str) ? "SUCCESS" : "DEATH");
(str) ? printf ("Health: %d\n", str) : printf ("Location: %d\n", pos - 1);
printf ("Coins: %d\n", coins);
printCount ++;
}
return 0;
}
Solution:
/*
Accounting Blues
Phil Bordelon
*/
#include
#include
#include
#define INFILE stdin
#define OUTFILE stdout
/*
* We're going to set 'sammy' as a #define up here, just in case we want to use
* this code for some /other/ project.
*/
#define USERNAME "sammy"
/*
* We're also going to have a convenient constant length for strings,
* to make sure we have enough room. Since its an arbitrarily long value . . .
*/
#define ALV 999
int main (void)
{
int name_count; /* The number of names that we're going to test. */
int name_loop; /* Loop control for the names. */
char read_name [ALV]; /* The name read in from the file. */
char cleaned_name [ALV]; /* The name after clean-up. */
int clean_count; /* Count of characters in the cleaned string. */
int clean_loop; /* Loop control for the clean loop. */
FILE * infile; /* Input file. */
FILE * outfile; /* Output file. */
/*
* Since we're not doing file IO, we're going to simply set these to stdin and
* stdout.
*/
infile = INFILE;
outfile = OUTFILE;
/* Get the number of iterations. */
fscanf (infile, " %d", &name_count);
/* Iterate! */
for (name_loop = 1; name_loop <= name_count; name_loop ++)
{
/* Read the name in. */
fscanf (infile, " %s", &read_name);
/*
* Now we're going to clean the string up. The method of doing this is simple.
* We're going to take each character in the string and throw it away if it's
* not a number or letter. We're also going to lowercase all of the
* characters, so that case becomes irrelevant. We'll insert these changed
* characters into cleaned_name, and then we'll use that to see if the name we
* want is in there. To do this, we need to start our cleaned string's count at
* 0, the first location in the string.
*/
clean_count = 0;
for (clean_loop = 0; clean_loop < strlen (read_name);
clean_loop ++)
{
if (isalnum (read_name[clean_loop])) /* Alphabetic? */
{
cleaned_name[clean_count] = tolower
(read_name[clean_loop]);
clean_count ++;
}
}
/* Add a null character to the end of the cleaned-up string. */
cleaned_name[clean_count] = '\0';
/*
* Now, for the magic--if the name we want is a substring of the cleaned
* string, we cannot delete that account. If it isn't, we can.
*/
if (NULL == strstr (cleaned_name, USERNAME)) /* Not there. */
{
fprintf (outfile, "Name %d: Deleted: %s\n", name_loop,
read_name);
}
else /* It's in there! */
{
fprintf (outfile, "Name %d: Not Deleted: %s\n",
name_loop, read_name);
}
}
return (0);
}
Solution:
/*
Bandwidth of the Future
Shovon Nazmus
*/
# include
int main()
{
signed int total = 0;
int line1=0,
i = 0,
j = 0,
blocks = 0,
block1 = 0,
tot=0;
FILE * in;
in = stdin;
fscanf (in, "%d", &line1);
for (i=0;i
Solution:
/*
Colour is the Key
Nick Muguira
*/
#include
#include
int main ()
{
int numCases, caseLoop, numTurns, readLoop, temp;
int indexes[14];
char stones[14][7];
char wheel, dir;
scanf(" %d", &numCases);
for ( caseLoop = 1; caseLoop <= numCases; caseLoop++ )
{
for ( readLoop = 0; readLoop < 14; readLoop++ )
{
scanf(" %s", stones[readLoop]);
stones[readLoop][strlen(stones[readLoop])] = '\0';
indexes[readLoop] = readLoop;
}
scanf(" %d", &numTurns);
for ( ; numTurns > 0; numTurns-- )
{
scanf(" %c %c", &wheel, &dir);
if ( wheel == 'L' )
{
temp = indexes[0];
indexes[0] = indexes[ (dir == 'W') ? 1 : 7 ];
indexes[ (dir == 'W') ? 1 : 7] = indexes[ (dir == 'W') ? 2 : 6 ];
indexes[ (dir == 'W') ? 2 : 6] = indexes[ (dir == 'W') ? 3 : 5 ];
indexes[ (dir == 'W') ? 3 : 5] = indexes[4];
indexes[4] = indexes[ (dir == 'W') ? 5 : 3];
indexes[ (dir == 'W') ? 5 : 3] = indexes[ (dir == 'W') ? 6 : 2];
indexes[ (dir == 'W') ? 6 : 2] = indexes[ (dir == 'W') ? 7 : 1];
indexes[ (dir == 'W') ? 7 : 1] = temp;
}
else
{
temp = indexes[8];
indexes[8] = indexes[ (dir == 'W') ? 3 : 5 ];
indexes[ (dir == 'W') ? 3 : 5] = indexes[ (dir == 'W') ? 9 : 13];
indexes[ (dir == 'W') ? 9 : 13] = indexes[ (dir == 'W') ? 10: 12];
indexes[ (dir == 'W') ? 10 : 12] = indexes[11];
indexes[11] = indexes[ (dir == 'W') ? 12 : 10];
indexes[ (dir == 'W') ? 12 : 10] = indexes[ (dir == 'W') ? 13 : 9];
indexes[ (dir == 'W') ? 13 : 9] = indexes[ (dir == 'W') ? 5 : 3];
indexes[ (dir == 'W') ? 5 : 3] = temp;
}
}
printf("Simulation %d:\n", caseLoop);
for ( readLoop = 0; readLoop < 14; readLoop++ )
{
printf("%s\n", stones[indexes[readLoop]]);
}
}
return 0;
}
Solution:
/*
Bandwidth of the Future
Nick Muguira
*/
#include
int main ()
{
int numCust, custLoop, numBlocks, blockLoop, price, consumed, temp;
scanf("%d", &numCust);
for( custLoop = 0; custLoop < numCust; custLoop++ )
{
consumed = 0;
scanf("%d", &numBlocks);
for ( blockLoop = 0; blockLoop < numBlocks; blockLoop++ )
{
scanf("%d", &temp);
consumed += temp;
}
price = 2500;
if ( consumed >= 400 )
{
price += ( consumed - 400 ) * 25;
}
if ( consumed >= 120 )
{
price += ( consumed - 120 ) * 10;
}
if ( consumed >= 60 )
{
price += ( consumed - 60 ) * 5;
}
if ( consumed >= 1000 )
{
price = 32000;
}
printf("Month %d: %d One World Credits\n", custLoop + 1, price);
}
return 0;
}
Solution:
/*
Fifty-Fifty
Nick Muguira
*/
#include
int main ( void )
{
char cur, prev;
int numStrings, sLoop, numC;
int heads, tails, alt;
scanf(" %d", &numStrings);
for ( sLoop = 1; sLoop <= numStrings; sLoop++ )
{
heads = tails = alt = 0;
cur = ' ';
scanf(" %d", &numC);
for ( ; numC > 0; numC-- )
{
prev = cur;
scanf(" %c", &cur);
if ( cur == 'H' )
{
heads++;
}
else
{
tails++;
}
if ( ( cur - prev ) == ( 'H' - 'T' ) || ( cur - prev ) == ( 'T' - 'H' ) )
{
alt++;
}
}
printf("Series %d: ", sLoop);
if ( heads > tails && heads > alt )
{
printf("All Heads\n");
}
else if( tails > heads && tails > alt )
{
printf("All Tails\n");
}
else if ( alt > heads && alt > tails )
{
printf("Alternating\n");
}
else
{
printf("Indeterminate\n");
}
}
return 0;
}
Solution:
/*
Fleeing, Floating, Flying
Phil Bordelon.
*/
#include
#define INFILE stdin
#define OUTFILE stdout
#define UNKNOWN 0
#define MADE_IT 1
#define STUCK 2
int main (void)
{
int sim_count; /* The number of simulations that we are running. */
int barge_count; /* The number of barges for the current simulation. */
int helipad_loc; /* The location of the helipad. */
int curr_loc; /* Current location we're reading from. */
int curr_height; /* Sam's current height. */
int read_height; /* Read-in height. */
int sim_loop; /* Loop control for the simulations. */
int status; /* Sam's status. */
FILE * infile; /* Input file. */
FILE * outfile; /* Output file. */
/* Since we're using stdin and stdout, no fancy file IO is needed. */
infile = INFILE;
outfile = OUTFILE;
/* Read in the number of simulations to do. */
fscanf (infile, " %d", &sim_count);
/* Now, we have the main loop for the simulations. */
for (sim_loop = 1; sim_loop <= sim_count; sim_loop ++)
{
/* First, get the number of barges for the simulation. */
fscanf (infile, " %d", &barge_count);
/* Set Sam's starting location; we know he starts at barge 1. */
curr_loc = 1;
/* Finally, get the location of the helipad and Sam's starting height. */
fscanf (infile, " %d", &helipad_loc);
fscanf (infile, " %d", &curr_height);
/*
* Set his status to unknown. He will become stuck or escape before the
* end of the barges, since all input is valid.
*/
status = UNKNOWN;
/* Now, loop until he escapes or gets stuck. */
while (UNKNOWN == status)
{
if (helipad_loc == curr_loc) /* Sam made it! */
{
status = MADE_IT;
}
else /* We haven't made it yet; try to move forward. */
{
/*
* Get the next height in the file and increment the current location;
* even if Sam can't make it into the square, we need to make sure that we
* read the correct number of barge heights after he gets stuck or escapes
* to prepare for the next simulation.
*/
fscanf (infile, " %d", &read_height);
curr_loc ++;
/* Now, either he's stuck or he successfully moves forward. */
if ((curr_height - read_height <= 4) &&
(read_height - curr_height <= 1))
{
curr_height = read_height;
}
else /* Stuck. */
{
status = STUCK;
}
}
}
/*
* We have Sam's status, so we know what we need to print. But we need to
* clear out any barge heights past the helipad (or where we got stuck).
* We'll use curr_loc for this.
*/
while (curr_loc < barge_count)
{
fscanf (infile, " %d", &read_height);
curr_loc ++;
}
/* Now, print the proper output. */
fprintf (outfile, "Simulation %d: ", sim_loop);
if (MADE_IT == status) /* Made it! */
{
fprintf (outfile, "Sam made it to the helipad!\n");
}
else /* Stuck. */
{
fprintf (outfile, "Sam got caught by the crew!\n");
}
}
return (0);
}
Solution:
/*
Tropical Trouble
Shovon Nazmus
*/
# include
int main()
{
int watergained = 0;
int i=0,
basin,
days,
j=0,
water,
sun,
evap,
sim,
totwater=0,
gain = 0;
FILE * in;
in = stdin;
fscanf(in, "%d", &sim);
for (i=0; ibasin)
totwater = basin;
gain = sun * evap;
if (gain
Solution:
/*
Wherehouse?
Nick Muguira
*/
#include
int main ()
{
int dimsLoop, convertLoop, numDim, numToConvert, toConvert, dims[5];
int numJobs, jobLoop;
scanf(" %d", &numJobs);
for ( jobLoop = 1; jobLoop <= numJobs; jobLoop++ )
{
printf("Job %d:\n", jobLoop);
dims[0] = dims[1] = dims[2] = dims[3] = dims[4] = 0;
scanf("%d", &numDim);
for ( dimsLoop = 0; dimsLoop < numDim; dimsLoop++ )
{
scanf("%d", &dims[numDim - dimsLoop - 1]);
}
dims[4] = dims[3] * dims[2] * dims[1] * dims[0];
dims[3] = dims[2] * dims[1] * dims[0];
dims[2] = dims[1] * dims[0];
dims[1] = dims[0];
scanf("%d", &numToConvert);
for ( convertLoop = 0; convertLoop < numToConvert; convertLoop++ )
{
scanf("%d", &toConvert);
printf("%d -> [", toConvert);
toConvert--;
for ( dimsLoop = 0; dimsLoop < numDim - 1; dimsLoop++ )
{
printf("%d%c", ( toConvert / dims[ numDim - dimsLoop - 1] + 1 ), ',');
toConvert = toConvert % dims[ numDim - dimsLoop - 1];
}
printf("%d]\n", 1 + toConvert);
}
dims[0] = dims[1] = dims[2] = dims[3] = dims[4] = 0;
}
return 0;
}
Solution:
/*
Bandwidth of the Future
Vy Nguyen
*/
#include
#include
const int flatfee=2500;
const int first_extra_fee= 60*5;
const int second_extra_fee=280*15;
const int third_extra_fee=600*40;
const int final_flat_fee=32000;
int main(void)
{
int num_of_month, counter, counter2, totalblocks, tempblock;
int num_of_blocks=0;
int totalfee=0;
scanf ("%d", &num_of_month);
for (counter=1; counter<=num_of_month; counter++)
{ scanf ("%d", &num_of_blocks);
totalblocks=0;
if (num_of_blocks>0 && num_of_blocks<=100)
{ for (counter2=1; counter2<=num_of_blocks; counter2++)
{ scanf ("%d", &tempblock);
totalblocks=totalblocks+tempblock;
}
}
if (totalblocks > 1000)
totalfee=final_flat_fee;
else
if (totalblocks > 400)
totalfee=flatfee + first_extra_fee + second_extra_fee +
(totalblocks-400)*40;
else
if (totalblocks>120)
totalfee=flatfee + first_extra_fee +
(totalblocks-120)*15;
else
if (totalblocks>60)
totalfee=flatfee + (totalblocks-60)*5;
else
totalfee=flatfee;
printf ("Month %d: %d One World Credits\n", counter, totalfee);
}
return 0;
}
Solution:
/*
Tropical Trouble
Nick Muguira
*/
#include
int main ()
{
int capacity, current = 0, evap, hours, added = 0, collected = 0;
int sims, simLoop;
int days, dayLoop;
scanf(" %d", &sims);
for ( simLoop = 1; simLoop <= sims; simLoop++)
{
current = added = collected = 0;
scanf(" %d %d", &capacity, &days);
for( dayLoop = 0; dayLoop < days; dayLoop++)
{
scanf(" %d %d %d", &added, &hours, &evap);
current += added;
if ( current > capacity )
{
current = capacity;
}
if ( hours * evap > current )
{
collected += current;
current = 0;
}
else
{
current -= hours * evap;
collected += hours * evap;
}
}
printf("Simulation %d: The village produced %d gallons of fresh water.\n", simLoop, collected);
}
return 0;
}
Solution:
/*
Family Ties
Nick Muguira
*/
#include
int main ( void )
{
char mom[3], dad[3], kid[3];
int cases, caseLoop;
scanf(" %d", &cases);
for ( caseLoop = 1; caseLoop <= cases; caseLoop++ )
{
scanf(" %s %s %s", mom, dad, kid);
if ( ( ( mom[0] == kid[0] && dad[0] == kid[1] ) || ( mom[0] == kid[0] && dad[1] == kid[1] ) )
|| ( ( mom[1] == kid[0] && dad[0] == kid[1] ) || ( mom[1] == kid[0] && dad[1] == kid[1] ) )
|| ( ( mom[0] == kid[1] && dad[0] == kid[0] ) || ( mom[0] == kid[1] && dad[1] == kid[0] ) )
|| ( ( mom[1] == kid[1] && dad[0] == kid[0] ) || ( mom[1] == kid[1] && dad[1] == kid[0] ) ) )
{
printf("Set %d: This could be their child.\n", caseLoop);
}
else
{
printf("Set %d: This cannot be their child.\n", caseLoop);
}
}
return 0;
}
Solution:
/*
Tropical Trouble
Nick Muguira
*/
#include
int main ()
{
int capacity, current = 0, evap, hours, added = 0, collected = 0;
int sims, simLoop;
int days, dayLoop;
scanf(" %d", &sims);
for ( simLoop = 1; simLoop <= sims; simLoop++)
{
current = added = collected = 0;
scanf(" %d %d", &capacity, &days);
for( dayLoop = 0; dayLoop < days; dayLoop++)
{
scanf(" %d %d %d", &added, &hours, &evap);
current += added;
if ( current > capacity )
{
current = capacity;
}
if ( hours * evap > current )
{
collected += current;
current = 0;
}
else
{
current -= hours * evap;
collected += hours * evap;
}
}
printf("Simulation %d: The village produced %d gallons of fresh water.\n", simLoop, collected);
}
return 0;
}
Solution:
/*
Tropical Trouble
Shovon Nazmus
*/
# include
int main()
{
int watergained = 0;
int i=0,
basin,
days,
j=0,
water,
sun,
evap,
sim,
totwater=0,
gain = 0;
FILE * in;
in = stdin;
fscanf(in, "%d", &sim);
for (i=0; ibasin)
totwater = basin;
gain = sun * evap;
if (gain
Solution:
/*
Accounting Blues
Phil Bordelon
*/
#include
#include
#include
#define INFILE stdin
#define OUTFILE stdout
/*
* We're going to set 'sammy' as a #define up here, just in case we want to use
* this code for some /other/ project.
*/
#define USERNAME "sammy"
/*
* We're also going to have a convenient constant length for strings,
* to make sure we have enough room. Since its an arbitrarily long value . . .
*/
#define ALV 999
int main (void)
{
int name_count; /* The number of names that we're going to test. */
int name_loop; /* Loop control for the names. */
char read_name [ALV]; /* The name read in from the file. */
char cleaned_name [ALV]; /* The name after clean-up. */
int clean_count; /* Count of characters in the cleaned string. */
int clean_loop; /* Loop control for the clean loop. */
FILE * infile; /* Input file. */
FILE * outfile; /* Output file. */
/*
* Since we're not doing file IO, we're going to simply set these to stdin and
* stdout.
*/
infile = INFILE;
outfile = OUTFILE;
/* Get the number of iterations. */
fscanf (infile, " %d", &name_count);
/* Iterate! */
for (name_loop = 1; name_loop <= name_count; name_loop ++)
{
/* Read the name in. */
fscanf (infile, " %s", &read_name);
/*
* Now we're going to clean the string up. The method of doing this is simple.
* We're going to take each character in the string and throw it away if it's
* not a number or letter. We're also going to lowercase all of the
* characters, so that case becomes irrelevant. We'll insert these changed
* characters into cleaned_name, and then we'll use that to see if the name we
* want is in there. To do this, we need to start our cleaned string's count at
* 0, the first location in the string.
*/
clean_count = 0;
for (clean_loop = 0; clean_loop < strlen (read_name);
clean_loop ++)
{
if (isalnum (read_name[clean_loop])) /* Alphabetic? */
{
cleaned_name[clean_count] = tolower
(read_name[clean_loop]);
clean_count ++;
}
}
/* Add a null character to the end of the cleaned-up string. */
cleaned_name[clean_count] = '\0';
/*
* Now, for the magic--if the name we want is a substring of the cleaned
* string, we cannot delete that account. If it isn't, we can.
*/
if (NULL == strstr (cleaned_name, USERNAME)) /* Not there. */
{
fprintf (outfile, "Name %d: Deleted: %s\n", name_loop,
read_name);
}
else /* It's in there! */
{
fprintf (outfile, "Name %d: Not Deleted: %s\n",
name_loop, read_name);
}
}
return (0);
}
Return to Problem Index