20220825笔试

1.给了三种合理的情况,一个是两位数与一位数交替出现,一个是两头是两位数,中间全是一位数,最后一种情况是两头是一位数,中间全是两位数。

待改进,有什么好的改进方法?

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<unordered_map>
#include<set>
#include<map>

using namespace std;
vector<int> nums; 
bool isF1(vector<int>& nums){
	int n=nums.size();
	if(0<nums[0] && nums[0]<=9 && 0<nums[n-1]&& nums[n-1]<=9){
		for(int i=1;i<=n-2;i++){
			if( nums[i]<10 || nums[i]>100){
				return false;
			}
		}
		return true;		
	}
	return false;
}
bool isF2(vector<int>& nums){
	int n=nums.size();
	if(9<nums[0] && nums[0]<100 && 9<nums[n-1]&& nums[n-1]<100){
		for(int i=1;i<=n-2;i++){
			if( 0>nums[i] || nums[i]>9){
				return false;
			}
		}
		return true;	
	}
	return false;
}
bool isF3(vector<int>& nums){
	int n=nums.size();
	if(0<nums[0] && nums[0]<=9){
		for(int i=0;i<n;i=i+2){
			if(9<nums[i] && nums[i]<100){
				return false;
			}
		}
		for(int i=1;i<n;i=i+2){
			if(0<nums[i] && nums[i]<=9){
				return false;
			}
		}
	}
	else{
		for(int i=0;i<n;i=i+2){
			if(0<nums[i] && nums[i]<=9){
				return false;
			}
		}
		for(int i=1;i<n;i=i+2){
			if(9<nums[i] && nums[i]<100){
				return false;
			}
		}
	}
	return true;
}
int main()
{
	while(1){
		nums.clear();
		int num;
		char c;
		while(scanf("%d",&num)!=EOF){
			nums.push_back(num);
			if(c=getchar()=='\n'){
				break;
			}
		}
		bool f1=false;
		bool f2=false;
		bool f3=false;
		int n=nums.size();
		f1=isF1(nums);
		f2=isF2(nums);
		f3=isF3(nums);
		if(f1||f2||f3){
			printf("true\n");
		}
		else{
			printf("false\n");
		}
	}
	
	return 0;	
 } 

2.现给出一组单词和行约束参数L,需要对文本进行处理,需求如下:

  • 规整后输出的每行所包含的字符个数应等于L
  • 每行文本包含尽可能多的单词,单词之间或行尾不足部分都以“*”填充
  • 如果一行有两个或两个以上的单词,则要求第一个单词和最后一个单词分别与左右边界对齐 一行内单词
  • 一行内单词间的“”尽可能均匀分布。
  • 如果无法均匀分布,则左边可多于右边1个“” 最后一行的单词之间仅需一个“*”
    代码参考https://blog.csdn.net/qq_30457077/article/details/121755606
    有修改
//输入
/* 
This,is,an,eggplant,of,our,beautifull,sweety,baby
16
*/
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<unordered_map>
#include<set>
#include<map>
using namespace std;
vector<string> justify(vector<string> &words,int L){
	vector<string> res;
	for(int start=0,end;start<words.size();start=end){
		int width=0,spaces=1,extra=0;
		for(end=start;end<words.size()&&width+words[end].size()+(end-start)<=L;++end){
			width+=words[end].size();
		}
		if(end-start!=1 && end!=words.size()){
			spaces=(L-width)/(end-start-1);
			extra=(L-width)%(end-start-1);
		}
		string line(words[start]);
		for(int i=start+1;i<end;++i){
			line+=string(spaces,'*');
			if(extra-- > 0) line+="*";
			line+=words[i];
		}
		line+=string(L-line.size(),'*');
		res.push_back(line);
	}
	return res;
}
int main()
{
	vector<string> words;
	char c;
	string str,temp;
	cin>>str;
	
	for(int i=0;i<str.size();i++){
		if(str[i]==','){
			words.push_back(temp);
			temp.clear();
		}
		else{
			temp.push_back(str[i]);
		}
	}
	if(temp!=""){
		words.push_back(temp);
	}
	int L;
	scanf("%d",&L);
	vector<string> res=justify(words,L);
	for(auto k:res){
		cout<<k<<endl;
	}
	return 0;	
 } 

3.给定多组原本的航班预订信息(航班号,座位号,乘客姓名),以及多组要改签的航班信息(原本航班号,原本座位号,新航班号,新座位号)

输出最后的航班预订信息,要是有重复的内容,以最新改签的为标准

输入的内容如下: 3 表示原本的航班信息数,2表示要改签的航班数

3
CZ7132,A1,ZHANGSAN
CZ7132,A2,ZHAOSI
CZ7156,A2,WANGWU
2
CZ7132,A1,CZ7156,A2
CZ7156,A2,CZ7156,A3

输出内容如下:
CZ7132,A2,ZHAOSI
CZ7156,A2,ZHANGSA
CZ7156,A3,WANGW

测试用例未全部通过,待解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值