Canadian Coding Competition(CCC) 2021 Junior 题解

目录

Problem J1: Boiling Water

Problem J2: Silent Auction

Problem J3: Secret Instructions

Problem J4: Arranging Books

Problem J5/S2: Modern Art


Problem J1: Boiling Water

Problem Description

At sea level, atmospheric pressure is 100 kPa and water begins to boil at 100◦C. As you go above sea level, atmospheric pressure decreases, and water boils at lower temperatures. As you go below sea level, atmospheric pressure increases, and water boils at higher temperatures. A formula relating atmospheric pressure to the temperature at which water begins to boil is

P = 5 × B − 400

where P is atmospheric pressure measured in kPa, and B is the temperature at which water begins to boil measured in ◦C. Given the temperature at which water begins to boil, determine atmospheric pressure. Also determine if you are below sea level, at sea level, or above sea level. Note that the science of this problem is generally correct but the values of 100◦C and 100 kPa are approximate and the formula is a simplification of the exact relationship between water’s boiling point and atmospheric pressure.

Input Specification

The input is one line containing an integer B where B ≥ 80 and B ≤ 200. This represents the temperature in ◦C at which water begins to boil.

Output Specification

The output is two lines. The first line must contain an integer which is atmospheric pressure measured in kPa. The second line must contain the integer -1, 0, or 1. This integer represents whether you are below sea level, at sea level, or above sea level, respectively.

这是一个非常简单的问题。让我们先了解问题,然后再进入解决方案。我们将得到水开始沸腾时的水温(也称为沸点)。通过使用问题描述中的公式,我们需要找出以 kPa 为单位的大气压。公式是:

𝑃 = 5 × 𝐵 − 400

使用公式后,我们必须打印 P 的值。

然后,根据以下条件,我们要判断我们是低于海平面、处于海平面还是高于海平面。

条件:

如果大气压小于 100 kPa,那么我们就在海平面以上。所以我们将打印 1

如果大气压力为 100 kPa,则我们处于海平面。所以我们将打印 0

如果大气压力大于 100,那么我们就在海平面以上。所以我们将打印 -1

所以,在开始编码之前,我们必须看一下输入和输出规范。请记住,我们需要以这样一种方式编写我们的代码,即我们可以完全按照给定的方式输入 B 的值。不需要提示消息。同样,代码的输出也必须与输出规范中所告知的完全一致。示例输出区分大小写,因此我们需要注意这一点。

流程图

 代码

#include <bits/stdc++.h>

using namespace std;

int main() {   
  int b;
  cin >> b; 
  int res = 5 * b - 400;
  cout << res << '\n';            
  cout << (res == 100 ? 0 : (res < 100 ? 1 : -1)) << '\n';
  return 0;
}

Problem J2: Silent Auction

Problem Description

A charity is having a silent auction where people place bids on a prize without knowing anyone else’s bid. Each bid includes a person’s name and the amount of their bid. After the silent auction is over, the winner is the person who has placed the highest bid. If there is a tie, the person whose bid was placed first wins. Your job is to determine the winner of the silent auction.

Input Specification

The first line of input contains a positive integer N, where 1 ≤ N ≤ 100, representing the number of bids collected at the silent auction. Each of the next N pairs of lines contains a person’s name on one line, and the amount of their bid, in dollars, on the next line. Each bid is a positive integer less than 2000. The order of the input is the order in which bids were placed.

Output Specification

Output the name of the person who has won the silent auction.

Sample Input 1

3

Ahmed

300

Suzanne

500

Ivona

450

Output for Sample Input 1

Suzanne

Explanation of Output for Sample Input 1

The highest bid placed was 500 and it was placed by Suzanne. Suzanne wins the silent auction.

在我们的第二个问题中,我们将得到一个正整数 N。这个 N 代表投标人的数量。然后,我们将得到 N 对包含姓名和他/她的出价的输入。我们的工作是打印最高出价者的名字。

首先,我们将获取 N 的输入。然后,我们将初始化一个名为 max_bid 的变量,该变量将被设置为 -1。接下来,我们将采用 for 循环,它将迭代 N 次。在 for 循环中,我们将分别输入我们的投标人姓名和他/她的出价。现在,我们将比较 current_bid 和我们的 max_bid 变量。如果当前出价者的出价高于之前的 max_bid,那么我们会将他/她的名字存储在 winner 变量中,并将 max_bid 设置为 current_bid 值。

最后,获胜者变量将包含出价最高者的姓名。因此,我们将打印 winner 变量的值。

流程图

代码

#include <bits/stdc++.h>

using namespace std;

int main() {   
  int n;
  cin >> n;
  vector<string> st(n);
  vector<int> val(n);
  for (int i = 0; i < n; i++) {
    cin >> st[i] >> val[i];
  }
  int p = *max_element(val.begin(), val.end());
  for (int i = 0; i < n; i++) {
    if (val[i] == p) {
      cout << st[i] << '\n';
      return 0;
    }
  }
  return 0;
}

Problem J3: Secret Instructions

Problem Description

Professor Santos has decided to hide a secret formula for a new type of biofuel. She has, however, left a sequence of coded instructions for her assistant. Each instruction is a sequence of five digits which represents a direction to turn and the number of steps to take. The first two digits represent the direction to turn: • If their sum is odd, then the direction to turn is left. • If their sum is even and not zero, then the direction to turn is right. • If their sum is zero, then the direction to turn is the same as the previous instruction. The remaining three digits represent the number of steps to take which will always be at least 100. Your job is to decode the instructions so the assistant can use them to find the secret formula.

Input Specification

