Canadian Coding Competition(CCC) 2021 Junior 题解

news2024/11/16 19:29:43

目录

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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/134147.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

自动挂载USB和TF卡

转自链接https://zhuanlan.zhihu.com/p/443745437 ①创建用于挂载U盘的目录 mkdir /mnt/usb –p②在/etc/udev/rules.d目录下添加用于检测U盘插入规则&#xff08;add&#xff09;&#xff0c;终端下执行以下命令创建第一个U盘插入规则。 vim /etc/udev/rules.d/11-add-usb.r…

【ROS】—— ROS通信机制——服务通信(三)

文章目录前言1. 服务通信理论模型2. 服务通信自定义srv2.1 定义srv文件2.2 编辑配置文件2.3 编译3. 服务通信自定义srv调用(C)3.1 vscode配置3.2 服务端3.3 客户端3.4 配置 CMakeLists.txt4. 服务通信自定义srv调用(python)4.1 vscode配置4.2 服务端4.3 客户端4.4 配置 CMakeLi…

将Android进行到底之内容提供者(ContentProvider)

文章目录前言一、ContentProvider是什么&#xff1f;二、使用示例1.为应用创建内容提供者2.使用内容提供者2.1 内容URI2.2 Uri参数解析2.2 使用内容URI操作数据3.ContentProvider妙用4 内容URI对应的MIME类型5.ContentProvider重点注意6 演示demo源码总结前言 随着现在的应用越…

java通过JDBC连接mysql8.0数据库,并对数据库进行操作

目录 一、JDBC简介 二、添加依赖 三、JDBC操作数据库的步骤 四、JDBC操作数据库——增删改查 (一)新增数据 (二)删除数据 (三)修改数据 (四)查询数据 (五)多表连接查询 一、JDBC简介 Java数据库连接&#xff0c;&#xff08;Java Database Connectivity&#xff0c;简…

C进阶:字符串相关函数及其模拟实现

目录 &#x1f431;&#x1f638;一.strlen &#x1f54a;️1.功能 &#x1f43f;️2.模拟实现 &#x1f42c;&#x1f40b;二.strcpy &#x1f432;1.功能 &#x1f916;2.注意事项 &#x1f47b;3.模拟实现 &#x1f431;&#x1f42f;三.strcat &#x1f984;1.功能…

i.MX8MP平台开发分享(IOMUX篇)- Linux注册PAD

专栏目录&#xff1a;专栏目录传送门 平台内核i.MX8MP5.15.71文章目录1. pinfunc.h2.pinctrl-imx8mp.c3.PAD信息注册这一篇开始我们深入Linux中的pinctl框架。1. pinfunc.h pinfunc.h中定义了所有的引脚&#xff0c;命名方式是MX8MP_IOMUXC___&#xff0c;例如下面的MX8MP_IO…

【JDBC】----------ServletContext和过滤器

分享第二十四篇励志语句 幸福是什么&#xff1f;幸福不一定是腰缠万贯、高官显禄、呼风唤雨。平凡人自有平凡人的幸福&#xff0c;只要你懂得怎样生活&#xff0c;只要你不放弃对美好生活的追求&#xff0c;你就不会被幸福抛弃。 一&#xff1a;ServletContext&#xff08;重要…

js对象篇

面向对象 对象 如果对象的属性键名不符合JS标识符命名规范&#xff0c;则这个键名必须用引号包裹 访问属性有2种方法&#xff0c;有点语法和方括号填写法&#xff0c;特别地&#xff0c;如果属性名不符合JS命名规范或者使用变量存储属性名&#xff0c;则必须使用方括号访问 属…

【王道操作系统】2.3.6 进程同步与互斥经典问题(生产者消费者问题、吸烟者问题、读者写者问题、哲学家进餐问题)

进程同步与互斥经典问题(生产者消费者问题、吸烟者问题、读者写者问题、哲学家进餐问题) 文章目录进程同步与互斥经典问题(生产者消费者问题、吸烟者问题、读者写者问题、哲学家进餐问题)1.生产者-消费者问题1.1 问题描述1.2 问题分析1.3 如何实现1.4 实现互斥的P操作一定在实现…

深化全面To C战略魏牌发布与用户共创大六座SUV蓝山

