Look and say 源码分析

来源:互联网 发布:域名cname 编辑:程序博客网 时间:2024/05/17 06:28

Look and say 源码分析:

Code:
  1. #include <iostream>   
  2. #include <fstream>   
  3. #include <string>   
  4. // each subsequent element is defined from the   
  5. //previous one by "verbally"   
  6. //For exam the string 122344111 can be descibled as   
  7. //"one 1,twp 2,one 1,one 3,twp 4,three 1.   
  8. using namespace std;   
  9.   
  10. int main(int argc,char *argv[])   
  11. {   
  12.     //ifstream cin("aaa.txt");   
  13.     string s,t;   
  14.     int n;   
  15.     cin>>n;   
  16.     for(int i=0;i<n ;i++)   
  17.     {   
  18.         cin>>s;   
  19.         int c=0;   
  20.         t=s[0];   
  21.         int temp =0;   
  22.         for(int j=0;j<s.size();j++)   
  23.         {   
  24.             if(s[j]==t[0])   
  25.             {   
  26.                 temp++;   
  27.                 if(j==s.size()-1)   
  28.                 {   
  29.                     printf("%d%c",temp,t[0]);   
  30.                 }   
  31.             }   
  32.             else  
  33.             {   
  34.                 printf("%d%c",temp,t[0]);   
  35.                 t[0]=s[j];   
  36.                 temp =1 ;   
  37.                 if(j==s.size()-1)   
  38.                 {   
  39.                     printf("%d%c",temp,t[0]);   
  40.                 }   
  41.             }   
  42.   
  43.         }   
  44.         cout<<endl;   
  45.     }   
  46.     return 0;   
  47. }   
  48.   
  49.   

 

原创粉丝点击