字符串-1125-删除字符串中间的*
#include <iostream>
using namespace std;
int main(){
string a,b;
cin>>a;
bool k=true;
int o=0,p=a.size()-1;
while(k) {
if (a[o] == '*') {
b += a[o];
o++;
continue;
} else {
k = false;
break;
}
}
bool s=true;
int y=0;
while(s){
if(a[p]=='*'){
y++;
p--;
continue;
}
else{
s=false;
break;
}
}
for(int i=o;i<a.size();i++){
if(a[i]!='*') b+=a[i];
}
for(int i=1;i<=y;i++) b+='*';
cout<<b;
return 0;
}
1133.字符串的反码
#include <iostream>
#include <string>
using namespace std;
char gei(char c){
if(c>='a'&&c<='z'){
return 'z'-(c-'a');
}
else if(c>='A'&&c<='Z'){
return 'Z'-(c-'A');
}
else return c;
}
string gtis(string s){
string re="";
for (char c:s) {
re+=gei(c);
}
return re;
}
int main(){
string s;
cin>>s;
string inv=gtis(s);
cout<<inv<<endl;
return 0;
}
1312. 看完动漫要几天?
#include <iostream>
using namespace std;
string cut(string t,int h,int o){
string c;
int d=0;
for (int i = h; i<=o; ++i) {
c+=t[i];
d++;
}
return c;
}
int main()
{
int n,m=0,l=0,q=0;
string a,b,c,d,e,f;
cin>>n>>a>>b;
c=cut(a,0,1);
d=cut(a,3,4);
e=cut(b,0,1);
f=cut(b,3,4);
m=int(e[0]-c[0])*10+int(e[1]-c[1]);
l=int(f[0]-d[0])*10+int(f[1]-d[1]);
q=m*60+l;
n%q==0?cout<<n/q:cout<<n/q+1;
return 0;
}
1012. 我是第几个单词
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(){
string a,b,c;
getline(cin,a,'.');
cin>>c;
stringstream ss(a);
int k=0,m=0;
bool t=false;
while(ss>>b){
k++;
m+=b.size();
if(b==c){
t=true;
break;
}
}
t?cout<<k:cout<<m;
return 0;
}
1116. 调换位置
#include <iostream>
#include <string>
using namespace std;
int main(){
string a,b,c,f="";
cin>>a;
int d=0;
for(int i=0;i<a.size();i++){
if(a[i]==','){
d=i;
break;
}
}
b=a.substr(0,d);
c=a.substr(d+1,a.size()-b.size()-1);
f+=c;
f+=',';
f+=b;
cout<<f;
return 0;
}
1129. 简单a*b
#include<iostream>
#include <string>
using namespace std;
int main(){
string a,b,c;
cin>>a;
int d;
for (int i = 0; i < a.size(); ++i) {
if(a[i]=='*'){
d=i;
break;
}
}
b=a.substr(0,d);
c=a.substr(d+1,a.size()-b.size()-2);
long long e,f;
e=stoi(b);
f=stoi(c);
cout<<e*f<<endl;
return 0;
}
1130. 简单a+b
#include<iostream>
#include <string>
using namespace std;
int main(){
string a,b,c;
cin>>a;
int d;
for (int i = 0; i < a.size(); ++i) {
if(a[i]=='+'){
d=i;
break;
}
}
b=a.substr(0,d);
c=a.substr(d+1,a.size()-b.size()-2);
long long e,f;
e=stoi(b);
f=stoi(c);
cout<<e+f<<endl;
return 0;
}
1100. 词组缩写
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
string a,b,c;
getline(cin,a);
stringstream ss(a);
while(ss>>b&&b!="0"){
b[0]>='a'&&b[0]<='z'?c+=b[0]-32:c+=b[0];
}
cout<<c<<endl;
return 0;
}
1103. 字符串压缩
#include <iostream>
#include <string>
using namespace std;
int main() {
string a,b;
cin >> a;
int d = 0;
char r = a[0];
for (char c: a) {
if (c==r) {
d++;
} else {
if(d>1) b+=to_string(d);
b += r;
r = c;
d = 1;
}
}
if(d>1){
b+= to_string(d);
}
b+=r;
cout << b << endl;
return 0;
}
1104. 字符串解压
#include <iostream>
#include <string>
using namespace std;
int main() {
string c;
cin >> c;
string d = "";
int co = 0;
for (char ch : c) {
if (isdigit(ch)) {
co = co * 10 + (ch - '0');
} else {
if (co > 0) {
d += string(co, ch);
co = 0;
} else {
d += ch;
}
}
}
cout <<d<<endl;
return 0;
}
1105. 字符串连接
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string s1, s2;
cin >> s1 >> s2;
vector<bool> a(26, false);
string r = "";
for (char ch : s1) {
if (!a[ch - 'a']) {
r += ch;
a[ch - 'a'] = true;
}
}
for (char ch : s2) {
if (!a[ch - 'a']) {
r += ch;
a[ch - 'a'] = true;
}
}
cout <<r<< endl;
return 0;
}
1106. 统计单词个数
#include<iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
string a,b;
int c=0;
getline(cin,a);
stringstream ss(a);
while(ss>>b&&b!="0"){
c++;
}
cout<<c<<endl;
return 0;
}
1107. 求英文句子中的最长单词
#include<iostream>
#include<sstream>
#include <algorithm>
using namespace std;
int main(){
string a,b,c[305];
getline(cin,a);
stringstream ss(a);
int k=1,si=0;
while(ss>>b){
b.size()>si?si=b.size():si=si;
c[k]+=b;
k++;
}
for(string r:c){
if(r.size()==si){
cout<<r<<endl;
break;
}
}
return 0;
}
1108. 正整数N转换成一个二进制数
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(){
int n;
cin>>n;
if(n==0) cout<<"0"<<endl;
else {
string k;
while (n > 0) {
int b = n & 1;
k = to_string(b) + k;
n >>= 1;
}
cout << k << endl;
}
return 0;
}