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

中国电信校招编程题


题目描述

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

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

和指定旅行的总次数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;
}
相关推荐
chenbingjie_c1 小时前
C++ 进阶核心知识点总结:模板、内存分区、new/delete、内存池
开发语言·c++
专注仿真2 小时前
ACM算法
算法·深度优先
Escalating_xu2 小时前
【C 语言】深入理解指针(1·下):指针运算、野指针、assert 与传址实战
java·c语言·开发语言
实心儿儿2 小时前
Qt — Qt 事件
开发语言·qt
周杰偷奶茶3 小时前
【Java】数据类型与变量
java·开发语言
code斗3 小时前
Java数据结构:堆详解
java·开发语言·数据结构
Frank_refuel3 小时前
【C++】Json库介绍和使用
c++
重生之小比特3 小时前
【C++进阶】map和set
java·开发语言·c++
lie..3 小时前
30天从零开始学AI应用开发(Day 12):像产品一样稳:流式输出、报错重试、花钱控制
开发语言·人工智能·php
傻啦嘿哟3 小时前
Python的默认参数把我坑惨了,原来写[]和写None的区别这么大
开发语言·python·机器学习