74. 字符串替换

lantian1

C语言最终通过的代码:(我原先是把结果存在一个数组里面最后再输出,但第七个测试一直超时,于是我改为边判断边输出就通过了,有点奇怪)

include

include

int is_number(char str)
{
if(str >= ‘0’ && str <= ‘9’)
{
return str-‘0’;
}
return -1;
}

int main()
{
char p[10005],temp[10005]={0};
scanf(“%s”,p);
int fir=0,end=0,len=strlen(p),i,num=0,r;
if(is_number(p[len-1])==-1){p[len]=‘1’;p[len+1]=’\0’;len+=1;}
while(end<len)
{
if(is_number(p[end])==-1) end++;
else
{
for(i=end;is_number(p[i])!=-1;i++)
{
num*=10;
num+=is_number(p[i]);
}
r=i;
for(i=fir;i<end;i++)
{
temp[i-fir]=p[i];
}
temp[i-fir]=’\0’;
for(i=0;i<num;i++)
{
printf(“%s”,temp);//原先是通过strcat函数连接起来,等最大的while循环结束后再输出,现改为直接printf
}
end=r;
fir=end;
num=0;
}
}
return 0;
}

Andrew-Malcom
注意可能出现的特殊情况是一串字母后没有数字,此时直接输出该串字符即可
#include<iostream>
#include<string>
using namespace std;
int main(){
    string s;cin>>s;
    int i = 0;
    while(s[i]){
        string ss = "";
        int judge = 0;
        if(isalpha(s[i])){
            while(isalpha(s[i])){
                ss+=s[i++];
            }
        }
        if(s[i]>='0'&&s[i]<='9'){
            int f = 0;judge = 1;
            while(s[i]>='0'&&s[i]<='9'){
                f=f*10+(s[i]-'0');
                i++;
            }
            for(int i=0;i<f;i++){
                cout<<ss;
            }
        }
        if(judge == 0){
            cout<<ss;
        }
    }
}
会奔跑的bug

one_s = input()
my_num = []
i= 0
all_num = [str(k) for k in list(range(10))]
while i<len(one_s):
one_num = ‘0’
one_ss = ‘’
if one_s[i] in all_num:
while i<len(one_s):
if one_s[i] in all_num:
one_num += one_s[i]
i += 1
if i==len(one_s):
my_num.append(int(one_num))
else:
my_num.append(int(one_num))
break
else:
while i<len(one_s):
if one_s[i] not in all_num:
one_ss += one_s[i]
i += 1
if i==len(one_s):
my_num.append(one_ss)
else:
my_num.append(one_ss)
break
p_ss = ‘’
j=0
my_num_len = len(my_num)
for j in range(1,my_num_len):
if (j+1)%2==0:
for k in range(my_num[j]):
p_ss += my_num[j-1]
else:
if j == my_num_len-1:
p_ss += my_num[j]
print(p_ss)

Commando
#include <bits/stdc++.h>

using namespace std;

int main() {
    string str, res, tmp;
    cin >> str;

    int value;

    for (int i = 0; i < str.size();) {
        if (isalpha(str[i])) tmp += str[i++];
        else {
            int m = i;
            while (i < str.size() && isdigit(str[i])) ++i;

            stringstream ss(string(str.begin() + m, str.begin() + i));
            ss >> value;

            while (value--) res += tmp;

            tmp.clear();
        }
    }

    if (!tmp.empty()) res += tmp;
    cout << res << endl;
}
你当前正在回复 博客/题目
存在问题!