对魏牌而言&#xff0c;与用户共创不是吸引眼球的营销噱头&#xff0c;而是“直面用户需求&#xff0c;真实倾听用户意见”的有效途径。 2022年12月30日&#xff0c;第二十届广州国际汽车展览会&#xff08;以下简称“广州车展”&#xff09;正式启幕。魏牌以“品位蓝山 有咖有…

神经网络必备基础知识:卷积、池化、全连接(通道数问题、kernel与filter的概念)

文章目录卷积操作实际操作filter与kernel1x1的卷积层可视化的例子池化全连接卷积操作 这个不难理解。我们知道图像在计算机中是由一个个的像素组成的&#xff0c;可以用矩阵表示。 假设一个5x5的输入图像&#xff0c;我们定义一个3x3的矩阵&#xff08;其中的数值是随机生成的…

excel图表美化:设置标记样式让拆线图精巧有趣

折线图作为我们平时数据视图化非常常规的表现方式&#xff0c;想必大家已经司空见惯了。折线图很简单&#xff0c;每个人都会做&#xff0c;但是不同的人做出来的折线图却千差万别。大多数人的折线图都是直接插入默认折线图样式生成的&#xff0c;这样的折线图先不说有没有用心…

五、IDEA中创建Web项目

文章目录5.1 创建Web项目5.1.1 创建项目5.1.2 编写Servlet类5.2 手动部署项目5.3 自动部署项目5.3.1 IDEA集成Tomcat5.3.2 IDEA部署JavaWeb项目5.4 war包部署5.4.1 导出war包5.1 创建Web项目 5.1.1 创建项目 1、打开IDEA&#xff0c;单击“New Project”或者通过File–>ne…

Perl语言入门

一、简介 Perl语言是拉里.沃尔&#xff08;Larry Wall&#xff09;在1987年开发的一种编程语言&#xff0c;借鉴了C、sed、awk、shell脚本语言以及其他语言的特性&#xff0c;专门用于文本处理。 它可以在各种平台上运行&#xff0c;例如Windows&#xff0c;Mac OS和各种UNIX…

bean生命周期

1.Aware和InitializingBean接口 Aware 接口用于注入一些与容器相关信息&#xff0c;例如 BeanNameAware: 注入bean的名字BeanFactorAware&#xff1a; 注入beanFactor容器ApplicationContextAware&#xff1a; 注入applicationContext容器EmbeddedValueResolverAware: ${} 代码…

爬虫进阶一(基础一)

文章目录简介cookie爬取雪球热帖代理模拟登陆防盗链异步爬虫协程asyncioM3U8HLS爬取seleniumbilibili无头浏览器规避检测MySQLMongoDBRedis简介 这个系列分四部分 基础进阶Scrapy 框架逆向分析实战运用 先补充一些爬虫需要的基础知识和技能预热&#xff0c;爬取个简历模板网站…

浅谈Git

Git是一个开源的分布式版本控制系统&#xff0c;可以有效、高速地处理从很小到非常大的项目版本管理,也是Linus Torvalds为了帮助管理Linux内核开发而开发的一个开放源码的版本控制软件。 版本控制 什么是版本控制&#xff1f; 版本控制是一种在开发的过程中用于管理我们对文…

如何评价唐卫国公李靖的战功、军事才能、政治才能?

link 一鞭直渡清河洛Research and no development已关注470 人赞同了该回答个人以为&#xff0c;在军事上&#xff0c;李靖是当之无愧的唐朝第一名将&#xff0c;他用兵如神&#xff0c;精于谋略&#xff0c;无论是在实际的军事指挥上&#xff0c;还是军事理论上&#xff0c;他…

Vue3 中computed计算属性的使用

目录前言&#xff1a;什么是计算属性选项式中的计算属性组合式中的计算属性计算属性和方法的区别&#xff1a;计算属性/计算方法注意事项&#xff1a;总结前言&#xff1a; 目标&#xff1a;现在vue3的使用越来越普遍了&#xff0c;vue3这方面的学习我们要赶上&#xff0c;今天…

银行家算法 源码+实验报告(用了自取)

XIAN TECHNOLOGICAL UNIVERSITY 课程设计报告 实验课程名称 操作系统—银行家算法 专 业&#xff1a;计算机科学与技术 班 级&#xff1a; 姓 名&#xff1a; 学 号&#xff1a; 实验学时&#xff1a; …