1016 Numbers That Count

来源:互联网 发布:北邮软件学院 编辑:程序博客网 时间:2024/04/28 10:37

校内练习赛的时候做的,题目很长,要细心看规则

直接模拟即可

  1. //4452572_AC_157MS_924K
  2. /**********************************************************************
  3. *       Online Judge   : POJ
  4. *       Problem Title  : Numbers That Count
  5. *       ID             : 1016
  6. *       Date           : 12/4/2008
  7. *       Time           : 14:38:9
  8. *       Computer Name  : EVERLASTING-PC
  9. ***********************************************************************/
  10. #include<iostream>
  11. #include<string>
  12. #include<sstream>
  13. using namespace std;
  14. int t,step,i;
  15. string his[18];
  16. string s;
  17. int c[10];
  18. stringstream ss;
  19. bool found;
  20. int main()
  21. {
  22.     {
  23.         while(cin>>s&&s!="-1")
  24.         {
  25.             found=false;
  26.             his[0]=s;
  27.             step=0;
  28.             while(1)
  29.             {
  30.                 step++;
  31.                 memset(c,0,sizeof(c));
  32.                 s=his[step-1];
  33.                 for(i=0;i<s.length();++i)
  34.                 {
  35.                     c[s[i]-48]++;
  36.                 }
  37.                 ss.clear();
  38.                 for(i=0;i<10;++i)
  39.                 {
  40.                     if(c[i]!=0)
  41.                     {
  42.                         ss<<c[i]<<i;
  43.                     }
  44.                 }
  45.                 ss>>s;
  46.                 if(his[0]==s)
  47.                 {
  48.                     if(step==1)
  49.                     {
  50.                         cout<<his[0]<<" is self-inventorying/n";
  51.                         found=true;
  52.                     }
  53.                 }
  54.                 else if(his[step-1]==s)
  55.                 {   cout<<his[0]<<" is self-inventorying after "<<step-1<<" steps/n";
  56.                 found=true;
  57.                 }
  58.                 else
  59.                     for(i=step-1;i>=0;--i)
  60.                     {
  61.                         if(his[i]==s)
  62.                         {
  63.                             cout<<his[0]<<" enters an inventory loop of length "<<step-i<<endl;
  64.                             found=true;
  65.                             break;
  66.                         }
  67.                     }
  68.                     if(found)
  69.                     {
  70.                         break;
  71.                     }
  72.                     his[step]=s;
  73.                     if(step==15)
  74.                     {
  75.                         break;
  76.                     }
  77.             }
  78.             if(!found)
  79.             {
  80.                 cout<<his[0]<<" can not be classified after 15 iterations/n";
  81.             }
  82.         }
  83.     }
  84.     return 0;
  85. }
原创粉丝点击