C++程序题-2021.08.31

拆分输出字符串

题目描述

连续输入字符串(输入字符串个数为N,每个字符串长度不大于100,输入字符串间按照空格键分隔),请按长度为8拆分每个字符串后输出到新的字符串数组,输出的字符串按照升序排列。
长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入描述
输入内容:2 abc 123456789
输入说明:
1.输入两个字符串(以空客分隔),其中一个为abc,另一个为123456789
输出描述
输出内容:12345678 90000000 abc00000
输出说明:
1.abc字符串需要在后边补零,123456789拆分为12345678与90000000,所有的字符串需要升序排列后输出(以空格分隔)
2.输出字符串中的重复字符串不能删除

参考代码

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
	int N;
	cin >> N;
	vector<string> instr;
	string tmp_instr;
	for (int i = 0; i < N; i++)
	{
		cin >> tmp_instr;
		instr.push_back(tmp_instr);
	}

	int len, need;
	multiset<string> outstr;
	string tmp_outstr;
	for (int i = 0; i < N; i++)
	{
		len = instr[i].length();
		for (int j = 0; j < len; j++)
		{
			if ((j + 1) % 8 == 0)
			{
				tmp_outstr += instr[i][j];
				outstr.insert(tmp_outstr);
				tmp_outstr = "";
			}
			else
				tmp_outstr += instr[i][j];
		}
		if (len % 8 != 0)
		{
			need = 8 - len % 8;
			for (int k = 0; k < need; k++)
			{
				tmp_outstr += "0";
			}
			outstr.insert(tmp_outstr);
			tmp_outstr = "";
		}
	}

	for (multiset<string>::iterator it = outstr.begin(); it != outstr.end(); ++it)
	{
		if (it == outstr.begin())
		{
			cout << *it;
		}
		else
		{
			cout << " " << *it;
		}
		
	}

	return 0;
}

题2

题目描述

输入—行字符,排除字符串中的英文字母和数字,将剩余的字符串倒序输出。
输入描述
输入一行由键盘上相关符号组成的字符串
输出描述
字符串
e.g:
输入:~AzC*039&
输出:&*~

参考代码

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	string str;
	cin >> str;
	vector<char> ret;
	for (int i = 0; i < str.length(); i++)
	{
		if ((str[i] <= '9' && str[i] >= '0') || (str[i] <= 'z' && str[i] >= 'a') || (str[i] <= 'Z' && str[i] >= 'A'))
		{
			continue;
		}
		else
		{
			ret.push_back(str[i]);
		}
	}

	for (int i = ret.size() - 1; i >= 0; i--)
	{
		cout << ret[i];
	}
	return 0;
}

题3

题目描述

小华是社保单位的一位职员,最近小华接到了一项任务,需要统计有多少人来过社保单位,每个人来过几次。小华看着“出入记录薄”想,有没有什么好办法能让记录更加清晰呢?已知每个人都有唯一的社保号,且社保号固定为10位(前2位数字,第3-5位为大写字母,后五位为数字)。随机输入n个社保号,输出社保号根据大小排序后的结果(ASCII码小的字符在前),及每个社保号的出入次数。
输入描述
第1行: n个社保号的个数
第2行:第一个社保号

第n+1行:第n个社保号
输出描述
社保号排序后的结果及社保号出现的次数
一行一个社保号,及出现次数,社保号与次数之间用空格隔开
输出带回车换行
e.g:
输入:

5
39zwh89943
70tpt88092
26rau81365
39zwh89940
26rau81365

输出:

26rau81365 2
39zwh89940 1
39zwh89943 1
70tpt88092 1

参考代码

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;

	multiset<string> ms;
	vector<string> vstr;
	vector<string> outstr;
	vector<int> outnum;

	string tmp;
	for (int i = 0; i < n; i++)
	{
		cin >> tmp;
		ms.insert(tmp);
	}

	for (multiset<string>::iterator it = ms.begin(); it != ms.end(); ++it)
	{
		vstr.push_back(*it);
	}

	int tmp_num = 0;
	outstr.push_back(vstr[0]);
	outnum.push_back(1);
	for (int i = 1; i < n; i++)
	{
		if (vstr[i-1]==vstr[i])
		{
			outnum[tmp_num] = outnum[tmp_num] + 1;
			
		}
		else
		{
			tmp_num++;
			outstr.push_back(vstr[i]);
			outnum.push_back(1);
		}
	}

	for (int i = 0; i < outstr.size(); i++)
	{
		cout << outstr[i] << " " << outnum[i] << endl;
	}

	return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值