题目大概:
输入n,输入n行数字,接下来,每行按输入顺序,输出每个数字出现的次数后面紧跟该数字。
思路:
用string储存,用循环读取每个数字,再计数,输出。
感想:
这个题有个别数字需要初始化,需要注意。
代码:
#include <iostream>
#include <string>
using namespace std ;
int main ()
{ int n ;
cin >>n ;
for ( int i = 0 ;i <n ;i ++)
{ string a ;
int k ,j = 1 ;
cin >>a ;
k =a [ 0 ];
for ( int t = 1 ;t <a . size ();t ++)
{ if (k ==a [t ])j ++;
else {cout <<j <<a [t -1 ];k =a [t ];j = 1 ; }
}
cout <<j <<a [a . size ()- 1 ]<<endl ;
}
return 0 ;
}