题目一:
题目描述
两个字符串input1,和字符串input2,字符串中元素的值域是26个大写字母,判断input2中的所有字符是否都包含在字符串input1中
如果包含返回:true
否则返回:false
输入描述:两个字符串input1和input2,其中input1的长度大于input2.要求两个输入字符串的input1和input2的长度不小于5,如:
A字符串:BBDDCFFEL
B字符串:LCEFB
输出描述:
true 或者false
示例输入:
BBDDCFFEL
LCEFB
示例输出:
true
解题步骤:
#include "stdafx.h"
#include<iostream>
#include<string>
#include<ctype.h> //attention
using namespace std;
bool S1containS2(string s1,string s2) {
int size1 = s1.size();
int size2 = s2.size();
//边界条件判断
if (size1<5||size2<5||size1<=size2)
{
return false;
}
//检查里面的是不是大写字母
for (int i = 0; i < size1; i++)
{
if (isupper(s1[i])) { //这里直接调用库函数,省事
continue;
}
else
{
return false;
}
}
for (int j = 0; j < size2; j++)
{
if (isupper(s2[j])) {
continue;
}
else
{
return false;
}
}
//对于s2里面的字符在s1中找是否存在
int count = 0;
for (int i = 0; i < size2; i++)
{
char temp = s2[i];
bool isFoundThisRound = false;
for (int j = 0; j < size1;j++) {
if (s1[j]==temp)
{
isFoundThisRound = true;
break;
}
}
if (isFoundThisRound)
{
count++;
}
}
if (count==size2)
{
return true;
}
else
{
return false;
}
}
int main()
{
string s1, s2;
cin >> s1;
cin >> s2;
bool result = S1containS2(s1, s2);
if (result) {
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
system("pause");
return 0;
}
题目二:
题目描述:
2个大整数相加,每一位是一个小写字母,二十六进制,a表示0,z表示25,求结果
输入描述:
输入2个26进制数
输出描述:
输出2个26进制的和
示例:
输入:
z
bc
输出:
cb
解题代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
string a, b;
char c[10010];
int main()
{
cin >> a >> b;
reverse(a.begin(),a.end());
reverse(b.begin(), b.end());
int len = 0;
for (int i = 0,g=0; g|| i <max(a.size(),b.size()); i++)
{
int x = g;
if (i < a.size()) x += a[i] - 'a';
if (i < b.size()) x += b[i] - 'a';
c[len++] = (x % 26) + 'a';
g = x / 26;
}
for (int i =len-1;i>=0; i--)
{
cout << c[i];
}
cout << endl;
system("pause");
return 0;
}
题目三:
字符串解压:
解压规则:
每个字符串后面跟随一个数字,表示这个字符串重复的次数。
例如: "a5"解压后为"aaaaa","abc3"解压后的结果为"abcabcabc"
排序规则
1、根据每个字符串的重复次数的升序排序,然后输出结果
例如:"a3b2",输出结果为"bbaaa"
2、如果字符重复次数一样,则根据ASCII编码顺序做升序排列,然后输出结果,例如,"b2a2" ,输出结果为"aabb"
输入的原始字符仅仅包含字母与数字
输出的结果字符仅仅包含字母与数字
a11b2bac3bad3abcd2
bbabcdabcdbacbacbacbadbadbadaaaaaaaaaaa
代码:
struct member
{
string data;
int time_copy;
};
bool compare_str(string &a,string &b) {
if (a.size()<b.size())
{
return true;
}
else if (a.size()>b.size())
{
return false;
}
for (int i = 0; i <a.size(); i++)
{
if (a[i]>b[i]) {
return false;
}
else if (a[i]<b[i])
{
return true;
}
}
return true; //记得加一个默认值
}
bool comparison(member a,member b) {
if (a.time_copy<b.time_copy)
{
return true;
}
else if (a.time_copy>b.time_copy)
{
return false;
}
else
{
return compare_str(a.data, b.data);
}
}
int main() {
string input;
cin >> input; // a11b2bac3bad3abcd2
int countN = 0;
string data_now = "";
vector<member> answer_list;
for (int i = 0; i <input.size(); i++)
{
if (input[i]>='0' && input[i]<='9') {
//读到一个数字
countN = countN * 10 + input[i] - '0';
while (true) //然后疯狂的往后读,然后计数
{
if (i>=input.size()) {
break;
}
if (input[i+1]>='0' && input[i+1]<='9')
{
countN = countN * 10 + input[i+1] - '0';
i += 1;
}
else
{
break;
}
}
member new_member;
new_member.time_copy = countN;
new_member.data = data_now;
answer_list.push_back(new_member);
countN = 0;
data_now = "";
}
else
{
data_now += input[i];
}
}
sort(answer_list.begin(),answer_list.end(),comparison);
for (int i = 0; i < answer_list.size(); i++)
{
for (int j = 0; j< answer_list[i].time_copy; j++) {
cout << answer_list[i].data;
}
}
cout << endl;
system("pause");
return 0;
}
好了,今天就到此为止好了。