当前位置: 首页 > news >正文

考研复试机试 | C++ | 尽量不要用python,很多学校不支持

目录

  • 1.1打印日期 (清华大学上机题)
    • 题目:
    • 代码:
  • 1.2改一改:上一题反过来
    • 问题
    • 代码:
  • 2.Day of Week (上交&&清华机试题)
    • 题目:
    • 代码:
  • 3.剩下的树(清华机试)
    • 题目:
    • 代码:
  • 4.手机键盘
    • 题目:
    • 代码:

1.1打印日期 (清华大学上机题)

题目:

给出年分m和一年中的第n天,算出第n天是几月几号。

输入描述:
输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。

输出描述:
可能有多组测试数据,对于每组数据,
按 yyyy-mm-dd的格式将输入中对应的日期打印出来。

示例1
输入:
2000 3
2000 31
2000 40
2000 60
2000 61
2001 60
输出:
2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01

代码:

#include <cstdio>using namespace std;int main(){int year,n;//                 1  2  3  4  5  6  7  8 9 10 11 12int mday[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};while(scanf("%d%d",&year,&n) != EOF){int mon = 1;int day = 1;for (int i=1;i<n;i++){// 是否使闰年 bool isRun = year%400==0 || year % 100!=0 && year%4==0;// 如果使闰年,2月置为29天 if (isRun){mday[2] = 29;}// nextdayday++;// 如果天数累加到超过当月,重置天数计时,mon+=1 if(day>mday[mon]){mon++;day=1;// 如果mon累加超过12个月 if(mon>12){mon = 1;year++;} }	}printf("%04d-%02d-%02d\n",year,mon,day); }
} 

在这里插入图片描述
提交网址:
https://www.nowcoder.com/questionTerminal/b1f7a77416194fd3abd63737cdfcf82b

1.2改一改:上一题反过来

问题

在这里插入图片描述

代码:

#include <cstdio>using namespace std;int main(){int year,mon,day;//                 1  2  3  4  5  6  7  8 9 10 11 12int mday[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};while(scanf("%d %d %d",&year,&mon,&day)!=EOF){int count=0;// 判断是否是闰年 bool isRun = year%400==0 || year % 100!=0 && year%4==0;if(isRun) mday[2]=29;int i;// 将前mon个月的天数相加 for(i=1;i<mon;i++){count += mday[i];}// 将后一个月的天数加进去 count += day;printf("%d",count); }
} 

2.Day of Week (上交&&清华机试题)

题目:

描述
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400. For example, years 2004, 2180 and 2400 are leap. Years 2005, 2181 and 2300 are not leap. Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.
(简单来说就是,给你一个日期,让你判断这是本周的第几天)

输入描述:
There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.
输出描述:
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case. Month and Week name in Input/Output: January, February, March, April, May, June, July, August, September, October, November, December Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

直接看示例就好

示例1
输入
9 October 2001
14 October 2001
输出
Tuesday
Sunday

代码:

王道风格

#include <cstdio>
#include <string> 
#include <map> using namespace std;// 涉及字符串时,输入输出使用C风格的字符串,中间过程使用C++的字符串 
int main(){// 字符串月份到int的map map<string,int> monthToInt = {{"January",1}, {"February",2}, {"March",3}, {"April",4}, {"May",5}, {"June",6}, {"July",7}, {"August",8}, {"September",9}, {"October",10}, {"November",11}, {"December",12}};string intToWeekday[7] = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};int mday[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};int year,day,mon;char str[100];string month;bool isBefore;// 9 October 2001// 字符串的输入不需要加& while(scanf("%d%s%d",&day,str,&year)!=EOF) {month = str; //把字符串从C风格转换为C++风格// month.c_str()  字符串从C++风格转换为C风格 mon = monthToInt[month];// 写这个代码时  时2023年2月15日 if(year<2023 || year==2023&&mon<2 || year==2023&&mon==2&&day<15){isBefore=true;}else{isBefore=false;}//确定起点和终点 int begYear,begMon,begDay,endYear,endMon,endDay;if(isBefore){begYear = year;begMon=mon;begDay = day;endYear = 2023;endMon=2;endDay=17;}else{begYear = 2023;begMon=2;begDay = 17;endYear = year;endMon=mon;endDay= day;}int totalDay = 0;while(true){if(begYear==endYear&&begMon==endMon&&begDay==endDay) break;totalDay++;bool isRun = begYear%400==0 || begYear % 100!=0 && begYear%4==0;if(isRun) {mday[2]=29;}else {mday[2]=28;}begDay++;if(begDay>mday[begMon]){begDay = 1;begMon++;if(begMon>12){begMon = 1;begYear++;}}}if(isBefore){// (x+totalDay)%7 = 5  --> x = (12-totalDay%7)%7 printf("%s\n",intToWeekday[(12-totalDay%7)%7].c_str());}else{// 2023年2月17日是星期5 printf("%s\n",intToWeekday[(totalDay+5)%7].c_str());}}
} 

在这里插入图片描述

3.剩下的树(清华机试)

题目:

