『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester
这几天一直在宿舍跑PY模型,学校的ACM寒假集训我也没去成,来学校的时候已经18号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试了一下,果然被虐成渣,十二道题目在有限时间内就做了四道,还有一道疯狂的WA,拿出两道一些有趣的想法出来分享一下。
今天打题就遇到了大数计算的问题,本来昨晚想解决这个难题,也没来得及,所以打题的时候大数计算那道就放弃了,过几天我一定会扔上来的。
今日兴趣新闻:
年度最惨小学生!在姥姥家热炕头写作业,写完一看字都没了!
https://baijiahao.baidu.com/s?id=1625958926368259758&wfr=spider&for=pc
笑出猪叫声,正映衬现在刚开学前的大学生疯狂的恶补一些零零碎碎的作业和策划书哈哈,话说这个热可擦我也还没玩过呢,老了老了。
------------------------------------------------题目----------------------------------------------------------
Malek and Summer Semester
Malek registered n courses for the summer semester. Malek has a success rate m, which means he has to succeed at least in ceil(n × m) courses out of the n courses, in order to consider the summer semester as a successful semester. Malek is considered successful in the ith course, if his grade on this course was greater than or equal to 50.
ceil(x) is the smallest integer greater than or equal to x. For example, ceil(0.95) = 1, ceil(4) = 4, and ceil(7.001) = 8.
Malek will give you his grades in the n courses, and your task is to tell him whether the summer semester was a successful semester or not.
Input
The first line contains an integer T (1 ≤ T ≤ 100), where T is the number of test cases.
The first line of each test case contains an integer n and a real number m (1 ≤ n ≤ 100) (0 < m < 1), where n is the number of courses, and m is the required success rate.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 100), where ai is the grade of the ith course.
The success rate m is given with exactly two digits after the decimal point.
Output
For each test case, print a single line containing "YES" (without quotes), if the summer semester was a successful semester for Malek. Otherwise, print "NO" (without quotes).
Example
input
0.60 0.75
output
NO
YES
------------------------------------------------题目----------------------------------------------------------
(一) 原题大意:
Malek 为夏季学期注册了n门课程。马利克拥有成功率米,这意味着他至少在成功小区(ñ × 米)课程出的ñ课程,为了考虑夏季学期作为一个成功的学期。马利克被认为是成功的我个当然,如果他对这个课程成绩大于或等于50。
ceil(x)是大于或等于 x的最小整数。例如, ceil(0.95)= 1, ceil(4)= 4,ceil(7.001)= 8。
题目很简单,实际上这是一道2017 JUST编程大赛3.0的签到题,拿这道题出来呢是有一个小的细节使用知识点,那就是ceil
(二) AC代码:
#include<iostream>
#include<cmath>
#include<stdio.h>
using namespace std;
int times,number,mark,ok;
float up;
int main()
{
scanf("%d", ×);
for(;times>;times--)
{
scanf("%d %f", &number,&up);
ok = ceil(number*up);
printf("%d",ok);
for(;number>;number--)
{
scanf("%d", &mark);
if(mark>=) ok--;
}
if(ok <= ) printf("YES\n");
else printf("NO\n");
}
return ;
}
(三) 解后分析:
这道题难度小,如果会用ceil这个函数就特别简单了,ceil(x)这个函数是向上取整函数,如果需要向下取整,还有一个函数是floor(x)。
floor()是向负无穷大舍入,floor(-10.5) == -11;
ceil()是向正无穷大舍入,ceil(-10.5) == -10
这是一个细节知识点,可能以后会经常用到呢。
------------------------------------------------题目----------------------------------------------------------
The Architect Omar
Architect Omar is responsible for furnishing the new apartments after completion of its construction. Omar has a set of living room furniture, a set of kitchen furniture, and a set of bedroom furniture, from different manufacturers.
In order to furnish an apartment, Omar needs a living room furniture, a kitchen furniture, and two bedroom furniture, regardless the manufacturer company.
You are given a list of furniture Omar owns, your task is to find the maximum number of apartments that can be furnished by Omar.
Input
The first line contains an integer T (1 ≤ T ≤ 100), where T is the number of test cases.
The first line of each test case contains an integer n (1 ≤ n ≤ 1000), where n is the number of available furniture from all types. Then n lines follow, each line contains a string s representing the name of a furniture.
Each string s begins with the furniture's type, then followed by the manufacturer's name. The furniture's type can be:
- bed, which means that the furniture's type is bedroom.
- kitchen, which means that the furniture's type is kitchen.
- living, which means that the furniture's type is living room.
All strings are non-empty consisting of lowercase and uppercase English letters, and digits. The length of each of these strings does not exceed 50 characters.
Output
For each test case, print a single integer that represents the maximum number of apartments that can be furnished by Omar
Example
Input
bedXs
kitchenSS1
kitchen2
bedXs
living12
livingh
Output
------------------------------------------------题目----------------------------------------------------------
(一) 原题大意:
这道题呢难度也不大,算是签到题,只是有点好玩。原题意思是某个人要建一个公寓,现在需要两张床bed、一个厨房kitchen、一个居室living,才能算是一个公寓,现在给你一堆家居,自带类型+乱七八糟的字符串,让你识别出是什么类型的家具,然后输出可以组成几间公寓。
(二) 题目分析:
唯一难点可能就是在识别输入的字符串中是否含有某个词语,解题后我翻了一下网上的一些做法,有人干脆就直接判断第0下标的字母'b' 'k' 'l'来区分,感觉这是不严谨的,,还有人用到了string的find函数,这个倒是挺新奇,我还不知道,待会也贴上来学习一下。我的题目做法呢是用strncmp函数,去对比开头是否是所要求的类型,然后再进行计算。打题的时候还发现一些比较有趣的字符串处理函数,如strrev(array);这是反转字符串,等等。
(三) 代码分块:
第一步:先去获取到每一个家具的类型:
for(;number>;number--)
{
scanf("%s", name);
if(strncmp(name,"bed",) == ) have[]++;
else if(strncmp(name,"kitchen",) == ) have[]++;
else if(strncmp(name,"living",) == ) have[]++;
}
我是这么做的,对比前面字母是否相同,然后获取。
第二步,计算最大拥有几个公寓:
while()
{
if(have[] < || have[] == || have[] == ) break;
have[]-=;
have[]--;
have[]--;
ok++;
}
(四) AC代码:
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
int times,number,have[],ok;
char name[];
float up;
int main()
{
scanf("%d", ×);
for(;times>;times--)
{
have[] = have[] = have[] = ok = ;
scanf("%d %f", &number);
for(;number>;number--)
{
scanf("%s", name);
if(strncmp(name,"bed",) == ) have[]++;
else if(strncmp(name,"kitchen",) == ) have[]++;
else if(strncmp(name,"living",) == ) have[]++;
}
while()
{
if(have[] < || have[] == || have[] == ) break;
have[]-=;
have[]--;
have[]--;
ok++;
}
printf("%d",ok);
}
return ;
}
(五)AC截图:
(六) 解后分析:
题目不难,直接判断首字母也能过,翻看别人的题解的时候发现一个string的某个用法,可能感觉挺有用,贴出来学习一下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int num1=,num2=,num3=;
for(int i=;i<n;i++){
string s;
cin>>s;
if(s.find("bed")==)num1++;
if(s.find("kitchen")==)num2++;
if(s.find("living")==)num3++;
}
//cout<<num1<<" "<<num2<<" "<<num3<<endl;
int ans=min(num1/,min(num2,num3));
printf("%d\n",ans);
}
return ;
}
该题解使用了string类,并使用了string的find函数,这个函数可以查找字符串中特定的字符串内容。为了拓展这个知识点,我还找到了下面一些有趣的函数原型:
注:以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算)。若查找成功,返回按查找规则找到的第一个字符或子串的位置;若查找失败,返回npos,即-1(打印出来为4294967295)。
(1)find():
//string (1)
size_type find (const basic_string& str, size_type pos = ) const noexcept;
//c-string (2)
size_type find (const charT* s, size_type pos = ) const;
//buffer (3)
size_type find (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type find (charT c, size_type pos = ) const noexcept;
这个也就是上面代码所使用了的了,后面的参数可以省略。
(2)rfind():
//string (1)
size_type rfind (const basic_string& str, size_type pos = npos) const noexcept;
//c-string (2)
size_type rfind (const charT* s, size_type pos = npos) const;
//buffer (3)
size_type rfind (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type rfind (charT c, size_type pos = npos) const noexcept;
rfind()与find()很相似,差别在于查找顺序不一样,rfind()是从指定位置起向前查找,直到串首。例如,上例中的st1.rfind('a',7)一句,就是从st1的位置7(st1的最后一个字符b)开始查找字符a,第一次找到的是倒数第2个字符a,所以返回6。
(3)find_first_of():
//string (1)
size_type find_first_of (const basic_string& str, size_type pos = ) const noexcept;
//c-string (2)
size_type find_first_of (const charT* s, size_type pos = ) const;
//buffer (3)
size_type find_first_of (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type find_first_of (charT c, size_type pos = ) const noexcept;
在源串中从位置pos起往后查找,只要在源串中遇到一个字符,该字符与目标串中任意一个字符相同,就停止查找,返回该字符在源串中的位置;若匹配失败,返回npos。
(4) find_last_of():
//string (1)
size_type find_last_of (const basic_string& str, size_type pos = npos) const noexcept;
//c-string (2)
size_type find_last_of (const charT* s, size_type pos = npos) const;
//buffer (3)
size_type find_last_of (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type find_last_of (charT c, size_type pos = npos) const noexcept;
该函数与find_first_of()函数相似,只不过查找顺序是从指定位置向前。
(5)find_first_not_of():
//string (1)
size_type find_first_not_of (const basic_string& str, size_type pos = ) const noexcept;
//c-string (2)
size_type find_first_not_of (const charT* s, size_type pos = ) const;
//buffer (3)
size_type find_first_not_of (const charT* s, size_type pos, size_type n) const;
//character(4)
size_type find_first_not_of (charT c, size_type pos = ) const noexcept;
在源串中从位置pos开始往后查找,只要在源串遇到一个字符,该字符与目标串中的任意一个字符都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满 足条件的字符,则返回npos。
注:同理也有find_last_not_of()。
注:如果有更好的解法,真心希望您能够评论留言贴上您的代码呢~互相帮助互相鼓励才能成长鸭~~
『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester的更多相关文章
- Java基础知识强化11:多态的两道基础题
1.第1题 class Base { public void method() { System.out.print("Base method"); } } class Child ...
- 各位大佬Python的第一部分道基础题已经整理好了,希望大家面试的时候能用的上。
Python的第一部分道基础题,希望大家面试的时候能用的上. 1.为什么学习Python? Python是目前市面上,我个人认为是最简洁.最优雅.最有前途.最全能的编程语言,没有之一. 2.通过什么途 ...
- 『ACM C++』 Codeforces | 1066A - Points in Segments
大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席.团委团总支.社团社长都体验过一番了,现在差个班长也没试过,就来体验了 ...
- ACM/ICPC 之 Floyd范例两道(POJ2570-POJ2263)
两道以Floyd算法为解法的范例,第二题如果数据量较大,须采用其他解法 POJ2570-Fiber Network //经典的传递闭包问题,由于只有26个公司可以采用二进制存储 //Time:141M ...
- ACM/ICPC 之 SPFA范例两道(POJ3268-POJ3259)
两道以SPFA算法求解的最短路问题,比较水,第二题需要掌握如何判断负权值回路. POJ3268-Silver Cow Party //计算正逆最短路径之和的最大值 //Time:32Ms Memory ...
- 『ACM C++』PTA浙大 | 基础题 - Have Fun with Numbers
连着这两道都是开学前数构老师的“爱心作业”,还没上课开学就给我们布置作业了,这道题有点小坑,也经常遇到类似的问题,特地拿出来记录一下. -------------------------------- ...
- 『ACM C++』 PTA 天梯赛练习集L1 | 029-033
哈哈,今天开始我也是学车人了~ 开始一千多道疯狂刷题~ ------------------------------------------------L1-029------------------ ...
- 『ACM C++』 PTA 天梯赛练习集L1 | 007-011
真的是忙头晕了,学业.ACM打题.班级活动.自学新东西,哇这充实的大学~ ------------------------------------------------L1-007--------- ...
- 『ACM C++』HDU杭电OJ | 1418 - 抱歉 (拓扑学:多面体欧拉定理引申)
呕,大一下学期的第一周结束啦,一周过的挺快也挺多出乎意料的事情的~ 随之而来各种各样的任务也来了,嘛毕竟是大学嘛,有点上进心的人多多少少都会接到不少任务的,忙也正常啦~端正心态 开心面对就好啦~ 今天 ...
随机推荐
- Java Timer定时器原理
做项目很多时候会用到定时任务,比如在深夜,流量较小的时候,做一些统计工作.早上定时发送邮件,更新数据库等.这里可以用Java的Timer或线程池实现.Timer可以实现,不过Timer存在一些问题.他 ...
- java常用API之System类
System中代表程序所在系统,提供了对应的一些系统属性信息,和系统操作.System类不能手动创建对象,因为构造方法被private修饰,阻止外界创建对象.System类中的都是static方法,类 ...
- asp 日期操作
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <% Response.Buffer=True Sessi ...
- 在 CentOS 上安装 node.js
进入到 /usr/local/ 目录中: cd /usr/local/ 创建 nodejs 文件夹: mkdir -p nodejs 进入到 nodejs 目录中: cd nodejs 下载 node ...
- PHP正则表达式实例汇总
$str = preg_replace("/(<a.*?>)(.*?)(<\/a>)/", '\1<span class="link&quo ...
- js时间与时间戳之间的转换操作,返回天、小时、分,全家桶
1.将时间戳转换成时间 var formatDate = function(d) { var now = new Date(d); var year = now.getFullYear(); var ...
- 有关table布局时tr 属性display:block显示布局错乱
display:block display:block是可以把非块级元素强制转换为块级元素显示,如内嵌元素span,原来不支持设置宽高,宽度是由内容撑开的; display:table-row tab ...
- Entity Framework:“无法加载指定的元数据资源
System.Data.Entity.Core.MetadataException:“无法加载指定的元数据资源 CodeFirst方式使用EF,写入数据时报错.System.Data.Entity.C ...
- JSTL标签概述
什么是JSTL JSP 标准标记库(JSP Standard Tag Library,JSTL)是一个实现 Web 应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断.数据管理格式化. ...
- 搭建 Spring 2.5.6 开发环境
1.jar 包准备: spring 2.5.6 的 jar 包(链接: http://pan.baidu.com/s/1skVFfcx 密码: mbiz),如图: commons-logging-1. ...