Solutions, SRU 2003 High School Programming Competition


First, you might want to look at the problem set.

Warning!

Some browsers are not fully honoring the "pre" tag, so C++ code such as #includes and functions after ">" is corrupted. To be sure of seeing the correct code, view this document as source. (at least until I have the time to reformat things.)

  1. Quadratic


    //Find coordinates of a solution point of a quadratic equation
    
    #include    //needed for cin, cout.
    
    int main()
    {
       double a, b, c, x, y;
    
       cin >> a >> b >> c >> x;
       while (cin){
          y = a * x * x + b * x + c;
          cout << "(" << x << ", " << y << ")\n";
          cin >> a >> b >> c >> x;
       }
    
       return 0;
    }
    
      

  2. Vicksburg Cipher


    //Vicksburg Cipher program
    
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
       char table[26][26];
       const int ENCRYPT=1, DECRYPT=2;
       int mode;
       char key[81], plaintext[81], ciphertext[81], ch;
    
       for (int i=0; i<26; i++)
          for (int j=0,k=i; j<26; j++, k++)
             table[i][j] = 'A' + k % 26;
    
       cin >> mode;
       while (mode == ENCRYPT || mode == DECRYPT){
          do {ch = cin.get();} while (ch!='\n');
          cin.getline(key, 80, '\n');
          for (int i=0; i= 'A' && key[i] <= 'Z')
                   key[j++] = key[i++];
                else
                   i++;
             }
             key[j] = 0;
          }
          switch (mode){
             case ENCRYPT:
                cin.getline(plaintext, 80, '\n');
    	    for (int i=0; i= 'A' && plaintext[i] <= 'Z')
                         ch = table[plaintext[i]-'A'][key[j++]-'A'];
                      else if (plaintext[i] == ' ')
                         ch = ' ';
                      else {
                         ch = plaintext[i];
                         j = 0;
                      }
                      j %= strlen(key);
                      cout << ch;
                   }
                   cout << endl;
                }
                break;
             case DECRYPT:
                cin.getline(ciphertext, 80, '\n');
                {
                   int i , j, row, col;
                   for (i=0,j=0; i= 'A' && ciphertext[i] <= 'Z'){
                         col = key[j++] - 'A';
                         row = 0;
                         while (table[row][col] != ciphertext[i])
                            row++;
                         ch = row + 'A';
                      } else if (ciphertext[i] == ' ')
                         ch = ' ';
                      else {
                         ch = ciphertext[i];
                         j = 0;
                      }
                      j %= strlen(key);
                      cout << ch;
                   }
                   cout << endl;
                }
          }
          cin >> mode;
       }
       return 0;
    }
    
    
      
  3. Letter Count

    //Letter Count
    
    #include   //Needed for cin, cout.
    #include     //Needed for strlen().
    
    void sort(const char[3][80], char[3][80]);
    
    int main()
    {
       const int TRUE = 1, FALSE = 0;
       char word[3][80], letters[3][80], ch;
       int histo[3][26], done = FALSE, max[3], max_ct[3];
    
       while (!done){
          for (int i=0, row=0, col=0; ; i++){
             ch = cin.get();
             if (!cin){
                done = TRUE;
                break;
             }
             if (ch != ' ' && ch != '\n')
                word[row][col++] = ch;
             else {
                word[row++][col] = 0;
                col = 0;
                if (row > 2)
                   break;
             }
          }
          if (done) break;
          sort(word, letters);
          for (int i=0; i<3; i++)
             for (int j=0; j<26; j++)
                histo[i][j] = 0;
          for (int i=0; i<3; i++)
             for (int j=0; j histo[i][max[i]]){ //new max
                   max[i] = j;
                   max_ct[i] = 1;
                }
          }
    
          if (histo[0][max[0]] > histo[1][max[1]] &&
              histo[0][max[0]] > histo[2][max[2]] &&
              max_ct[0] == 1){
             cout << word[0] << ' ' << histo[0][max[0]] << ' '
                  << (char)(max[0] + 'a') << endl;
             continue;
          }
          if (histo[1][max[1]] > histo[2][max[2]] &&
              histo[1][max[1]] > histo[0][max[0]] &&
              max_ct[1] == 1){
             cout << word[1] << ' ' << histo[1][max[1]]
                  << ' ' << (char)(max[1] + 'a') << endl;
             continue;
          }
          if (histo[2][max[2]] > histo[1][max[1]] &&
              histo[2][max[2]] > histo[0][max[0]] &&
              max_ct[2] == 1){
             cout << word[2] << ' ' << histo[2][max[2]]
                  << ' ' << (char)(max[2] + 'a') << endl;
             continue;
          }
          cout << "tie\n";
    
       }
       return 0;//Letter Count
    
    
      
  4. Aibohphobia


    #include   //Needed for cin, cout.
    #include     //Needed for strlen().
    
    
    int main()
    {
       const int TRUE = 1, FALSE = 0;
       int palindrome;
       char line[81], newline[81];
    
       while (1){
          cin.getline(line, 80);
          if (!cin)
             break;
          {int j = 0;
          for (int i=0; i= 'A' && line[i] <= 'Z')
                newline[j++] = line[i];
          }
          newline[j] = 0;
          }
    
          palindrome = TRUE;
          for (int i=0, j=strlen(newline)-1; j-i>1; i++,j--)
             if (newline[i] != newline[j]){
                palindrome = FALSE;
                break;
             }
          if (palindrome)
             cout << "palindrome\n";
          else
             cout << "not a palindrome\n";
       }
    }
    
    
    void sort(const char word[3][80], char letters[3][80])
    {
       char temp;
       int min;
    
       for (int row=0; row<3; row++){
          for (int i=0; i<=strlen(word[row]); i++)
             letters[row][i] = word[row][i];
          for (int pass=0; pass< strlen(letters[row])-1; pass++){
             min = pass;
             for (int i=pass; i
  5. Pyramids

    //Egyptia Pyramid Problem
    
    #include 
    #include 
    #include 
    #include 
    
    int main(){
    
       int area, height, s_block_ct, d_block_ct, top_side, bot_side;
    
       cin >> area >> height;
       cout << "  Area     Height    No. of Double Blocks    No. of Single Blocks"
            << endl;
       while (cin){
          top_side = (int) sqrt(area);
          bot_side = top_side + height - 1;
          if (top_side % 2 == 1 && height % 2 == 1)
             s_block_ct = height / 2 + 1;
          else
             s_block_ct = height / 2;
          d_block_ct = area / 2;
          for (int side=top_side+1; side<=bot_side; side++)
             d_block_ct += (side * side) / 2;
          cout << setw(5) << area << setw(10) << height << setw(17) << d_block_ct
               << setw(24) << s_block_ct << endl;
          cin >> area >> height;
       }
       return EXIT_SUCCESS;
    }