有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,…,L共L+1个位置上有L+1棵树。 现在要移走一些树,移走的树的区间用一对数字表示,如 100 200表示移走从100到200之间(包括端点)所有的树。 可能有M(1<=M<=100)个区间,区间之间可能有重叠。现在要求移走所有区间的树之后剩下的树的个数。

输入描述:
两个整数L(1<=L<=10000)和M(1<=M<=100)。 接下来有M组整数,每组有一对数字。

输出描述:
可能有多组输入数据,对于每组输入数据,输出一个数,表示移走所有区间的树之后剩下的树的个数。

示例:
输入:
500 3
100 200
150 300
输出:
470 471

代码:

#include <cstdio>int main(){int tree[10001];  // 1表示有树,0表示没树 int L,M;scanf("%d%d",&L,&M);//L+1颗树 --> 0到Lfor(int i=0;i<=L;i++){tree[i] = 1;} for(int idx=0;idx<M;idx++){int left,right;scanf("%d%d",&left,&right);for(int i =left;i<=right;i++){tree[i] = 0;} }int total = 0;for (int i=0;i<=L;i++){if (tree[i]==1)total++;}printf("%d\n",total);
}

4.手机键盘

题目:

按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次。
一、如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按2下,kz需要按6下
二、如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如ac,在按了a之后,需要等一会儿才能按c。 现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。 现在给出一串字符,需要计算出它所需要花费的时间。

输入描述:
一个长度不大于100的字符串,其中只有手机按键上有的小写字母
输出描述:
输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间

示例:
输入:
bob
www
输出:
7
7

代码:

#include<cstdio>
#include<map>
using namespace std;
int main(){//各个字母属于哪个按键map<char,int> position={{'a',2},{'b',2},{'c',2},{'d',3},{'e',3},{'f',3},{'g',4},{'h',4},{'i',4},{'j',5},{'k',5},{'l',5},{'m',6},{'n',6},{'o',6},{'p',7},{'q',7},{'r',7},{'s',7},{'t',8},{'u',8},{'v',8},{'w',9},{'x',9},{'y',9},{'z',9}};//输入某一个字母需要多长时间map<char,int> input_time={{'a',1},{'b',2},{'c',3},{'d',1},{'e',2},{'f',3},{'g',1},{'h',2},{'i',3},{'j',1},{'k',2},{'l',3},{'m',1},{'n',2},{'o',3},{'p',1},{'q',2},{'r',3},{'s',4},{'t',1},{'u',2},{'v',3},{'w',1},{'x',2},{'y',3},{'z',4}};// 题目中说明字符串长度不超过100 char str[101];while(scanf("%s",str)!=EOF){//input_last是上次按下的按键。初始时,将input_last置为与其他所有按键都不同的值int input_last=1;//输入一个字符串需要的总时间int total_time=0;// C风格的字符串以 '\0' 结束 for(int i=0;str[i]!='\0';++i){//如果本次即将要输入的字母的按键 与上个字母所在按键相同// str[i]是本次要输入的字符, position[str[i]]是本次要按下的数字键 if(position[str[i]]==input_last){total_time=total_time+2; // 相同则需要等待2个时间段 }// 按出这个字母所需的时间 total_time=total_time+input_time[str[i]];// 记录本次按下的数字 input_last=position[str[i]];}printf("%d\n",total_time);}
}
http://www.lryc.cn/news/11347.html

相关文章:

  • ChatGPT时代,别再折腾孩子了
  • 万字干货 | 荔枝魔方基于云原生的架构设计与实践
  • #科研筑基# python初学自用笔记 第九篇 面向对象编程
  • Python快速上手系列--邮件发送--详解篇
  • 【Bluetooth开发】蓝牙开发入门
  • 07:进阶篇 - 在程序中嵌入 CTK Plugin Framework
  • 快速低成本动画视频课
  • 大数据平台测试-软件测试常见面试回答(持续更新)
  • 链表学习之反转链表
  • ONNXRUNTUIME实例分割网络说明
  • 几行代码,就写完懒加载啦?
  • PyTorch常用的损失函数(ChatGPT)
  • LeetCode——1237. 找出给定方程的正整数解
  • 系统编程中的进程的概念No.3【进程状态】
  • 推荐 3 款 Golang 语义化版本库
  • Windows平台使用gdb连接qemu虚拟机上的系统
  • 【博客624】MAC地址表、ARP表、路由表(RIB表)、转发表(FIB表)
  • 【蓝桥日记⑤】2014第五届省赛(软件类)JavaA组❆答案解析
  • Leetcode.1139 最大的以 1 为边界的正方形
  • Bing+ChatGPT 对传统搜索引擎的降维打击
  • 【JS】数组常用方法总结-功能、参数、返回值
  • pytest 单元测试前后置处理
  • 汽车安全硬件扩展 AUTOSAR SHE SecureHardwareExtensions
  • 2023年美国大学生数学建模C题:预测Wordle结果建模详解+模型代码
  • 5、HAL库驱动W25Qxx
  • git rebase 洐合(变基)
  • Kubernetes 1.18学习笔记
  • AJAX技术
  • 华为OD机试 - 最大排列(JS)
  • Prometheus Docker安装及监控自身