a2b
解码后:aab
#include<iostream>
using namespace std;
typedef struct Node {
char data;
int size;
Node* next;
}Node,*Linklist;
char* scan(char str[],int size) {
int i = 0;
Linklist head = new Node;
Linklist rear = head;
while (i<size-1) {
Linklist node = new Node;
if (str[i+1] > '1' && str[i+1] <= '9') {
node->data = str[i];
node->size = str[i + 1] - '0';
rear->next = node;
rear = node;
i += 2;
}
else {
node->data = str[i];
node->size = 1;
rear->next = node;
rear = node;
i += 1;
}
}if (i<size && (str[i] <'1' || str[i] > '9')) {
Linklist node = new Node;
node->data = str[i];
node->size = 1;
rear->next = node;
rear = node;
i += 1;
}
rear->next = nullptr;
Linklist p = head->next;
char* str1 = new char[i+1];
int k = 0;
while (p != nullptr) {
for (int j = p->size; j > 0;j--) {
str1[k] = p->data;
k++;
}
p = p->next;
}
str1[k] = '\0';
return str1;
}
int main() {
char str[40];
int i = 0;
do {
str[i] = getchar();
i++;
} while (str[i-1] != '\n');
char* str1 = scan(str,i-1);
cout <<str1;
return 0;
}
参考
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s[101];
cin>>s;
int len;
len = strlen(s);
for(int i = 0;i<len;i++){
if(s[i+1]>'1' && s[i+1]<='9' ){
for(char j = '1';j<=s[i+1];j++){
cout<<s[i];
}
i++;
}else{
cout<<s[i];
}
}
return 0;
}