第一题
This commit is contained in:
57
Solving/StringSegmentation.cpp
Normal file
57
Solving/StringSegmentation.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
#include "Questions.h"
|
||||
|
||||
/**
|
||||
给定一个非空字符串S,其被N个‘-’分隔成N+1的子串,给定正整数K,要求除第一个子串外,
|
||||
其余的子串每K个字符组成新的子串,并用‘-’分隔。对于新组成的每一个子串,如果它含有的
|
||||
小写字母比大写字母多,则将这个子串的所有大写字母转换为小写字母;反之,如果它含有的
|
||||
大写字母比小写字母多,则将这个子串的所有小写字母转换为大写字母;大小写字母的数量相等时,不做转换。
|
||||
输入描述:
|
||||
输入为两行,第一行为参数K,第二行为字符串S。
|
||||
输出描述:
|
||||
输出转换后的字符串。
|
||||
示例1
|
||||
输入
|
||||
3
|
||||
12abc-abCABc-4aB@
|
||||
输出
|
||||
12abc-abc-ABC-4aB-@
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
void StringSegmentation() {
|
||||
std::string inputStr;
|
||||
int inputNmb;
|
||||
std::cin >> inputNmb;
|
||||
std::cin >> inputStr;
|
||||
|
||||
std::string output;
|
||||
int index = inputStr.find('-');
|
||||
if (index == -1) {
|
||||
return;
|
||||
}
|
||||
output = std::string(inputStr.begin(), inputStr.begin() + index + 1);
|
||||
inputStr = inputStr.substr(index + 1);
|
||||
std::string tmpStr{};
|
||||
index = inputStr.find('-');
|
||||
while (index != -1) {
|
||||
tmpStr += inputStr.substr(0, index);
|
||||
inputStr = inputStr.substr(index + 1);
|
||||
index = inputStr.find('-');
|
||||
}
|
||||
tmpStr += inputStr;
|
||||
for (auto i: tmpStr) {
|
||||
int low{}, up{}, cnt{};
|
||||
std::string lowStr = "-";
|
||||
std::string upStr = "-";
|
||||
if (i >= 'a' && i <= 'z') {
|
||||
++low;
|
||||
lowStr.push_back(i);
|
||||
upStr.push_back(i + 26);
|
||||
}
|
||||
else if (i >= 'A' && i <= 'z')
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::cout << output;
|
||||
}
|
Reference in New Issue
Block a user