Professional Documents
Culture Documents
Apl3 3
Apl3 3
2. Objective:
Task 1: Marc's Cakewalk - Marc loves cupcakes, but he also likes to stay fit. Each
cupcake has a calorie count, and Marc can walk a distance to expend those calories. If
Marc has eaten j cupcakes so far, after eating a cupcake with c calories he must walk
at least 2j * c miles to maintain his weight.
Task 2: Grid Challenge - Given a square grid of characters in the range ascii[a-
z],rearrange elements of each row alphabetically, ascending. Determine if the
columns are also in ascending alphabetical order, top to bottom. Return YES if they
are or NO if they are not.
sort(calorie.rbegin(), calorie.rend());
long miles = 0;
long calorieFactor = 1;
for (int i = 0; i < calorie.size(); i++)
{
miles += calorie[i] * calorieFactor;
calorieFactor *= 2;
}
return miles;
int main()
{
int n;
cin >> n;
vector<int> calorie(n);
for (int i = 0; i < n; i++)
{
cin >> calorie[i];
}
long result = marcsCakewalk(calorie);
cout << result << endl;
return 0;
}
Output:
Task 2: Grid Challenge
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
string gridChallenge(vector<string> grid)
{
int n = grid.size();
int m = grid[0].size();
for (int i = 0; i < n; i++)
{
sort(grid[i].begin(), grid[i].end());
}
for (int j = 0; j < m; j++)
{
for (int i = 0; i < n - 1; i++)
{
if (grid[i][j] > grid[i + 1][j])
{
return "NO";
}
}
}
return "YES";
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n; // Number of rows
vector<string> grid(n);
for (int i = 0; i < n; i++)
{
cin >> grid[i];
}
string result = gridChallenge(grid);
cout << result << endl;
}
return 0;
}
Output: