题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805260583813120
特别注意一下to_string的用法,里面参数要填成long long类型的才能在vs上通过,但是在PTA上面没问题

代码

  1. #include<cstdio>
  2. #include<string>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. int main(){
  7. string d;
  8. int n, j;
  9. cin>>d;
  10. scanf("%d", &n);
  11. for(int cnt = 1; cnt < n; cnt++){
  12. string t;
  13. for(int i = 0; i < d.size(); i = j){
  14. for(j = i; j < d.size() && d[j]==d[i]; j++);
  15. t += d[i] + to_string((long long)(j - i));
  16. }
  17. d = t;
  18. }
  19. cout<<d<<endl;
  20. return 0;
  21. }

理解错了题意的写法

  1. #include<cstdio>
  2. #include<string>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. int main(){
  7. string d;
  8. int n;
  9. cin>>d;
  10. scanf("%d", &n);
  11. for(int a = 0; a < n; a++){
  12. int hash[1000] = {0};
  13. for(int i = 0; i < d.size(); i++){
  14. hash[d[i] - '0']++;
  15. }
  16. d = "";
  17. for(int i = 0; i < 1000; i++){
  18. if(hash[i] != 0){
  19. d += i + '0';
  20. while(hash[i] != 0){
  21. d += hash[i] % 10 + '0';
  22. hash[i] /= 10;
  23. }
  24. }
  25. }
  26. cout<<d<<endl;
  27. }
  28. }