题目网址
https://pintia.cn/problem-sets/994805260223102976/problems/994805260583813120
思路
从前往后数,碰到一样的就计数。我这里用三个数组实现,数组A存结果,B存数值,C存个数。例如,11,A=[1,1],B=[1],C=[2]。
注意点
测试点4的数组越界问题。解决办法是把数组开大,用全局变量。
C/C++中数组大小的上限:https://blog.csdn.net/zfjBIT/article/details/88638547
代码
#include<iostream>
#include<algorithm>
#define MAX 1000000
using namespace std;
int A[MAX];
int B[MAX];
int C[MAX];
int main() {
int n;
int data;
int len = 1;
int index;
int cnt;
cin >> data >> n;
A[0] = data;
for (int i = 1; i < n; i ++) {
index = 0;
cnt = 1;
for (int j = 0; j < len; j ++) {
if (j + 1 < len && A[j] == A[j + 1]) {
cnt ++;
} else {
B[index] = A[j];
C[index] = cnt;
cnt = 1;
index ++;
}
}
for (int k = 0; k < index * 2; k += 2) {
A[k] = B[k / 2];
A[k + 1] = C[k / 2];
}
len = index * 2;
}
for (int i = 0; i < len; i ++) {
cout << A[i];
}
return 0;
}