1. 程式人生 > >HustOJ - 1026

HustOJ - 1026

相同 clas open 否則 style online pen using pro

技術分享
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,i,j;
 7     string s,f;
 8     cin>>n;//輸入行數 
 9     for (i=0;i<n;i++)
10     {
11         cin>>s;//循環輸入每行數據 
12         int t=0;
13         f=s[0];//判斷有幾個和f相等的 
14         for (j=0;j<s.size();j++)
15 { 16 if (s[j]==f[0])//如果有一個和f的值相等 17 { 18 t++;//計算個數 19 if (j==s.size()-1)//如果最後一個 20 { 21 if (t==1)cout<<f[0];//如果這個數據只有一個就輸出,比如:A 22 else cout<<t<<f[0];//否則輸出有幾個這個數據,比如:AAAAAA輸出6A
23 } 24 } 25 else 26 {//把最開始一組相同數據輸出 27 if (t==1)cout<<f[0]; 28 else cout<<t<<f[0]; 29 //令f[0]等於下一組相同數據的開頭 30 f[0]=s[j]; 31 t=1;//把計算個數歸零 32 if
(j==s.size()-1)//如果是最後一個,輸出。否則執行上面的統計並輸出。 33 { 34 if (t==1)cout<<f[0]; 35 else cout<<t<<f[0]; 36 } 37 } 38 } 39 cout<<endl;//換行 40 s="";//歸零 41 } 42 return 0; 43 }
1026 Encoding

ZOJ:2478

HustOJ - 1026