There will be at least two lines of input. Each line except the last line will contain exactly five digits representing an instruction. The first line will not begin with 00. The last line will contain 99999 and no other line will contain 99999.

Output Specification

There must be one line of output for each line of input except the last line of input. These output lines correspond to the input lines (in order). Each output line gives the decoding of the corresponding instruction: either right or left, followed by a single space, followed by the number of steps to be taken in that direction.

Sample Input

57234

00907

34100

99999

Output for Sample Input

right 234

right 907

left 100

在这个问题中,我们试图解码桑托斯教授给出的一系列代码。我们将获得一些输入行。每个输入行将包含 5 个字符的字符串。

现在,我们必须解码这 5 个字符。在这里,前两个字符代表运行的方向。因此,我们将这两个字符转换为整数并求出它们的和。然后我们将通过以下条件确定方向:

如果总和为奇数,则转弯方向为左。

如果它们的总和为零,则方向不会改变,所以我们将通过并

如果它们的总和为偶数且不为零,则转弯方向是正确的。

我们可以通过将一个数除以 2 并检查提示符是 0 还是 1 来确定它是偶数还是奇数。为此我们可以使用 mod (%)。例如,3 是奇数。所以 3%2 = 1。同样,6 是偶数。所以 6%2 = 0。

最后,输入行的其余 3 个字符确定需要采取的步骤数。它将字符串从索引 2 切到末尾。看图清楚地了解解码过程。

代码

#include <bits/stdc++.h>

using namespace std;

int main() {   
  string prev(1, '.');
  while (true) {
    string s;
    cin >> s;
    if (s == "99999") {
      break;
    }
    int n0 = (int) (s[0] - '0');
    int n1 = (int) (s[1] - '0');
    {
      if (n0 + n1 == 0) {
        cout << prev << " ";
      } else if ((n0 + n1) % 2 == 0) {
        cout << "right ";
        prev = "right";
      } else if ((n0 + n1) % 2 == 1) {
        cout << "left ";
        prev = "left";
      }
    }
    cout << s.substr(2, 3) << '\n';
  }
  return 0;
}

Problem J4: Arranging Books

Problem Description

Valentina wants books on a shelf to be arranged in a particular way. Every time she sees a shelf of books, she rearranges the books so that all the large books appear on the left, followed by all the medium-sized books, and then all the small books on the right. She does this by repeatedly choosing any two books and exchanging their locations. Exchanging the locations of two books is called a swap. Help Valentina determine the fewest number of swaps needed to arrange a shelf of books as she wishes.

Input Specification

The input will consist of exactly one line containing at least one character. The following table shows how the available 15 marks are distributed.

 Output Specification

Output a single integer which is equal to the minimum possible number of swaps needed to arrange the books so that all occurrences of L come first, followed by all occurrences of M, and then all occurrences of S.

代码

#include <bits/stdc++.h>

using namespace std;

int main() {   
  string s;
  cin >> s; 
  int cl = count(s.begin(), s.end(), 'L');
  int cm = count(s.begin(), s.end(), 'M');
  int nl = 0;
  for (int i = 0; i < cl; i++) {  
    nl += (s[i] != 'L');
  }
  int nm = 0;
  for (int i = cl; i < cl + cm; i++) {
    nm += (s[i] != 'M');
  }
  int m_in_l = 0;
  for (int i = 0; i < cl; i++) {
    m_in_l += (s[i] == 'M');
  }
  int l_in_m = 0;
  for (int i = cl; i < cl + cm; i++) {
    l_in_m += (s[i] == 'L');
  }
  cout << nl + nm - min(m_in_l, l_in_m) << '\n';
  return 0;
}

Problem J5/S2: Modern Art

Problem Description

A new and upcoming artist has a unique way to create checkered patterns. The idea is to use an M-by-N canvas which is initially entirely black. Then the artist repeatedly chooses a row or column and runs their magic brush along the row or column. The brush changes the colour of each cell in the row or column from black to gold or gold to black. Given the artist’s choices, your job is to determine how much gold appears in the pattern determined by these choices.

Input Specification

The first line of input will be a positive integer M. The second line of input will be a positive integer N. The third line of input will be a positive integer K. The remaining input will be K lines giving the choices made by the artist. Each of these lines will either be R followed by a single space and then an integer which is a row number, or C followed by a single space and then an integer which is a column number. Rows are numbered top down from 1 to M. Columns are numbered left to right from 1 to N. The following table shows how the available 15 marks are distributed.

Output Specification

Output one non-negative integer which is equal to the number of cells that are gold in the pattern determined by the artist’s choices. 

这个问题有点棘手,但是一旦你掌握了主要思想,你就会惊讶地发现我们如何使用简单的数学和逻辑技巧来解决冗长的问题。在这个问题中,我们将得到一个 M×N 网格类型的画布,其中每个单元格最初都是黑色的。我们的美术师将选择一行或一列,然后运行他们的魔法画笔,将颜色从黑色变为金色或将金色变为黑色。在画布上抚摸 K 次后,我们必须找出有多少单元格是金色的。

代码

#include <bits/stdc++.h>

using namespace std;

int main() {   
  int m, n, k;
  cin >> m >> n >> k;
  vector<int> delta_row(m);
  vector<int> delta_col(n);
  while (k--) {
    char op;
    int id;
    cin >> op >> id;
    --id;
    delta_row[id] += (op == 'R');
    delta_col[id] += (op == 'C');
  }
  vector<vector<int>> delta(m, vector<int>(n));
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      delta[i][j] += delta_row[i];
    }
  }
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      delta[i][j] += delta_col[j];
    }
  }
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cnt += (delta[i][j] % 2 == 1);
    }
  } 
  cout << cnt << '\n';
  return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值