AcWing 773. 字符串插入
原题链接
中等
作者:
Gease
,
2021-02-04 19:27:15
,
所有人可见
,
阅读 210
将字符串插入到字符串中,再输出新字符串,直接输出的我认为是取巧的
不使用string
#include <cstdio>
int main(){
char str[12], substr[4], strN[16];
while(~scanf("%s %s", str, substr)){
int max = 0;
for (int i = 0; str[i]; i++)
if(str[i]>str[max]) max = i;
int k = 0;
for (int i = 0; i <= max; i++)strN[k++] = str[i];
for (int i = 0; substr[i]; i++)strN[k++] = substr[i];
for (int i = max + 1; str[i]; i++)strN[k++] = str[i];
for (int i = 0; i < k; i++)printf("%c", strN[i]);
puts("");
}
return 0;
}
这个方法时间较长,常数时间操作不如第一种方法
使用string
#include <iostream>
using namespace std;
int main(){
string a, b;
while(cin >> a >> b){
int max = 0;
for (int i = 1; i < a.size(); i++){
if(a[i] > a[max])max = i;
}
a.insert(max + 1, b);
cout << a << endl;
}
return 0;
}