LOOK AND SAY

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(){
 ifstream cin("e.txt");
 string s,t;
 int n;
 cin>>n;
 for (int i =0;i < n;++i)
 {
  cin>>s;
  t=s[0];//t即爲不同的字符,雖然是字符串但只有一個字符
  int temp = 0;//用於記錄待比較的字符的個數
  for (string::size_type j = 0;j<s.size();++j)
  {
   if (s[j] == t[0])
   {
    temp++;

    if (j==s.size()-1)
    {
     //if(temp == 1)cout<<t[0];
     //else
      cout<<temp<<t[0];
    }
   }
   //不相等時,做:①輸出②更新t[0]
   else
   {
    //if(temp==1) cout<<t[0];
        //else
      cout<<temp<<t[0];

    t[0] = s[j];//由於s[j]不等於t[0],更新t[0]
    temp = 1;//更新temp值

    if (j ==s.size() -1)
    {
     //if(temp == 1)cout<<t[0];
     //else
      cout<<temp<<t[0];
    }
   }
  }
  cout<<endl;
  s = "";
 }
 return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章