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

文章包含多个清华大学编程上机题的题目与解题代码,涉及日期计算、反转日期、判断日期对应周几以及处理道路树木移除的问题。通过示例输入和输出展示了解题思路和解决方案。
部署运行你感兴趣的模型镜像

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 12
	int 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;
			}
			
			// nextday
			day++;
			// 如果天数累加到超过当月,重置天数计时,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 12
	int 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到L
	for(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);
    }
}


您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lijiamingccc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值