指定字符的索引
输出指定字母在字符串中的索引
给定一个字符串,把字符串按照大写在前小写在后排序,输出排好后的第 K 个字母在原来字符串的索引。相同字母输出第一个出现的位置。
例如:
输入
1 hAkDAjByBq 4 输出
1 6 说明:
排好序后 AABBDhjkqy,第 4 个是 B,第一个出现的在原字符串 6 这个位置。(索引是从 0 开始)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int FindSourcePos(string source_string, int new_pos) {
string string_tempUp;
string string_tempLow;
for (char i : source_string) {
if ((i >= 'A') && (i <= 'Z')) {
string_tempUp += i;
} else {
string_tempLow += i;
}
}
sort(string_tempUp.begin(), string_tempUp.end());
sort(string_tempLow.begin(), string_tempLow.end());
string new_string = string_tempUp + string_tempLow;
auto old_pos = source_string.find(new_string[new_pos - 1]);
return old_pos;
}
int main(int argc, char **argv) {
while (1) {
string source_string;
int new_pos;
cin >> source_string >> new_pos;
cout << FindSourcePos(source_string, new_pos) << endl;
}
return 0;
}
strs='hAkDAjByBq'
n=4
res=[]
res2=[]
for i in range(len(strs)):
if strs[i].isupper():
res.append((strs[i],i))
else:
res2.append((strs[i],i))
print(res)
print(res2)
a=res+res2
print(a)
print(a[4-1][1])