中国电信四川省2024年秋季校招笔试编程题 C++

中国电信校招编程题


题目描述

某个班级学生去旅行,student[i]表示第i-1名学生旅行所花费的时间。例如student[0]=2表示第1名学生旅行一次需要花费2单位的时间。每名学生可以旅行多次,即旅行结束后可立即再次旅行。

现给定一组数据表示学生的一次旅行所花费的时间std[m]

和指定旅行的总次数n

需要计算出完成n次旅游所花的最小时间。

例子:

输入

1 2 3 4

5

输出

4

解析:四名同学在单位时间0时同时出发,第四名同学须在单位时间为4时才能完成旅行。

第一名同学在4个单位时间内可完成4次旅行。

4 + 2 + 1 + 1 = 8 > 5


思路解析

抓住两个要点:

1.每名学生从单位时间0开始出发,必须保证每名学生都至少完成一次旅行。

2.旅行总次数不少于给定的次数。

这个有点像动态规划的意思

每次增加一个时间单位,看这些同学完成了几个轮次的旅游。

在保证上面两个条件都满足时,此时所消耗的时长就是最小时间。

和最短路径算法有相同的地方。

编程实现

cpp 复制代码
#include <iostream.h>
#include <string>
#include <vector>
using namespace std;

int main()
{
	vector<int> studs;
	int tempi,tarCount;
	
	while(1)
	{
		cin >> tempi;
		studs.push_back(tempi);
		if(getchar() == '\n') break;
	}
	cin >> tarCount;
	
	//sort(studs.begin(),studs.end());
	int leng = studs.size();
	int *curTime = new int[leng];
	int *curCount = new int[leng];
	for(int i = 0; i < leng;i++)//初始化 
	{
		curTime[i] = 0;
		curCount[i] = 0;
	}
	
	int timeCount = 1;
	int allCount = 0;
	int minCount = 0;
	while(timeCount)
	{
		for(int i = 0; i < leng;i++)//更新curTime和curCount数组 
		{
			curTime[i]++;
			if(curTime[i] == studs[i])
			{
				curCount[i]++;
				curTime[i] = 0;
			} 
		}
		
		allCount = 0;
		minCount = 1;//每个学生至少完成一次 
		for(int i = 0; i < leng;i++)//判断退出条件 
		{
			allCount += curCount[i]; 
			if(curCount[i] == 0) 
			{
				minCount = 0;
				break;
			}
		}
		if(minCount && allCount >= tarCount) break;
		
		timeCount++;
	}
	
	/*cout<<endl;
	for(int i = 0; i < leng;i++)//初始化 
	{
		cout<<curTime[i] <<"  ";
	}
	cout<<endl;
	for(int i = 0; i < leng;i++)//初始化 
	{
		cout<<curCount[i] <<"  ";
	}
	cout<<endl;*/
	
	cout<<timeCount;
	cout<<endl;
	
	delete[] curTime;
	delete[] curCount;
	
	/*while(studs.size())
	{
		cout<<studs.back();
		studs.pop_back();
		if(studs.size()!=0)cout<<" ";
	}
	cout<<"|"<<endl;*/
	
	return 0;
} 

另外的

这里给一个英语单词翻转的,这也是编程题的一道题。这个更多的时考察输入这部分吧

例子:

输入:

给定一句英语句子,每个单词之间可以有多个空格。句子首尾也可以有多个空格

| apple an is this |

输出:

翻转单词,去除多余的空格

|this is an apple|

主要注意输入,cin输入遇到空格结束。但是题目没告诉句子最大长度,也用不了getline

这里采用cin+getchar的方式

cpp 复制代码
#include <iostream.h>
#include <string>
#include <vector>
using namespace std;

int main(int argc, char *argv[])
{
	vector<string> words;
	string inWords;
	char tempc;
	while(1)
	{
		while(1)
		{
			tempc = getchar();
			if(tempc !=' ') break;
		}
		if(tempc=='\n') break;
		
		cin>>inWords;
		inWords = tempc + inWords;
		words.push_back(inWords);
	}
	
	
	while(words.size())
	{
		cout<<words.back();
		words.pop_back();
		if(words.size()!=0)cout<<" ";
	}
	//cout<<"|"<<endl;
	
	return 0;
}
相关推荐
向哆哆几秒前
Java与NoSQL数据库的集成与优化
java·开发语言·nosql
MSTcheng.4 分钟前
【C语言】指针(5)
c语言·开发语言
╮壞孩子的天5 分钟前
C语言多人聊天室 ---chat(客户端聊天)
c语言·开发语言
IT猿手16 分钟前
2025高维多目标优化:基于导航变量的多目标粒子群优化算法(NMOPSO)的无人机三维路径规划,MATLAB代码
开发语言·人工智能·算法·机器学习·matlab·无人机·cocos2d
呱牛do it19 分钟前
Python Matplotlib图形美化指南
开发语言·python·matplotlib
pianmian123 分钟前
python制图之小提琴图
开发语言·python·信息可视化
水瓶丫头站住23 分钟前
Qt中QRadioButton的使用
开发语言·qt
非 白30 分钟前
【Java】代理模式
java·开发语言·代理模式
阿乾之铭30 分钟前
动态规划算法
算法·动态规划
菠菠萝宝32 分钟前
【代码随想录】第九章-动态规划(上)
算法·动态规划·01背包·完全背包·多重背包·上楼梯