8 条题解

  • 1
    @ 2025-2-10 10:57:23
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    	string str;
    	cin >> str;
    	int sum = 1;
    	for (int i = 0; i < (int)str.length(); i++)
    	{
    		if (str[i] == str[i + 1]) sum++;
    		else
    		{
    			cout << str[i] << sum;
    			sum = 1;
    		}
    	}
    	return 0;
    }
    
    • 0
      @ 2025-3-6 20:40:11
      /*
      思路分析:
      思路一(error): 1 将aabbbaad分割成aa bbb aa d;
                  X 利用stringstream A(B) 函数进行分割
                  string获取字符串  .c_str()函数进行字符串转换
                  strcpy() 函数进行复制 也可以用 .copy函数
                  利用循环记录字符数量 拼接到新的字符串 加上空格
              2 将aa bbb aa d转换成a2 b3 a2 d1;
                  提取每一个字符串第一个字符 在后面加上字符串的长度 sizeof()
              3 将a2 b3 a2 d1链接起来形成a2b3a2d1。
                  在第二步用循环输出
      思路二: 1. 用一个字符串接收数据 str
             2. 循环嵌套 外循环遍历整个字符串注意.size()函数数据类型为size_t
                所以循环变量定义时也要用size_t    size_t i = 0;
                内循环依次遍历对比字符char str1 直到str1与后面字符不同时停止内循环
                相同则count++ 变量i++ 依次往后遍历对比
             3. 内循环结束后将得到的字符和数量添加到新的字符串后 str3 
                   字符和数量分开添加 字符用 push_back() 数字用 to_string()
      */
      
      #include <bits/stdc++.h>
      using namespace std;
      
      int main() 
      {
          //字符串接收一行数据
          string str;
          getline(cin , str);
          string str2; //新的字符串
          size_t i = 0;
          /*
          这里,input.size() 返回的是 size_t 类型,而 i 是 int 类型。
          如果字符串非常大(接近 size_t 的最大值),可能会导致类型转换问题。
          */
          //循环嵌套
          while(i < str.size()) { //遍历每个字符
              //用字符变量来遍历
              char s = str[i];
              size_t count = 0; //记录重复数量
              while( str[i] == s && i < str.size()) { //统计重复字符
                  count++;
                  i++;
              }
              //1将得到的字符和数量拼接到新的字符串上面
              //str2 += string(count , s) + " ";
              //2将得到的字符push_back()和数量to_string()进行拼接
              str2.push_back(s);
              str2 += to_string(count);
          }
          cout << str2;
          return 0;
      }
      
      • 0
        @ 2025-2-3 23:00:07
        #include<bits/stdc++.h>
        using namespace std;
        
        int main()
        {
            string input;
            cin >> input;
            string output;
            int len = input.size();
            char temp = input[0];
            int count = 1;
            for(int i = 1; i <= len; i++)
            {
                // 如果后面的字符与前面的一样
                if(input[i] == temp)
                {
                    count++;
                }
                else
                {
                    output+=temp;
                    output+=to_string(count);
                    temp=input[i];
                    count = 1;
                }
            }
            cout << output;
        }
        
        • 0
          @ 2025-1-19 19:07:06

          #include <iostream> #include<sstream> using namespace std; int main() { int p = 0, q = 1, num = 1; string str; stringstream ss; cin >> str; while (q < str.size()) { if (str[p] == str[q]) { num++; if (q == str.size() - 1) { ss << str[p] << num; } q++; } else { if(q!=str.size()-1){ ss << str[p] << num; p = q;q = q + 1;num = 1; } else { ss << str[p] << num; num = 1; ss << str[q] << num; q++; } } } cout << ss.str(); return 0; }

          • 0
            @ 2024-12-29 14:13:05
            #include <bits/stdc++.h>
            
            using namespace std;
            
            typedef long long ll;
            
            int main() {
                string s;
                cin >> s;
            //    ll lens = s.size();
                string ans = "";
                char t = s[0];
                ll cnt = 0;
                for (int i = 0; i < s.size(); i++) {
                    if (i - 1 >= 0 && s[i] != t) {
                        ans += t;
                        ans += to_string(cnt);
                        cnt = 0;
                    }
                    t = s[i];
                    cnt++;
                }
                ans += t;
                ans += to_string(cnt);
            
                cout << ans << endl;
                return 0;
            }
            
            • 0
              @ 2024-4-14 17:39:13
              #include<cstdio>
              #include<iostream>
              using namespace std;
              
              int main(){
              	
              	string s;
              	cin>>s;
              	int len=s.length();
              	char now=s[0];
              	int count=1;
              	
              	for(int i=1;i<len;++i){
              		if(s[i]==s[i-1]){
              			count++;
              		}
              		else{
              			cout<<now<<count;
              			count=1;
              			now=s[i];
              		}
              	}
              	
              	cout<<now<<count;
              
              	return 0;
              }
              
              • 0
                @ 2024-4-12 11:59:00
                #include<bits/stdc++.h>
                using namespace std;
                string s;
                int main(){
                    ios::sync_with_stdio(0);
                    cin.tie(0);
                    cout.tie(0);
                    cin>>s;
                    for(int i=0;i<s.size();){
                        int ans=1;
                        int j=i+1;
                        while(s[j]==s[i]){
                            ans++;
                            j++;
                        }
                            cout<<s[i]<<ans;
                            i=j;
                    }
                }
                
                • -1
                  @ 2024-4-13 17:07:09
                  s = input()
                  n = len(s)
                  t = 1
                  ans = 0
                  for i in range(n-1):
                      if s[i]==s[i+1]:
                          t+=1
                      else:
                          print(s[i],end = "")
                          print(t,end = "")
                          t=1
                  print(s[i+1],end = "")
                  print(t,end = "")
                  
                  • 1

                  信息

                  ID
                  65
                  时间
                  1000ms
                  内存
                  256MiB
                  难度
                  4
                  标签
                  递交数
                  230
                  已通过
                  109
                  上传者